Skip to content

Commit a132d5c

Browse files
authored
feat(sveltekit): Update scope transactionName when pageload route name is updated (#11406)
Our SvelteKit routing instrumentation updates the pageload rootspan name once we obtain the parameterized route id. This patch now also updates the scope's `transactionName` at this point.
1 parent cbe9336 commit a132d5c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

dev-packages/e2e-tests/test-applications/sveltekit-2/test/errors.client.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ test.describe('client-side errors', () => {
2727
);
2828

2929
expect(errorEvent.tags).toMatchObject({ runtime: 'browser' });
30+
31+
expect(errorEvent.transaction).toEqual('/client-error');
3032
});
3133

3234
test('captures universal load error', async ({ page }) => {
@@ -52,5 +54,6 @@ test.describe('client-side errors', () => {
5254
);
5355

5456
expect(errorEvent.tags).toMatchObject({ runtime: 'browser' });
57+
expect(errorEvent.transaction).toEqual('/universal-load-error');
5558
});
5659
});

packages/sveltekit/src/client/browserTracingIntegration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } fr
33
import {
44
WINDOW,
55
browserTracingIntegration as originalBrowserTracingIntegration,
6+
getCurrentScope,
67
startBrowserTracingNavigationSpan,
78
startBrowserTracingPageLoadSpan,
89
startInactiveSpan,
@@ -65,6 +66,7 @@ function _instrumentPageload(client: Client): void {
6566
if (routeId) {
6667
pageloadSpan.updateName(routeId);
6768
pageloadSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
69+
getCurrentScope().setTransactionName(routeId);
6870
}
6971
});
7072
}

packages/sveltekit/test/client/browserTracingIntegration.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ describe('browserTracingIntegration', () => {
4040
...txnCtx,
4141
updateName: vi.fn(),
4242
setAttribute: vi.fn(),
43-
setTag: vi.fn(),
4443
};
4544
return createdRootSpan;
4645
});
@@ -52,7 +51,6 @@ describe('browserTracingIntegration', () => {
5251
...txnCtx,
5352
updateName: vi.fn(),
5453
setAttribute: vi.fn(),
55-
setTag: vi.fn(),
5654
};
5755
return createdRootSpan;
5856
});
@@ -138,6 +136,29 @@ describe('browserTracingIntegration', () => {
138136
expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledTimes(0);
139137
});
140138

139+
it("updates the current scope's transactionName once it's resolved during pageload", () => {
140+
const scopeSetTransactionNameSpy = vi.fn();
141+
142+
// @ts-expect-error - only returning a partial scope here, that's fine
143+
vi.spyOn(SentrySvelte, 'getCurrentScope').mockImplementation(() => {
144+
return {
145+
setTransactionName: scopeSetTransactionNameSpy,
146+
};
147+
});
148+
149+
const integration = browserTracingIntegration();
150+
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
151+
integration.afterAllSetup(fakeClient);
152+
153+
// We emit an update to the `page` store to simulate the SvelteKit router lifecycle
154+
// @ts-expect-error - page is a writable but the types say it's just readable
155+
page.set({ route: { id: 'testRoute/:id' } });
156+
157+
// This should update the transaction name with the parameterized route:
158+
expect(scopeSetTransactionNameSpy).toHaveBeenCalledTimes(3);
159+
expect(scopeSetTransactionNameSpy).toHaveBeenLastCalledWith('testRoute/:id');
160+
});
161+
141162
it("doesn't start a navigation span when `instrumentNavigation` is false", () => {
142163
const integration = browserTracingIntegration({
143164
instrumentNavigation: false,
@@ -185,7 +206,6 @@ describe('browserTracingIntegration', () => {
185206
},
186207
});
187208

188-
// eslint-disable-next-line deprecation/deprecation
189209
expect(startInactiveSpanSpy).toHaveBeenCalledWith({
190210
op: 'ui.sveltekit.routing',
191211
name: 'SvelteKit Route Change',
@@ -248,7 +268,6 @@ describe('browserTracingIntegration', () => {
248268
},
249269
});
250270

251-
// eslint-disable-next-line deprecation/deprecation
252271
expect(startInactiveSpanSpy).toHaveBeenCalledWith({
253272
op: 'ui.sveltekit.routing',
254273
name: 'SvelteKit Route Change',

0 commit comments

Comments
 (0)