Skip to content

Commit 09af745

Browse files
authored
feat(sveltekit): Add request data to server-side events (#12254)
This PR adds request data to server-side Sentry events by extracting the request object in our `sentryHandle` request handler. This is in line with our other SDKs and was simply missing from the SvelteKit SDK so far.
1 parent cd97287 commit 09af745

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ test.describe('server-side errors', () => {
2020
);
2121

2222
expect(errorEvent.tags).toMatchObject({ runtime: 'node' });
23+
24+
expect(errorEvent.request).toEqual({
25+
cookies: {},
26+
headers: expect.objectContaining({
27+
accept: expect.any(String),
28+
'user-agent': expect.any(String),
29+
}),
30+
method: 'GET',
31+
url: 'http://localhost:3030/universal-load-error',
32+
});
2333
});
2434

2535
test('captures server load error', async ({ page }) => {
@@ -40,6 +50,16 @@ test.describe('server-side errors', () => {
4050
);
4151

4252
expect(errorEvent.tags).toMatchObject({ runtime: 'node' });
53+
54+
expect(errorEvent.request).toEqual({
55+
cookies: {},
56+
headers: expect.objectContaining({
57+
accept: expect.any(String),
58+
'user-agent': expect.any(String),
59+
}),
60+
method: 'GET',
61+
url: 'http://localhost:3030/server-load-error',
62+
});
4363
});
4464

4565
test('captures server route (GET) error', async ({ page }) => {
@@ -61,5 +81,14 @@ test.describe('server-side errors', () => {
6181
);
6282

6383
expect(errorEvent.transaction).toEqual('GET /server-route-error');
84+
85+
expect(errorEvent.request).toEqual({
86+
cookies: {},
87+
headers: expect.objectContaining({
88+
accept: expect.any(String),
89+
}),
90+
method: 'GET',
91+
url: 'http://localhost:3030/server-route-error',
92+
});
6493
});
6594
});

dev-packages/e2e-tests/test-applications/sveltekit-2-svelte-5/test/performance.server.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ test('server pageload request span has nested request span for sub request', asy
3232
expect.objectContaining({ op: 'http.server', description: 'GET /api/users' }),
3333
]),
3434
);
35+
36+
expect(serverTxnEvent.request).toEqual({
37+
cookies: {},
38+
headers: expect.objectContaining({
39+
accept: expect.any(String),
40+
'user-agent': expect.any(String),
41+
}),
42+
method: 'GET',
43+
url: 'http://localhost:3030/server-load-fetch',
44+
});
3545
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ test.describe('server-side errors', () => {
2121
);
2222

2323
expect(errorEvent.tags).toMatchObject({ runtime: 'node' });
24+
25+
expect(errorEvent.request).toEqual({
26+
cookies: {},
27+
headers: expect.objectContaining({
28+
accept: expect.any(String),
29+
'user-agent': expect.any(String),
30+
}),
31+
method: 'GET',
32+
url: 'http://localhost:3030/universal-load-error',
33+
});
2434
});
2535

2636
test('captures server load error', async ({ page }) => {
@@ -42,6 +52,16 @@ test.describe('server-side errors', () => {
4252
);
4353

4454
expect(errorEvent.tags).toMatchObject({ runtime: 'node' });
55+
56+
expect(errorEvent.request).toEqual({
57+
cookies: {},
58+
headers: expect.objectContaining({
59+
accept: expect.any(String),
60+
'user-agent': expect.any(String),
61+
}),
62+
method: 'GET',
63+
url: 'http://localhost:3030/server-load-error',
64+
});
4565
});
4666

4767
test('captures server route (GET) error', async ({ page }) => {
@@ -64,5 +84,14 @@ test.describe('server-side errors', () => {
6484
);
6585

6686
expect(errorEvent.transaction).toEqual('GET /server-route-error');
87+
88+
expect(errorEvent.request).toEqual({
89+
cookies: {},
90+
headers: expect.objectContaining({
91+
accept: expect.any(String),
92+
}),
93+
method: 'GET',
94+
url: 'http://localhost:3030/server-route-error',
95+
});
6796
});
6897
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ test('server pageload request span has nested request span for sub request', asy
3232
expect.objectContaining({ op: 'http.server', description: 'GET /api/users' }),
3333
]),
3434
);
35+
36+
expect(serverTxnEvent.request).toEqual({
37+
cookies: {},
38+
headers: expect.objectContaining({
39+
accept: expect.any(String),
40+
'user-agent': expect.any(String),
41+
}),
42+
method: 'GET',
43+
url: 'http://localhost:3030/server-load-fetch',
44+
});
3545
});

packages/sveltekit/src/server/handle.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
44
getActiveSpan,
5+
getCurrentScope,
56
getDefaultIsolationScope,
67
getIsolationScope,
78
getRootSpan,
@@ -12,7 +13,12 @@ import {
1213
import { startSpan } from '@sentry/core';
1314
import { captureException, continueTrace } from '@sentry/node';
1415
import type { Span } from '@sentry/types';
15-
import { dynamicSamplingContextToSentryBaggageHeader, logger, objectify } from '@sentry/utils';
16+
import {
17+
dynamicSamplingContextToSentryBaggageHeader,
18+
logger,
19+
objectify,
20+
winterCGRequestToRequestData,
21+
} from '@sentry/utils';
1622
import type { Handle, ResolveOptions } from '@sveltejs/kit';
1723

1824
import { getDynamicSamplingContextFromSpan } from '@sentry/opentelemetry';
@@ -168,9 +174,10 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
168174
return instrumentHandle(input, options);
169175
}
170176

171-
return withIsolationScope(() => {
177+
return withIsolationScope(isolationScope => {
172178
// We only call continueTrace in the initial top level request to avoid
173179
// creating a new root span for the sub request.
180+
isolationScope.setSDKProcessingMetadata({ request: winterCGRequestToRequestData(input.event.request.clone()) });
174181
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
175182
});
176183
};
@@ -206,6 +213,7 @@ async function instrumentHandle(
206213
name: routeName,
207214
},
208215
async (span?: Span) => {
216+
getCurrentScope().setSDKProcessingMetadata({ request: winterCGRequestToRequestData(event.request.clone()) });
209217
const res = await resolve(event, {
210218
transformPageChunk: addSentryCodeToPage(options),
211219
});

packages/sveltekit/test/server/handle.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function mockEvent(override: Record<string, unknown> = {}): Parameters<Handle>[0
4444
...override,
4545
};
4646

47+
event.request.clone = () => event.request;
48+
4749
return event;
4850
}
4951

0 commit comments

Comments
 (0)