Skip to content

Commit 3180af4

Browse files
committed
feat(sveltekit): Update scope transactionName when pageload route name is updated
1 parent 3ba23db commit 3180af4

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
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/opentelemetry/test/propagator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('SentryPropagator', () => {
203203
spanId: '6e0c63257de34c92',
204204
sampled: true,
205205
dsc: {
206-
transaction: 'sampled-transaction',
206+
transaction: 'users/123',
207207
sampled: 'false',
208208
trace_id: 'dsc_trace_id',
209209
public_key: 'dsc_public_key',

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ describe('browserTracingIntegration', () => {
4040
...txnCtx,
4141
updateName: vi.fn(),
4242
setAttribute: vi.fn(),
43-
startChild: vi.fn().mockImplementation(ctx => {
44-
return { ...mockedRoutingSpan, ...ctx };
45-
}),
46-
setTag: vi.fn(),
4743
};
4844
return createdRootSpan;
4945
});
@@ -55,7 +51,6 @@ describe('browserTracingIntegration', () => {
5551
...txnCtx,
5652
updateName: vi.fn(),
5753
setAttribute: vi.fn(),
58-
setTag: vi.fn(),
5954
};
6055
return createdRootSpan;
6156
});
@@ -141,6 +136,29 @@ describe('browserTracingIntegration', () => {
141136
expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledTimes(0);
142137
});
143138

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+
144162
it("doesn't start a navigation span when `instrumentNavigation` is false", () => {
145163
const integration = browserTracingIntegration({
146164
instrumentNavigation: false,

0 commit comments

Comments
 (0)