@embrace-io/web-sdk
Version:
62 lines (61 loc) • 2.73 kB
JavaScript
import { KEY_EMB_TYPE } from "../../../constants/attributes.js";
import { createPerformanceObserver, isEntryTypeSupported } from "../../../utils/performanceObserver/performanceObserver.js";
import { EmbraceInstrumentationBase } from "../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js";
import { KEY_EMB_ELEMENT_TIMING_ELEMENT, KEY_EMB_ELEMENT_TIMING_IDENTIFIER, KEY_EMB_ELEMENT_TIMING_LOAD_TIME, KEY_EMB_ELEMENT_TIMING_NATURAL_HEIGHT, KEY_EMB_ELEMENT_TIMING_NATURAL_WIDTH, KEY_EMB_ELEMENT_TIMING_RENDER_TIME, KEY_EMB_ELEMENT_TIMING_START_TIME, KEY_EMB_ELEMENT_TIMING_URL } from "./constants.js";
//#region src/instrumentations/element-timing/ElementTimingInstrumentation/ElementTimingInstrumentation.ts
var ElementTimingInstrumentation = class extends EmbraceInstrumentationBase {
_observer = null;
constructor({ diag, perf, limitManager } = {}) {
super({
instrumentationName: "ElementTimingInstrumentation",
instrumentationVersion: "1.0.0",
diag,
perf,
limitManager,
config: {}
});
if (this._config.enabled) this.enable();
}
enable() {
if (isEntryTypeSupported("element")) super.enable();
else this._diag.debug("element not supported, skipping");
}
onEnable() {
if (this._observer) this._observer.disconnect();
this._observer = createPerformanceObserver("element", (entry) => this._processEntry(entry), { diag: this._diag });
if (!this._observer) {
this._isEnabled = false;
this._diag.error("failed to enable");
return;
}
}
onDisable() {
if (this._observer) {
this._observer.disconnect();
this._observer = null;
}
}
_processEntry(entry) {
if (!this._isEnabled) return;
if (this.limitManager?.limitElementTimingEntry()) return;
const span = this.tracer.startSpan(entry.identifier, {
startTime: this.perf.getZeroTime(),
attributes: {
[KEY_EMB_TYPE]: "ux.element_timing",
[KEY_EMB_ELEMENT_TIMING_IDENTIFIER]: entry.identifier,
[KEY_EMB_ELEMENT_TIMING_ELEMENT]: entry.element?.tagName?.toLowerCase(),
[KEY_EMB_ELEMENT_TIMING_RENDER_TIME]: this.perf.millisFromZeroTime(entry.renderTime),
[KEY_EMB_ELEMENT_TIMING_LOAD_TIME]: this.perf.millisFromZeroTime(entry.loadTime),
[KEY_EMB_ELEMENT_TIMING_START_TIME]: this.perf.millisFromZeroTime(entry.startTime),
[KEY_EMB_ELEMENT_TIMING_URL]: entry.url,
[KEY_EMB_ELEMENT_TIMING_NATURAL_WIDTH]: entry.naturalWidth,
[KEY_EMB_ELEMENT_TIMING_NATURAL_HEIGHT]: entry.naturalHeight
}
});
if (entry.loadTime > 0) span.addEvent("load", this.perf.epochMillisFromOrigin(entry.loadTime));
span.end(this.perf.epochMillisFromOrigin(entry.startTime));
}
};
//#endregion
export { ElementTimingInstrumentation };
//# sourceMappingURL=ElementTimingInstrumentation.js.map