From e96b4afa557ec9785c92ce40de94d136c118b332 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 17 Jul 2024 08:24:15 +0000 Subject: [PATCH 1/2] . --- packages/browser-utils/src/metrics/browserMetrics.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/browser-utils/src/metrics/browserMetrics.ts b/packages/browser-utils/src/metrics/browserMetrics.ts index cb48c2e8b675..301a3b2022ae 100644 --- a/packages/browser-utils/src/metrics/browserMetrics.ts +++ b/packages/browser-utils/src/metrics/browserMetrics.ts @@ -287,7 +287,13 @@ export function addPerformanceEntries(span: Span): void { // eslint-disable-next-line @typescript-eslint/no-explicit-any performanceEntries.slice(_performanceCursor).forEach((entry: Record) => { const startTime = msToSec(entry.startTime); - const duration = msToSec(entry.duration); + const duration = msToSec( + // Inexplicibly, Chrome sometimes emits a negative duration. We need to work around this. + // There is a SO post attempting to explain this, but it leaves one with open questions: https://stackoverflow.com/questions/23191918/peformance-getentries-and-negative-duration-display + // The way we clamp the value is probably not accurate, since we have observed this happen for things that may take a while to load, like for example the replay worker. + // TODO: Investigate why this happens and how to properly mitigate. For now, this is a workaround to prevent transactions being dropped due to negative duration spans. + entry.duration < 0 ? 0 : entry.duration, + ); if (op === 'navigation' && transactionStartTime && timeOrigin + startTime < transactionStartTime) { return; From 90f86d3e425ad0b6ce3bbaa3f4f327792d77b28f Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 17 Jul 2024 08:35:26 +0000 Subject: [PATCH 2/2] math.max --- packages/browser-utils/src/metrics/browserMetrics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser-utils/src/metrics/browserMetrics.ts b/packages/browser-utils/src/metrics/browserMetrics.ts index 301a3b2022ae..02c044322bd3 100644 --- a/packages/browser-utils/src/metrics/browserMetrics.ts +++ b/packages/browser-utils/src/metrics/browserMetrics.ts @@ -292,7 +292,7 @@ export function addPerformanceEntries(span: Span): void { // There is a SO post attempting to explain this, but it leaves one with open questions: https://stackoverflow.com/questions/23191918/peformance-getentries-and-negative-duration-display // The way we clamp the value is probably not accurate, since we have observed this happen for things that may take a while to load, like for example the replay worker. // TODO: Investigate why this happens and how to properly mitigate. For now, this is a workaround to prevent transactions being dropped due to negative duration spans. - entry.duration < 0 ? 0 : entry.duration, + Math.max(0, entry.duration), ); if (op === 'navigation' && transactionStartTime && timeOrigin + startTime < transactionStartTime) {