Skip to content

Commit 34b3597

Browse files
authored
fix(browser): Always start navigation as root span (#17648)
fixes #17450
1 parent a271c6d commit 34b3597

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
tracesSampleRate: 1,
8+
integrations: [
9+
Sentry.browserTracingIntegration({
10+
_experiments: { enableInteractions: true },
11+
}),
12+
],
13+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Clicking the navigate button will push a new history state, triggering navigation
2+
document.querySelector('[data-test-id=navigate-button]').addEventListener('click', () => {
3+
const loc = window.location;
4+
const url = loc.href.includes('#nav') ? loc.pathname : `${loc.pathname}#nav`;
5+
6+
history.pushState({}, '', url);
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button data-test-id="navigate-button">Navigate</button>
8+
</body>
9+
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
4+
5+
sentryTest(
6+
'click-triggered navigation should produce a root navigation transaction',
7+
async ({ getLocalTestUrl, page }) => {
8+
if (shouldSkipTracingTest()) {
9+
sentryTest.skip();
10+
}
11+
12+
const url = await getLocalTestUrl({ testDir: __dirname });
13+
14+
await page.goto(url);
15+
await waitForTransactionRequest(page); // "pageload" root span
16+
17+
const interactionRequestPromise = waitForTransactionRequest(
18+
page,
19+
evt => evt.contexts?.trace?.op === 'ui.action.click',
20+
);
21+
const navigationRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'navigation');
22+
23+
await page.locator('[data-test-id=navigate-button]').click();
24+
25+
const interactionEvent = envelopeRequestParser(await interactionRequestPromise);
26+
const navigationEvent = envelopeRequestParser(await navigationRequestPromise);
27+
28+
// Navigation is root span, not a child span on the interaction
29+
expect(interactionEvent.contexts?.trace?.op).toBe('ui.action.click');
30+
expect(navigationEvent.contexts?.trace?.op).toBe('navigation');
31+
32+
expect(interactionEvent.contexts?.trace?.trace_id).not.toEqual(navigationEvent.contexts?.trace?.trace_id);
33+
34+
// does not contain a child navigation span
35+
const interactionSpans = interactionEvent.spans || [];
36+
const hasNavigationChild = interactionSpans.some(span => span.op === 'navigation' || span.op === 'http.server');
37+
expect(hasNavigationChild).toBeFalsy();
38+
},
39+
);

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
536536
_createRouteSpan(client, {
537537
op: 'navigation',
538538
...startSpanOptions,
539+
// Navigation starts a new trace and is NOT parented under any active interaction (e.g. ui.action.click)
540+
parentSpan: null,
541+
forceTransaction: true,
539542
});
540543
});
541544

0 commit comments

Comments
 (0)