{"version":3,"file":"NavigationInstrumentation.cjs","names":["EmbraceInstrumentationBase","page","KEY_EMB_TYPE","KEY_EMB_PAGE_PATH","KEY_EMB_PAGE_ID","KEY_EMB_INSTRUMENTATION"],"sources":["../../../../src/instrumentations/navigation/NavigationInstrumentation/NavigationInstrumentation.ts"],"sourcesContent":["import type { Span } from '@opentelemetry/api';\nimport type { Route } from '../../../api-page/index.ts';\nimport { page } from '../../../api-page/index.ts';\nimport {\n  EMB_NAVIGATION_INSTRUMENTATIONS,\n  EMB_TYPES,\n  KEY_EMB_INSTRUMENTATION,\n  KEY_EMB_PAGE_ID,\n  KEY_EMB_PAGE_PATH,\n  KEY_EMB_TYPE,\n} from '../../../constants/index.ts';\nimport { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport type { NavigationInstrumentationArgs } from './types.ts';\n\n// Regular expression to match path options in the format \"(option)\"\n// Used to clean up paths that are like \"/order/:orderState(pending|shipped|delivered)/type:(sale|normal)\" to \"/order/:orderState/:type\"\n// Could be simplified but done this way to prevent: https://javascript.info/regexp-catastrophic-backtracking\nconst PATH_OPTIONS_RE = /\\([^()]+\\)/g;\n\nexport class NavigationInstrumentation extends EmbraceInstrumentationBase {\n  private readonly _shouldCleanupPathOptionsFromRouteName: boolean = true;\n  private _currentRouteSpan: Span | null = null;\n  private _instrumentationType: EMB_NAVIGATION_INSTRUMENTATIONS =\n    EMB_NAVIGATION_INSTRUMENTATIONS.Manual;\n  private _removeSessionStartedFn: (() => void) | null = null;\n  private _removeSessionEndedFn: (() => void) | null = null;\n\n  public constructor({\n    diag,\n    shouldCleanupPathOptionsFromRouteName = true,\n  }: NavigationInstrumentationArgs) {\n    super({\n      instrumentationName: 'NavigationInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      config: {},\n    });\n\n    this._shouldCleanupPathOptionsFromRouteName =\n      shouldCleanupPathOptionsFromRouteName;\n\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public setInstrumentationType = (\n    instrumentationType: EMB_NAVIGATION_INSTRUMENTATIONS,\n  ) => {\n    this._instrumentationType = instrumentationType;\n  };\n\n  public setCurrentRoute = (route: Route) => {\n    if (!this._config.enabled) {\n      return;\n    }\n\n    const currentRoute = page.getCurrentRoute();\n\n    if (route.url !== currentRoute?.url) {\n      page.setCurrentRoute(route);\n\n      this._endRouteSpan(currentRoute);\n      this._startRouteSpan(route);\n    }\n  };\n\n  private readonly _setupSessionListeners = () => {\n    if (!this._removeSessionStartedFn) {\n      this._removeSessionStartedFn =\n        this.sessionManager.addSessionStartedListener(() => {\n          const currentRoute = page.getCurrentRoute();\n\n          if (currentRoute && !this._currentRouteSpan) {\n            this._diag.debug('Session started, starting route span.');\n\n            this._startRouteSpan(currentRoute);\n          }\n        });\n    }\n\n    if (!this._removeSessionEndedFn) {\n      this._removeSessionEndedFn = this.sessionManager.addSessionEndedListener(\n        () => {\n          if (this._currentRouteSpan) {\n            this._diag.debug('Session ended, ending route span.');\n\n            this._endRouteSpan(page.getCurrentRoute());\n          }\n        },\n      );\n    }\n  };\n\n  private readonly _cleanUpSessionListeners = () => {\n    if (this._removeSessionStartedFn) {\n      this._removeSessionStartedFn();\n      this._removeSessionStartedFn = null;\n    }\n\n    if (this._removeSessionEndedFn) {\n      this._removeSessionEndedFn();\n      this._removeSessionEndedFn = null;\n    }\n  };\n\n  private readonly _startRouteSpan = (route: Route): Span => {\n    this._diag.debug(`Starting route span for url: ${route.url}`);\n    this._setupSessionListeners();\n\n    const pathName = this._shouldCleanupPathOptionsFromRouteName\n      ? route.path.replace(PATH_OPTIONS_RE, '')\n      : route.path;\n    this._currentRouteSpan = this.tracer.startSpan(pathName, {\n      attributes: {\n        [KEY_EMB_TYPE]: EMB_TYPES.Surface,\n        [KEY_EMB_PAGE_PATH]: pathName,\n        [KEY_EMB_PAGE_ID]: page.getCurrentPageId() || undefined,\n      },\n    });\n\n    this._currentRouteSpan.setAttribute(\n      KEY_EMB_INSTRUMENTATION,\n      this._instrumentationType,\n    );\n\n    return this._currentRouteSpan;\n  };\n\n  private readonly _endRouteSpan = (currentRoute: Route | null) => {\n    if (this._currentRouteSpan) {\n      if (currentRoute) {\n        this._diag.debug(`Ending route span for url: ${currentRoute.url}`);\n      }\n\n      this._currentRouteSpan.end();\n      this._currentRouteSpan = null;\n    }\n  };\n\n  public enable = () => {\n    this.setConfig({\n      enabled: true,\n    });\n    this._diag.debug(\n      'NavigationInstrumentation enabled, listening for navigation events.',\n    );\n  };\n\n  public disable = () => {\n    this._cleanUpSessionListeners();\n    this.setConfig({\n      enabled: false,\n    });\n    this._diag.debug(\n      'NavigationInstrumentation disabled, stopped listening for navigation events.',\n    );\n  };\n}\n"],"mappings":";;;;;AAiBA,MAAM,kBAAkB;AAExB,IAAa,4BAAb,cAA+CA,+EAAAA,2BAA2B;CACxE,yCAAmE;CACnE,oBAAyC;CACzC,uBAAQ;CAER,0BAAuD;CACvD,wBAAqD;CAErD,YAAmB,EACjB,MACA,wCAAwC,QACR;AAChC,QAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA,QAAQ,EAAE;GACX,CAAC;AAEF,OAAK,yCACH;AAEF,MAAI,KAAK,QAAQ,QACf,MAAK,QAAQ;;CAIjB,0BACE,wBACG;AACH,OAAK,uBAAuB;;CAG9B,mBAA0B,UAAiB;AACzC,MAAI,CAAC,KAAK,QAAQ,QAChB;EAGF,MAAM,eAAeC,yBAAAA,KAAK,iBAAiB;AAE3C,MAAI,MAAM,QAAQ,cAAc,KAAK;AACnC,4BAAA,KAAK,gBAAgB,MAAM;AAE3B,QAAK,cAAc,aAAa;AAChC,QAAK,gBAAgB,MAAM;;;CAI/B,+BAAgD;AAC9C,MAAI,CAAC,KAAK,wBACR,MAAK,0BACH,KAAK,eAAe,gCAAgC;GAClD,MAAM,eAAeA,yBAAAA,KAAK,iBAAiB;AAE3C,OAAI,gBAAgB,CAAC,KAAK,mBAAmB;AAC3C,SAAK,MAAM,MAAM,wCAAwC;AAEzD,SAAK,g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