Skip to content

Commit a3acd38

Browse files
committed
fix(browser): Avoid recording long task spans starting before their parent span
1 parent c2bae3e commit a3acd38

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/browser-utils/src/metrics/browserMetrics.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,32 @@ export function startTrackingWebVitals({ recordClsStandaloneSpans }: StartTracki
105105
*/
106106
export function startTrackingLongTasks(): void {
107107
addPerformanceInstrumentationHandler('longtask', ({ entries }) => {
108-
if (!getActiveSpan()) {
108+
const parent = getActiveSpan();
109+
if (!parent) {
109110
return;
110111
}
112+
113+
const { op: parentOp, start_timestamp: parentStartTimestamp } = spanToJSON(parent);
114+
111115
for (const entry of entries) {
112116
const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime);
113117
const duration = msToSec(entry.duration);
114118

115-
const span = startInactiveSpan({
119+
if (parentOp === 'navigation' && parentStartTimestamp && startTime < parentStartTimestamp) {
120+
// Skip adding a span if the long task started before the navigation started.
121+
// `startAndEndSpan` will otherwise adjust the parent's start time to the span's start
122+
// time, potentially skewing the duration of the actual navigation as reported via our
123+
// routing instrumentations
124+
continue;
125+
}
126+
127+
startAndEndSpan(parent, startTime, startTime + duration, {
116128
name: 'Main UI thread blocked',
117129
op: 'ui.long-task',
118-
startTime,
119130
attributes: {
120131
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
121132
},
122133
});
123-
if (span) {
124-
span.end(startTime + duration);
125-
}
126134
}
127135
});
128136
}

0 commit comments

Comments
 (0)