Skip to content

Commit 580b8b5

Browse files
authored
ref(browser): Improve setting of propagation scope for navigation spans (#15108)
Extracted this out of #14955 Instead of registering a general hook & filtering there, we can just use the `beforeSpanEnd` hook that we already have/use to do this instead.
1 parent 098f205 commit 580b8b5

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

Diff for: packages/browser/src/tracing/browserTracingIntegration.ts

+13-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
getDynamicSamplingContextFromSpan,
2525
getIsolationScope,
2626
getLocationHref,
27-
getRootSpan,
2827
logger,
2928
propagationContextFromHeaders,
3029
registerSpanErrorInstrumentation,
@@ -276,6 +275,19 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
276275
_collectWebVitals();
277276
addPerformanceEntries(span, { recordClsOnPageloadSpan: !enableStandaloneClsSpans });
278277
setActiveIdleSpan(client, undefined);
278+
279+
// A trace should stay consistent over the entire timespan of one route - even after the pageload/navigation ended.
280+
// Only when another navigation happens, we want to create a new trace.
281+
// This way, e.g. errors that occur after the pageload span ended are still associated to the pageload trace.
282+
const scope = getCurrentScope();
283+
const oldPropagationContext = scope.getPropagationContext();
284+
285+
scope.setPropagationContext({
286+
...oldPropagationContext,
287+
traceId: idleSpan.spanContext().traceId,
288+
sampled: spanIsSampled(idleSpan),
289+
dsc: getDynamicSamplingContextFromSpan(span),
290+
});
279291
},
280292
});
281293
setActiveIdleSpan(client, idleSpan);
@@ -344,27 +356,6 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
344356
});
345357
});
346358

347-
// A trace should to stay the consistent over the entire time span of one route.
348-
// Therefore, when the initial pageload or navigation root span ends, we update the
349-
// scope's propagation context to keep span-specific attributes like the `sampled` decision and
350-
// the dynamic sampling context valid, even after the root span has ended.
351-
// This ensures that the trace data is consistent for the entire duration of the route.
352-
client.on('spanEnd', span => {
353-
const op = spanToJSON(span).op;
354-
if (span !== getRootSpan(span) || (op !== 'navigation' && op !== 'pageload')) {
355-
return;
356-
}
357-
358-
const scope = getCurrentScope();
359-
const oldPropagationContext = scope.getPropagationContext();
360-
361-
scope.setPropagationContext({
362-
...oldPropagationContext,
363-
sampled: oldPropagationContext.sampled !== undefined ? oldPropagationContext.sampled : spanIsSampled(span),
364-
dsc: oldPropagationContext.dsc || getDynamicSamplingContextFromSpan(span),
365-
});
366-
});
367-
368359
if (WINDOW.location) {
369360
if (instrumentPageLoad) {
370361
const origin = browserPerformanceTimeOrigin();

0 commit comments

Comments
 (0)