Skip to content

Commit 4fc17db

Browse files
committed
add unit test
1 parent d493702 commit 4fc17db

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,24 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
284284
});
285285
});
286286

287+
// A trace should to stay the consistent over the entire time span of one route.
288+
// Therefore, when the initial pageload or navigation transaction ends, we update the
289+
// scope's propagation context to keep span-specific attributes like the `sampled` decision and
290+
// the dynamic sampling context valid, even after the transaction has ended.
291+
// This ensures that the trace data is consistent for the entire duration of the route.
287292
client.on('spanEnd', span => {
288293
const op = spanToJSON(span).op;
289294
if (span !== getRootSpan(span) || (op !== 'navigation' && op !== 'pageload')) {
290295
return;
291296
}
292297

293298
const scope = getCurrentScope();
299+
const oldPropagationContext = scope.getPropagationContext();
294300

295-
// A trace should to stay the same for the entire time span of one route.
296-
// Therefore, when the initial pageload or navigation transaction is ended, we update the
297-
// scope's propagation context to keep span-specific attributes like the `sampled` decision and
298-
// the dynamic sampling context valid, even after the transaction has ended.
299-
// This ensures that the trace data is consistent for the entire duration of the route.
300301
const newPropagationContext = {
301-
...scope.getPropagationContext(),
302-
// it's okay to always set sampled: true/false here. If we have a span, it cannot be un-sampled.
303-
sampled: spanIsSampled(span),
304-
dsc: getDynamicSamplingContextFromSpan(span),
302+
...oldPropagationContext,
303+
sampled: oldPropagationContext.sampled !== undefined ? oldPropagationContext.sampled : spanIsSampled(span),
304+
dsc: oldPropagationContext.dsc || getDynamicSamplingContextFromSpan(span),
305305
};
306306

307307
scope.setPropagationContext(newPropagationContext);

packages/browser/test/unit/tracing/browserTracingIntegration.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ describe('browserTracingIntegration', () => {
638638
expect(getCurrentScope().getScopeData().transactionName).toBe('test navigation span');
639639
});
640640

641-
it("resets the scopes' propagationContexts", () => {
641+
it("updates the scopes' propagationContexts on a navigation", () => {
642642
const client = new BrowserClient(
643643
getDefaultBrowserClientOptions({
644644
integrations: [browserTracingIntegration()],
@@ -675,6 +675,45 @@ describe('browserTracingIntegration', () => {
675675
expect(newIsolationScopePropCtx?.traceId).not.toEqual(oldIsolationScopePropCtx?.traceId);
676676
expect(newCurrentScopePropCtx?.traceId).not.toEqual(oldCurrentScopePropCtx?.traceId);
677677
});
678+
679+
it("saves the span's sampling decision and its DSC on the propagationContext when the span finishes", () => {
680+
const client = new BrowserClient(
681+
getDefaultBrowserClientOptions({
682+
tracesSampleRate: 1,
683+
integrations: [browserTracingIntegration({ instrumentPageLoad: false })],
684+
}),
685+
);
686+
setCurrentClient(client);
687+
client.init();
688+
689+
const navigationSpan = startBrowserTracingNavigationSpan(client, {
690+
name: 'mySpan',
691+
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route' },
692+
});
693+
694+
const propCtxBeforeEnd = getCurrentScope().getPropagationContext();
695+
expect(propCtxBeforeEnd).toStrictEqual({
696+
spanId: expect.stringMatching(/[a-f0-9]{16}/),
697+
traceId: expect.stringMatching(/[a-f0-9]{32}/),
698+
});
699+
700+
navigationSpan!.end();
701+
702+
const propCtxAfterEnd = getCurrentScope().getPropagationContext();
703+
expect(propCtxAfterEnd).toStrictEqual({
704+
spanId: propCtxBeforeEnd?.spanId,
705+
traceId: propCtxBeforeEnd?.traceId,
706+
sampled: true,
707+
dsc: {
708+
environment: 'production',
709+
public_key: 'examplePublicKey',
710+
sample_rate: '1',
711+
sampled: 'true',
712+
transaction: 'mySpan',
713+
trace_id: propCtxBeforeEnd?.traceId,
714+
},
715+
});
716+
});
678717
});
679718

680719
describe('using the <meta> tag data', () => {

0 commit comments

Comments
 (0)