UNPKG

@embrace-io/web-sdk

Version:
149 lines (148 loc) 6.55 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_utils_generateWebVitalID = require("../../../utils/generateWebVitalID.cjs"); 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_loaf_LoafInstrumentation_constants = require("./constants.cjs"); let _opentelemetry_api_logs = require("@opentelemetry/api-logs"); //#region src/instrumentations/loaf/LoafInstrumentation/LoafInstrumentation.ts var LoafInstrumentation = class extends require_instrumentations_EmbraceInstrumentationBase_EmbraceInstrumentationBase.EmbraceInstrumentationBase { _observer = null; _isFirstEntry = true; _totalDuration = 0; _workDuration = 0; _styleLayoutDuration = 0; _count = 0; _longestDuration = 0; _longestDurationExcludingFirst = 0; _totalBlockingDuration = 0; _scriptSummaries = /* @__PURE__ */ new Map(); constructor({ diag, perf } = {}) { super({ instrumentationName: "LoafInstrumentation", instrumentationVersion: "1.0.0", diag, perf, config: {} }); if (this._config.enabled) this.enable(); } enable() { if (require_utils_performanceObserver_performanceObserver.isEntryTypeSupported("long-animation-frame")) super.enable(); else this._diag.debug("long-animation-frame not supported, skipping"); } onEnable() { if (this._observer) this._observer.disconnect(); this._observer = require_utils_performanceObserver_performanceObserver.createPerformanceObserver("long-animation-frame", (entry) => this._processEntry(entry), { diag: this._diag }); if (!this._observer) { this._isEnabled = false; this._diag.error("failed to enable"); return; } this.setSessionPartListeners({ end: () => { try { this._flushReport(); } catch (e) { this._diag.error("error flushing report", e); } } }); } onDisable() { this._resetAccumulators(); if (this._observer) { this._observer.disconnect(); this._observer = null; } } _processEntry(entry) { if (!this._isEnabled) return; this._count++; this._totalDuration += entry.duration; this._workDuration += entry.renderStart ? entry.renderStart - entry.startTime : entry.duration; if (entry.styleAndLayoutStart) this._styleLayoutDuration += Math.max(0, entry.startTime + entry.duration - entry.styleAndLayoutStart); this._longestDuration = Math.max(this._longestDuration, entry.duration); if (this._isFirstEntry && this._count === 1) this._isFirstEntry = false; else { this._longestDurationExcludingFirst = Math.max(this._longestDurationExcludingFirst, entry.duration); if (entry.firstUIEventTimestamp === 0) this._totalBlockingDuration += entry.blockingDuration; } try { for (const script of entry.scripts) { let url = script.sourceURL || "(inline)"; if (url.length > 2048) url = `${url.substring(0, require_instrumentations_loaf_LoafInstrumentation_constants.MAX_SCRIPT_URL_LENGTH)}...`; const existing = this._scriptSummaries.get(url); if (existing) { existing.totalDuration += script.duration; existing.styleAndLayoutDuration += script.forcedStyleAndLayoutDuration; existing.count++; } else this._scriptSummaries.set(url, { totalDuration: script.duration, styleAndLayoutDuration: script.forcedStyleAndLayoutDuration, count: 1 }); } } catch (e) { this._diag.error("error processing scripts for entry", e); } } _flushReport() { if (this._count === 0) return; let rating; if (this._totalBlockingDuration <= 200) rating = "good"; else if (this._totalBlockingDuration <= 600) rating = "needs-improvement"; else rating = "poor"; const attrs = { [require_constants_attributes.KEY_EMB_TYPE]: "ux.web_vital", ["browser.web_vital.id"]: require_utils_generateWebVitalID.generateWebVitalID(), ["browser.web_vital.name"]: "tbd", ["browser.web_vital.value"]: Math.round(this._totalBlockingDuration), ["browser.web_vital.rating"]: rating, [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_TOTAL_DURATION]: Math.round(this._totalDuration), [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_WORK_DURATION]: Math.round(this._workDuration), [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_STYLE_AND_LAYOUT_DURATION]: Math.round(this._styleLayoutDuration), [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_COUNT]: this._count, [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_LONGEST_DURATION]: Math.round(this._longestDuration), [require_instrumentations_loaf_LoafInstrumentation_constants.ATTR_TBD_LOAF_LONGEST_DURATION_EXCLUDING_FIRST]: Math.round(this._longestDurationExcludingFirst) }; try { this.logger.emit({ eventName: require_instrumentations_loaf_LoafInstrumentation_constants.LOAF_EVENT_NAME, severityNumber: _opentelemetry_api_logs.SeverityNumber.INFO, attributes: attrs }); } catch (e) { this._diag.error("error emitting loaf report", e); } try { if (this._scriptSummaries.size > 0) { const scripts = [...this._scriptSummaries].sort((a, b) => b[1].totalDuration - a[1].totalDuration).slice(0, 250).map(([url, script]) => [url, { total_duration: Math.round(script.totalDuration), style_and_layout_duration: Math.round(script.styleAndLayoutDuration), count: script.count }]); this.logger.emit({ eventName: require_instrumentations_loaf_LoafInstrumentation_constants.LOAF_SCRIPTS_EVENT_NAME, severityNumber: _opentelemetry_api_logs.SeverityNumber.INFO, body: JSON.stringify(Object.fromEntries(scripts)), attributes: { [require_constants_attributes.KEY_EMB_TYPE]: "ux.loaf_scripts" } }); } } catch (e) { this._diag.error("error emitting loaf script summary", e); } this._resetAccumulators(); } _resetAccumulators() { this._totalDuration = 0; this._workDuration = 0; this._styleLayoutDuration = 0; this._count = 0; this._longestDuration = 0; this._longestDurationExcludingFirst = 0; this._totalBlockingDuration = 0; this._scriptSummaries = /* @__PURE__ */ new Map(); } }; //#endregion exports.LoafInstrumentation = LoafInstrumentation; //# sourceMappingURL=LoafInstrumentation.cjs.map