UNPKG

@embrace-io/web-sdk

Version:
90 lines (89 loc) 4.84 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_constants_attributes = require("../../../constants/attributes.cjs"); const require_utils_performanceObserver_performanceObserver = require("../../../utils/performanceObserver/performanceObserver.cjs"); const require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase = require("../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.cjs"); const require_instrumentations_user_timing_UserTimingInstrumentation_constants = require("./constants.cjs"); let _opentelemetry_api_logs = require("@opentelemetry/api-logs"); //#region src/instrumentations/user-timing/UserTimingInstrumentation/UserTimingInstrumentation.ts var UserTimingInstrumentation = class extends require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase.EmbraceInstrumentationBase { _markObserver = null; _measureObserver = null; _seenEntries = /* @__PURE__ */ new Set(); _allowedEntries; constructor({ diag, perf, limitManager, allowedEntries } = {}) { super({ instrumentationName: "UserTimingInstrumentation", instrumentationVersion: "1.0.0", diag, perf, limitManager, config: {} }); this._allowedEntries = allowedEntries; if (this._config.enabled) this.enable(); } onEnable() { this._seenEntries = /* @__PURE__ */ new Set(); if (this._markObserver) this._markObserver.disconnect(); if (this._measureObserver) this._measureObserver.disconnect(); this._markObserver = require_utils_performanceObserver_performanceObserver.createPerformanceObserver("mark", this._processEntry.bind(this), { diag: this._diag }); if (!this._markObserver) this._diag.error("failed to enable mark observer"); this._measureObserver = require_utils_performanceObserver_performanceObserver.createPerformanceObserver("measure", (entry) => this._processEntry(entry), { diag: this._diag }); if (!this._measureObserver) this._diag.error("failed to enable measure observer"); if (!this._markObserver && !this._measureObserver) this._isEnabled = false; } onDisable() { if (this._markObserver) { this._markObserver.disconnect(); this._markObserver = null; } if (this._measureObserver) { this._measureObserver.disconnect(); this._measureObserver = null; } this._seenEntries = /* @__PURE__ */ new Set(); } _processEntry(entry) { if (!this._isEnabled) return; if (this._allowedEntries !== void 0) { if (!(Array.isArray(this._allowedEntries) ? this._allowedEntries.includes(entry.name) : this._allowedEntries(entry))) return; } const key = `${location.href}::${entry.name}`; if (this._seenEntries.has(key)) return; this._seenEntries.add(key); if (this.limitManager?.limitUserTimingEntry(entry.entryType)) return; if (entry.entryType === "measure") { const measureDetail = entry.detail; this.tracer.startSpan(entry.name, { startTime: this.perf.epochMillisFromOrigin(entry.startTime), attributes: { [require_constants_attributes.KEY_EMB_TYPE]: "ux.user_timing", [require_constants_attributes.KEY_EMB_INSTRUMENTATION]: "user_timing", [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_ENTRY_TYPE]: entry.entryType, [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_START_TIME]: this.perf.millisFromZeroTime(entry.startTime), [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_DURATION]: entry.duration, ...measureDetail != null && { ["emb.user_timing.detail"]: JSON.stringify(measureDetail) } } }).end(this.perf.epochMillisFromOrigin(entry.startTime + entry.duration)); return; } const detail = entry.detail; const body = detail != null ? JSON.stringify(detail) : void 0; this.logger.emit({ timestamp: this.perf.epochMillisFromOrigin(entry.startTime), eventName: require_instrumentations_user_timing_UserTimingInstrumentation_constants.USER_TIMING_EVENT_NAME, severityNumber: _opentelemetry_api_logs.SeverityNumber.INFO, attributes: { [require_constants_attributes.KEY_EMB_TYPE]: "ux.user_timing", [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_NAME]: entry.name, [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_START_TIME]: this.perf.millisFromZeroTime(entry.startTime), [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_DURATION]: entry.duration, [require_instrumentations_user_timing_UserTimingInstrumentation_constants.KEY_EMB_USER_TIMING_ENTRY_TYPE]: entry.entryType }, body }); } }; //#endregion exports.UserTimingInstrumentation = UserTimingInstrumentation; //# sourceMappingURL=UserTimingInstrumentation.cjs.map