Skip to content

Commit 929f39a

Browse files
committed
ref(sveltekit): Update trace propagation & span options
1 parent 20ab586 commit 929f39a

File tree

4 files changed

+72
-86
lines changed

4 files changed

+72
-86
lines changed

packages/sveltekit/src/client/load.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
9494
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
9595
},
9696
name: routeId ? routeId : event.url.pathname,
97-
status: 'ok',
9897
},
9998
() => handleCallbackErrors(() => wrappingTarget.apply(thisArg, [patchedEvent]), sendErrorToSentry),
10099
);

packages/sveltekit/src/server/handle.ts

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import {
22
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
4+
continueTrace,
45
getActiveSpan,
5-
getCurrentScope,
66
getDynamicSamplingContextFromSpan,
77
setHttpStatus,
88
spanToTraceHeader,
99
withIsolationScope,
1010
} from '@sentry/core';
1111
import { getActiveTransaction, startSpan } from '@sentry/core';
1212
import { captureException } from '@sentry/node-experimental';
13-
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
1413
import type { Span } from '@sentry/types';
1514
import { dynamicSamplingContextToSentryBaggageHeader, objectify } from '@sentry/utils';
1615
import type { Handle, ResolveOptions } from '@sveltejs/kit';
@@ -171,39 +170,35 @@ async function instrumentHandle(
171170
return resolve(event);
172171
}
173172

174-
const { dynamicSamplingContext, traceparentData, propagationContext } = getTracePropagationData(event);
175-
getCurrentScope().setPropagationContext(propagationContext);
176-
177-
try {
178-
const resolveResult = await startSpan(
179-
{
180-
op: 'http.server',
181-
attributes: {
182-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
183-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: event.route?.id ? 'route' : 'url',
173+
const { sentryTrace, baggage } = getTracePropagationData(event);
174+
175+
return continueTrace({ sentryTrace, baggage }, async () => {
176+
try {
177+
const resolveResult = await startSpan(
178+
{
179+
op: 'http.server',
180+
attributes: {
181+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit',
182+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: event.route?.id ? 'route' : 'url',
183+
},
184+
name: `${event.request.method} ${event.route?.id || event.url.pathname}`,
184185
},
185-
name: `${event.request.method} ${event.route?.id || event.url.pathname}`,
186-
status: 'ok',
187-
...traceparentData,
188-
metadata: {
189-
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
186+
async (span?: Span) => {
187+
const res = await resolve(event, {
188+
transformPageChunk: addSentryCodeToPage(options),
189+
});
190+
if (span) {
191+
setHttpStatus(span, res.status);
192+
}
193+
return res;
190194
},
191-
},
192-
async (span?: Span) => {
193-
const res = await resolve(event, {
194-
transformPageChunk: addSentryCodeToPage(options),
195-
});
196-
if (span) {
197-
setHttpStatus(span, res.status);
198-
}
199-
return res;
200-
},
201-
);
202-
return resolveResult;
203-
} catch (e: unknown) {
204-
sendErrorToSentry(e);
205-
throw e;
206-
} finally {
207-
await flushIfServerless();
208-
}
195+
);
196+
return resolveResult;
197+
} catch (e: unknown) {
198+
sendErrorToSentry(e);
199+
throw e;
200+
} finally {
201+
await flushIfServerless();
202+
}
203+
});
209204
}

packages/sveltekit/src/server/load.ts

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
21
import {
32
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
43
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5-
getCurrentScope,
4+
continueTrace,
65
startSpan,
76
} from '@sentry/core';
87
import { captureException } from '@sentry/node-experimental';
98
import { addNonEnumerableProperty, objectify } from '@sentry/utils';
109
import type { LoadEvent, ServerLoadEvent } from '@sveltejs/kit';
1110

12-
import type { TransactionContext } from '@sentry/types';
1311
import type { SentryWrappedFlag } from '../common/utils';
1412
import { isHttpError, isRedirect } from '../common/utils';
1513
import { flushIfServerless, getTracePropagationData } from './utils';
@@ -70,19 +68,19 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
7068

7169
const routeId = event.route && event.route.id;
7270

73-
const traceLoadContext: TransactionContext = {
74-
op: 'function.sveltekit.load',
75-
attributes: {
76-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
77-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
78-
},
79-
name: routeId ? routeId : event.url.pathname,
80-
status: 'ok',
81-
};
82-
8371
try {
8472
// We need to await before returning, otherwise we won't catch any errors thrown by the load function
85-
return await startSpan(traceLoadContext, () => wrappingTarget.apply(thisArg, args));
73+
return await startSpan(
74+
{
75+
op: 'function.sveltekit.load',
76+
attributes: {
77+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
78+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
79+
},
80+
name: routeId ? routeId : event.url.pathname,
81+
},
82+
() => wrappingTarget.apply(thisArg, args),
83+
);
8684
} catch (e) {
8785
sendErrorToSentry(e);
8886
throw e;
@@ -134,35 +132,30 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
134132
// https://github.com/sveltejs/kit/blob/e133aba479fa9ba0e7f9e71512f5f937f0247e2c/packages/kit/src/runtime/server/page/load_data.js#L111C3-L124
135133
const routeId = event.route && (Object.getOwnPropertyDescriptor(event.route, 'id')?.value as string | undefined);
136134

137-
const { dynamicSamplingContext, traceparentData, propagationContext } = getTracePropagationData(event);
138-
getCurrentScope().setPropagationContext(propagationContext);
139-
140-
const traceLoadContext: TransactionContext = {
141-
op: 'function.sveltekit.server.load',
142-
attributes: {
143-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
144-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
145-
},
146-
name: routeId ? routeId : event.url.pathname,
147-
status: 'ok',
148-
metadata: {
149-
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
150-
},
151-
data: {
152-
'http.method': event.request.method,
153-
},
154-
...traceparentData,
155-
};
156-
157-
try {
158-
// We need to await before returning, otherwise we won't catch any errors thrown by the load function
159-
return await startSpan(traceLoadContext, () => wrappingTarget.apply(thisArg, args));
160-
} catch (e: unknown) {
161-
sendErrorToSentry(e);
162-
throw e;
163-
} finally {
164-
await flushIfServerless();
165-
}
135+
const { sentryTrace, baggage } = getTracePropagationData(event);
136+
137+
return continueTrace({ sentryTrace, baggage }, async () => {
138+
try {
139+
// We need to await before returning, otherwise we won't catch any errors thrown by the load function
140+
return await startSpan(
141+
{
142+
op: 'function.sveltekit.server.load',
143+
attributes: {
144+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
145+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url',
146+
'http.method': event.request.method,
147+
},
148+
name: routeId ? routeId : event.url.pathname,
149+
},
150+
() => wrappingTarget.apply(thisArg, args),
151+
);
152+
} catch (e: unknown) {
153+
sendErrorToSentry(e);
154+
throw e;
155+
} finally {
156+
await flushIfServerless();
157+
}
158+
});
166159
},
167160
});
168161
}

packages/sveltekit/src/server/utils.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { flush } from '@sentry/node-experimental';
2-
import { logger, tracingContextFromHeaders } from '@sentry/utils';
2+
import { logger } from '@sentry/utils';
33
import type { RequestEvent } from '@sveltejs/kit';
44

55
import { DEBUG_BUILD } from '../common/debug-build';
@@ -10,12 +10,11 @@ import { DEBUG_BUILD } from '../common/debug-build';
1010
*
1111
* Sets propagation context as a side effect.
1212
*/
13-
// eslint-disable-next-line deprecation/deprecation
14-
export function getTracePropagationData(event: RequestEvent): ReturnType<typeof tracingContextFromHeaders> {
15-
const sentryTraceHeader = event.request.headers.get('sentry-trace') || '';
16-
const baggageHeader = event.request.headers.get('baggage');
17-
// eslint-disable-next-line deprecation/deprecation
18-
return tracingContextFromHeaders(sentryTraceHeader, baggageHeader);
13+
export function getTracePropagationData(event: RequestEvent): { sentryTrace: string; baggage: string | null } {
14+
const sentryTrace = event.request.headers.get('sentry-trace') || '';
15+
const baggage = event.request.headers.get('baggage');
16+
17+
return { sentryTrace, baggage };
1918
}
2019

2120
/** Flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda ends */

0 commit comments

Comments
 (0)