@embrace-io/web-sdk
Version:
89 lines (88 loc) • 3.84 kB
JavaScript
import { KEY_EMB_INSTRUMENTATION, KEY_EMB_TYPE } from "../../../constants/attributes.js";
import { createPerformanceObserver } from "../../../utils/performanceObserver/performanceObserver.js";
import { EmbraceInstrumentationBase } from "../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js";
import { KEY_EMB_USER_TIMING_DURATION, KEY_EMB_USER_TIMING_ENTRY_TYPE, KEY_EMB_USER_TIMING_NAME, KEY_EMB_USER_TIMING_START_TIME, USER_TIMING_EVENT_NAME } from "./constants.js";
import { SeverityNumber } from "@opentelemetry/api-logs";
//#region src/instrumentations/user-timing/UserTimingInstrumentation/UserTimingInstrumentation.ts
var UserTimingInstrumentation = class extends 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 = createPerformanceObserver("mark", this._processEntry.bind(this), { diag: this._diag });
if (!this._markObserver) this._diag.error("failed to enable mark observer");
this._measureObserver = 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: {
[KEY_EMB_TYPE]: "ux.user_timing",
[KEY_EMB_INSTRUMENTATION]: "user_timing",
[KEY_EMB_USER_TIMING_ENTRY_TYPE]: entry.entryType,
[KEY_EMB_USER_TIMING_START_TIME]: this.perf.millisFromZeroTime(entry.startTime),
[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: USER_TIMING_EVENT_NAME,
severityNumber: SeverityNumber.INFO,
attributes: {
[KEY_EMB_TYPE]: "ux.user_timing",
[KEY_EMB_USER_TIMING_NAME]: entry.name,
[KEY_EMB_USER_TIMING_START_TIME]: this.perf.millisFromZeroTime(entry.startTime),
[KEY_EMB_USER_TIMING_DURATION]: entry.duration,
[KEY_EMB_USER_TIMING_ENTRY_TYPE]: entry.entryType
},
body
});
}
};
//#endregion
export { UserTimingInstrumentation };
//# sourceMappingURL=UserTimingInstrumentation.js.map