UNPKG

@embrace-io/web-sdk

Version:
57 lines (56 loc) 1.98 kB
import { KEY_EMB_TYPE } from "../../../constants/attributes.js"; import { EmbraceInstrumentationBase } from "../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js"; import { KEY_EMB_SERVER_TIMING_DESCRIPTION, KEY_EMB_SERVER_TIMING_DURATION, KEY_EMB_SERVER_TIMING_NAME, SERVER_TIMING_EVENT_NAME } from "./constants.js"; import { SeverityNumber } from "@opentelemetry/api-logs"; //#region src/instrumentations/server-timing/ServerTimingInstrumentation/ServerTimingInstrumentation.ts var ServerTimingInstrumentation = class extends EmbraceInstrumentationBase { _onLoad; _performanceCollected = false; constructor({ diag, perf, limitManager } = {}) { super({ instrumentationName: "ServerTimingInstrumentation", instrumentationVersion: "1.0.0", diag, perf, limitManager, config: {} }); this._onLoad = () => { this._readServerTiming(); }; if (this._config.enabled) this.enable(); } onEnable() { window.removeEventListener("load", this._onLoad); if (window.document.readyState === "complete") { this._readServerTiming(); return; } window.addEventListener("load", this._onLoad); } onDisable() { window.removeEventListener("load", this._onLoad); } _readServerTiming() { if (this._performanceCollected) return; this._performanceCollected = true; const serverTiming = performance.getEntriesByType("navigation")[0]?.serverTiming; if (!serverTiming?.length) return; for (const entry of serverTiming) { if (this.limitManager?.limitServerTimingEntry()) return; this.logger.emit({ eventName: SERVER_TIMING_EVENT_NAME, severityNumber: SeverityNumber.INFO, attributes: { [KEY_EMB_TYPE]: "ux.server_timing", [KEY_EMB_SERVER_TIMING_NAME]: entry.name, [KEY_EMB_SERVER_TIMING_DURATION]: entry.duration, [KEY_EMB_SERVER_TIMING_DESCRIPTION]: entry.description } }); } } }; //#endregion export { ServerTimingInstrumentation }; //# sourceMappingURL=ServerTimingInstrumentation.js.map