From e710f3bcdbe8b77fd24a4bb7e487fa8456286a68 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 17 Jul 2024 14:34:48 +0200 Subject: [PATCH] fix(browser): Fix bug causing unintentional dropping of transactions (#12933) --- 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..02c044322bd3 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. + Math.max(0, entry.duration), + ); if (op === 'navigation' && transactionStartTime && timeOrigin + startTime < transactionStartTime) { return;