@embrace-io/web-sdk
Version:
3 lines (2 loc) • 2.67 kB
JavaScript
import{EmbraceInstrumentationBase as t}from"../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js";import{EMB_TYPES as e,KEY_VIEW_NAME as n,KEY_EMB_TYPE as s,KEY_EMB_INSTRUMENTATION as i}from"../../../constants/attributes.js";const r=/\(.*?\)/g;class o extends t{constructor({diag:t,shouldCleanupPathOptionsFromRouteName:o=!0}){super({instrumentationName:"NavigationInstrumentation",instrumentationVersion:"1.0.0",diag:t,config:{}}),this._shouldCleanupPathOptionsFromRouteName=!0,this._currentRoute=null,this._currentRouteSpan=null,this._instrumentationType=null,this._removeSessionStartedFn=null,this._removeSessionEndedFn=null,this.setInstrumentationType=t=>{this._instrumentationType=t},this.setCurrentRoute=t=>{var e;this._config.enabled&&t.url!==(null===(e=this._currentRoute)||void 0===e?void 0:e.url)&&(this._endRouteSpan(),this._startRouteSpan(t),this._currentRoute=t)},this._setupSessionListeners=()=>{this._removeSessionStartedFn||(this._removeSessionStartedFn=this.sessionManager.addSessionStartedListener((()=>{this._currentRoute&&!this._currentRouteSpan&&(this._diag.debug("Session started, starting route span."),this._startRouteSpan(this._currentRoute))}))),this._removeSessionEndedFn||(this._removeSessionEndedFn=this.sessionManager.addSessionEndedListener((()=>{this._currentRouteSpan&&(this._diag.debug("Session ended, ending route span."),this._endRouteSpan())})))},this._cleanUpSessionListeners=()=>{this._removeSessionStartedFn&&(this._removeSessionStartedFn(),this._removeSessionStartedFn=null),this._removeSessionEndedFn&&(this._removeSessionEndedFn(),this._removeSessionEndedFn=null)},this._startRouteSpan=t=>{this._diag.debug(`Starting route span for url: ${t.url}`),this._setupSessionListeners();const o=this._shouldCleanupPathOptionsFromRouteName?t.path.replace(r,""):t.path;return this._currentRouteSpan=this.tracer.startSpan(o,{attributes:{[s]:e.View,[n]:o}}),this._instrumentationType&&this._currentRouteSpan.setAttribute(i,this._instrumentationType),this._currentRouteSpan},this._endRouteSpan=()=>{this._currentRouteSpan&&this._currentRoute&&(this._diag.debug(`Ending route span for url: ${this._currentRoute.url}`),this._currentRouteSpan.end(),this._currentRouteSpan=null)},this.enable=()=>{this._diag.debug("NavigationInstrumentation enabled, listening for navigation events.")},this.disable=()=>{this._cleanUpSessionListeners(),this.setConfig({enabled:!1}),this._diag.debug("NavigationInstrumentation disabled, stopped listening for navigation events.")},this._shouldCleanupPathOptionsFromRouteName=o,this._config.enabled&&this.enable()}}export{o as NavigationInstrumentation};
//# sourceMappingURL=NavigationInstrumentation.js.map