Skip to content

feat(sveltekit): Add request data to server-side events #12254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ test.describe('server-side errors', () => {
);

expect(errorEvent.tags).toMatchObject({ runtime: 'node' });

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/universal-load-error',
});
});

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

expect(errorEvent.tags).toMatchObject({ runtime: 'node' });

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-load-error',
});
});

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

expect(errorEvent.transaction).toEqual('GET /server-route-error');

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-route-error',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ test('server pageload request span has nested request span for sub request', asy
expect.objectContaining({ op: 'http.server', description: 'GET /api/users' }),
]),
);

expect(serverTxnEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-load-fetch',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ test.describe('server-side errors', () => {
);

expect(errorEvent.tags).toMatchObject({ runtime: 'node' });

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/universal-load-error',
});
});

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

expect(errorEvent.tags).toMatchObject({ runtime: 'node' });

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-load-error',
});
});

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

expect(errorEvent.transaction).toEqual('GET /server-route-error');

expect(errorEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-route-error',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ test('server pageload request span has nested request span for sub request', asy
expect.objectContaining({ op: 'http.server', description: 'GET /api/users' }),
]),
);

expect(serverTxnEvent.request).toEqual({
cookies: {},
headers: expect.objectContaining({
accept: expect.any(String),
'user-agent': expect.any(String),
}),
method: 'GET',
url: 'http://localhost:3030/server-load-fetch',
});
});
12 changes: 10 additions & 2 deletions packages/sveltekit/src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
getActiveSpan,
getCurrentScope,
getDefaultIsolationScope,
getIsolationScope,
getRootSpan,
Expand All @@ -12,7 +13,12 @@ import {
import { startSpan } from '@sentry/core';
import { captureException, continueTrace } from '@sentry/node';
import type { Span } from '@sentry/types';
import { dynamicSamplingContextToSentryBaggageHeader, logger, objectify } from '@sentry/utils';
import {
dynamicSamplingContextToSentryBaggageHeader,
logger,
objectify,
winterCGRequestToRequestData,
} from '@sentry/utils';
import type { Handle, ResolveOptions } from '@sveltejs/kit';

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

return withIsolationScope(() => {
return withIsolationScope(isolationScope => {
// We only call continueTrace in the initial top level request to avoid
// creating a new root span for the sub request.
isolationScope.setSDKProcessingMetadata({ request: winterCGRequestToRequestData(input.event.request.clone()) });
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
});
};
Expand Down Expand Up @@ -206,6 +213,7 @@ async function instrumentHandle(
name: routeName,
},
async (span?: Span) => {
getCurrentScope().setSDKProcessingMetadata({ request: winterCGRequestToRequestData(event.request.clone()) });
const res = await resolve(event, {
transformPageChunk: addSentryCodeToPage(options),
});
Expand Down
2 changes: 2 additions & 0 deletions packages/sveltekit/test/server/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function mockEvent(override: Record<string, unknown> = {}): Parameters<Handle>[0
...override,
};

event.request.clone = () => event.request;

return event;
}

Expand Down
Loading