Skip to content

Commit c6f26b8

Browse files
Lms24cadesalaberry
authored andcommitted
feat(sveltekit): Update scope transactionName when handling server-side request (getsentry#11511)
Update the scope's `transactionName` in the `sentryHandle` instrumentation that's always invoked when a request is made to the SvelteKit server.
1 parent e0021b9 commit c6f26b8

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ test.describe('server-side errors', () => {
6060
}),
6161
);
6262

63-
// TODO: Uncomment once we update the scope transaction name on the server side
64-
// expect(errorEvent.transaction).toEqual('GET /server-route-error');
63+
expect(errorEvent.transaction).toEqual('GET /server-route-error');
6564
});
6665
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ test.describe('server-side errors', () => {
6363
}),
6464
);
6565

66-
// TODO: Uncomment once we update the scope transaction name on the server side
67-
// expect(errorEvent.transaction).toEqual('GET /server-route-error');
66+
expect(errorEvent.transaction).toEqual('GET /server-route-error');
6867
});
6968
});

packages/sveltekit/src/server/handle.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
44
getActiveSpan,
5+
getDefaultIsolationScope,
6+
getIsolationScope,
57
getRootSpan,
68
setHttpStatus,
79
spanToTraceHeader,
@@ -10,11 +12,12 @@ import {
1012
import { startSpan } from '@sentry/core';
1113
import { captureException, continueTrace } from '@sentry/node';
1214
import type { Span } from '@sentry/types';
13-
import { dynamicSamplingContextToSentryBaggageHeader, objectify } from '@sentry/utils';
15+
import { dynamicSamplingContextToSentryBaggageHeader, logger, objectify } from '@sentry/utils';
1416
import type { Handle, ResolveOptions } from '@sveltejs/kit';
1517

1618
import { getDynamicSamplingContextFromSpan } from '@sentry/opentelemetry';
1719

20+
import { DEBUG_BUILD } from '../common/debug-build';
1821
import { isHttpError, isRedirect } from '../common/utils';
1922
import { flushIfServerless, getTracePropagationData } from './utils';
2023

@@ -183,6 +186,14 @@ async function instrumentHandle(
183186
return resolve(event);
184187
}
185188

189+
const routeName = `${event.request.method} ${event.route?.id || event.url.pathname}`;
190+
191+
if (getIsolationScope() !== getDefaultIsolationScope()) {
192+
getIsolationScope().setTransactionName(routeName);
193+
} else {
194+
DEBUG_BUILD && logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
195+
}
196+
186197
try {
187198
const resolveResult = await startSpan(
188199
{
@@ -192,7 +203,7 @@ async function instrumentHandle(
192203
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: event.route?.id ? 'route' : 'url',
193204
'http.method': event.request.method,
194205
},
195-
name: `${event.request.method} ${event.route?.id || event.url.pathname}`,
206+
name: routeName,
196207
},
197208
async (span?: Span) => {
198209
const res = await resolve(event, {

0 commit comments

Comments
 (0)