@embrace-io/web-sdk
Version:
95 lines (94 loc) • 3.72 kB
JavaScript
import { KEY_EMB_PAGE_ID, KEY_EMB_PAGE_PATH, KEY_EMB_TYPE } from "../../../constants/attributes.js";
import { page } from "../../../api-page/pageAPI.js";
import { EmbraceInstrumentationBase } from "../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js";
//#region src/instrumentations/navigation/NavigationInstrumentation/NavigationInstrumentation.ts
const PATH_OPTIONS_RE = /\([^()]+\)/g;
var NavigationInstrumentation = class extends EmbraceInstrumentationBase {
_shouldCleanupPathOptionsFromRouteName = true;
_pageManager;
_currentRouteSpan = null;
_currentRouteSpanUrl = null;
_currentRouteSpanPath = null;
constructor({ diag, shouldCleanupPathOptionsFromRouteName = true, pageManager }) {
super({
instrumentationName: "NavigationInstrumentation",
instrumentationVersion: "1.0.0",
diag,
config: {}
});
this._shouldCleanupPathOptionsFromRouteName = shouldCleanupPathOptionsFromRouteName;
this._pageManager = pageManager ?? page.getPageManager();
this._pageManager.addRouteChangedListener(this._onRouteChanged);
if (this._config.enabled) this.enable();
const currentRoute = this._pageManager.getCurrentRoute();
if (currentRoute) this._onRouteChanged(currentRoute);
}
_onRouteChanged = (route) => {
if (!this._config.enabled) return;
if (this._currentRouteSpan && this._currentRouteSpanUrl === route.url) {
if (this._currentRouteSpanPath !== route.path) this._renameCurrentRouteSpan(route.path);
return;
}
this._endRouteSpan();
this._startRouteSpan(route);
};
_startRouteSpan = (route) => {
this._diag.debug(`Starting route span for url: ${route.url}`);
const pathName = this._cleanPath(route.path);
this._currentRouteSpan = this.tracer.startSpan(pathName, {
startTime: this.perf.getNowMillis(),
attributes: {
[KEY_EMB_TYPE]: "ux.surface",
[KEY_EMB_PAGE_PATH]: pathName,
[KEY_EMB_PAGE_ID]: this._pageManager.getCurrentPageId() || void 0
}
});
this._currentRouteSpanUrl = route.url;
this._currentRouteSpanPath = route.path;
return this._currentRouteSpan;
};
_endRouteSpan = () => {
if (this._currentRouteSpan) {
this._diag.debug(`Ending route span for url: ${this._currentRouteSpanUrl ?? "unknown"}`);
this._currentRouteSpan.end(this.perf.getNowMillis());
this._currentRouteSpan = null;
this._currentRouteSpanUrl = null;
this._currentRouteSpanPath = null;
}
};
_renameCurrentRouteSpan = (path) => {
const pathName = this._cleanPath(path);
this._diag.debug(`Resolved route span path to: ${pathName}`);
this._currentRouteSpan?.updateName(pathName);
this._currentRouteSpan?.setAttribute(KEY_EMB_PAGE_PATH, pathName);
this._currentRouteSpanPath = path;
};
_cleanPath = (path) => this._shouldCleanupPathOptionsFromRouteName ? path.replace(PATH_OPTIONS_RE, "") : path;
_onSessionPartEnded = () => {
if (this._currentRouteSpan) {
this._diag.debug("Session ended, ending route span.");
this._endRouteSpan();
}
};
_onSessionPartStarted = ({ reason }) => {
if (reason === "web_soft_navigation") return;
const currentRoute = this._pageManager.getCurrentRoute();
if (currentRoute) this._onRouteChanged(currentRoute);
};
onEnable = () => {
this.setConfig({ enabled: true });
this.setSessionPartListeners({
start: this._onSessionPartStarted,
end: this._onSessionPartEnded
});
this._diag.debug("NavigationInstrumentation enabled, listening for navigation events.");
};
onDisable = () => {
this._endRouteSpan();
this.setConfig({ enabled: false });
this._diag.debug("NavigationInstrumentation disabled, stopped listening for navigation events.");
};
};
//#endregion
export { NavigationInstrumentation };
//# sourceMappingURL=NavigationInstrumentation.js.map