@embrace-io/web-sdk
Version:
1 lines • 3.51 kB
Source Map (JSON)
{"version":3,"file":"OTelPerformanceManager.cjs","names":[],"sources":["../../../src/utils/PerformanceManager/OTelPerformanceManager.ts"],"sourcesContent":["import type { PerformanceClock, PerformanceManager } from './types.ts';\n\nlet _zeroTimeMillis: number | undefined;\n\nexport function updateZeroTimeMillis(epochMs: number): void {\n _zeroTimeMillis = epochMs;\n}\n\nexport function _resetZeroTimeMillisForTesting(): void {\n _zeroTimeMillis = undefined;\n}\n\nexport class OTelPerformanceManager implements PerformanceManager {\n private readonly _clock: PerformanceClock;\n private navigationEntry: PerformanceNavigationTiming | null = null;\n\n public constructor(clock: PerformanceClock = window.performance) {\n this._clock = clock;\n }\n\n // originOffset (entry.startTime, event.timeStamp, performance.now()) is always\n // \"milliseconds since timeOrigin\" by spec, a fixed relationship for the page's\n // whole life, so converting to an epoch timestamp is just adding timeOrigin\n // back in. getZeroTime() answers a different question (how long the user has\n // been looking at the current view) and has no bearing on this conversion.\n public epochMillisFromOrigin = (originOffset: number) =>\n this._clock.timeOrigin + originOffset;\n\n public getNowMillis = () => this.epochMillisFromOrigin(this._clock.now());\n\n /**\n * To measure the way a user experienced a metric, we measure metrics relative to the time the user\n * started viewing the current page or view. On prerendered pages, this is activationStart. On bfcache\n * restores and soft navigations, this is the time of the restore or navigation. On all other pages\n * this value will be zero.\n */\n public getZeroTime = (): number =>\n Math.max(\n this._clock.timeOrigin + this._getNavigationActivationStart(),\n _zeroTimeMillis ?? 0,\n );\n\n private _getNavigationEntry(): PerformanceNavigationTiming | null {\n if (this.navigationEntry) {\n return this.navigationEntry;\n }\n\n const [entry] = this._clock.getEntriesByType?.('navigation') ?? [];\n\n if (entry) {\n this.navigationEntry = entry as PerformanceNavigationTiming;\n\n return this.navigationEntry;\n }\n\n return null;\n }\n\n private _getNavigationActivationStart(): number {\n const entry = this._getNavigationEntry();\n return entry?.activationStart ?? 0;\n }\n\n // originOffset is relative to timeOrigin, but zero time may sit later than\n // timeOrigin (activation start, bfcache restore). Subtract that gap to rebase the\n // offset onto zero time; clamp to 0 for anything that predates zero time (e.g.\n // prerendering activity captured before activationStart).\n public millisFromZeroTime = (originOffset: number) =>\n Math.max(0, originOffset - (this.getZeroTime() - this._clock.timeOrigin));\n}\n"],"mappings":";;AAEA,IAAI;AAEJ,SAAgB,qBAAqB,SAAuB;CAC1D,kBAAkB;AACpB;AAEA,SAAgB,iCAAuC;CACrD,kBAAkB,KAAA;AACpB;AAEA,IAAa,yBAAb,MAAkE;CAChE;CACA,kBAA8D;CAE9D,YAAmB,QAA0B,OAAO,aAAa;EAC/D,KAAK,SAAS;CAChB;CAOA,yBAAgC,iBAC9B,KAAK,OAAO,aAAa;CAE3B,qBAA4B,KAAK,sBAAsB,KAAK,OAAO,IAAI,CAAC;;;;;;;CAQxE,oBACE,KAAK,IACH,KAAK,OAAO,aAAa,KAAK,8BAA8B,GAC5D,mBAAmB,CACrB;CAEF,sBAAkE;EAChE,IAAI,KAAK,iBACP,OAAO,KAAK;EAGd,MAAM,CAAC,SAAS,KAAK,OAAO,mBAAmB,YAAY,KAAK,CAAC;EAEjE,IAAI,OAAO;GACT,KAAK,kBAAkB;GAEvB,OAAO,KAAK;EACd;EAEA,OAAO;CACT;CAEA,gCAAgD;EAE9C,OADc,KAAK,oBACR,CAAC,EAAE,mBAAmB;CACnC;CAMA,sBAA6B,iBAC3B,KAAK,IAAI,GAAG,gBAAgB,KAAK,YAAY,IAAI,KAAK,OAAO,WAAW;AAC5E"}