|
1 | 1 | /* eslint-disable @sentry-internal/sdk/no-optional-chaining */ |
2 | 2 | import { trace } from '@sentry/core'; |
3 | 3 | import { captureException } from '@sentry/node'; |
4 | | -import type { TransactionContext } from '@sentry/types'; |
| 4 | +import type { DynamicSamplingContext, TraceparentData, TransactionContext } from '@sentry/types'; |
5 | 5 | import { |
6 | 6 | addExceptionMechanism, |
7 | 7 | baggageHeaderToDynamicSamplingContext, |
8 | 8 | extractTraceparentData, |
9 | 9 | objectify, |
10 | 10 | } from '@sentry/utils'; |
11 | 11 | import type { HttpError, Load, LoadEvent, ServerLoad, ServerLoadEvent } from '@sveltejs/kit'; |
12 | | -import * as domain from 'domain'; |
13 | 12 |
|
14 | 13 | function isHttpError(err: unknown): err is HttpError { |
15 | 14 | return typeof err === 'object' && err !== null && 'status' in err && 'body' in err; |
@@ -53,42 +52,41 @@ function sendErrorToSentry(e: unknown): unknown { |
53 | 52 | export function wrapLoadWithSentry<T extends Load | ServerLoad>(origLoad: T): T { |
54 | 53 | return new Proxy(origLoad, { |
55 | 54 | apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad | Load>) => { |
56 | | - return domain.create().bind(() => { |
57 | | - const [event] = args; |
58 | | - const routeId = event.route && event.route.id; |
| 55 | + const [event] = args; |
| 56 | + const routeId = event.route && event.route.id; |
59 | 57 |
|
60 | | - const traceSharedLoadContext: TransactionContext = { |
61 | | - op: 'function.sveltekit.load', |
62 | | - name: routeId ? routeId : event.url.pathname, |
63 | | - status: 'ok', |
64 | | - metadata: { |
65 | | - source: routeId ? 'route' : 'url', |
66 | | - }, |
67 | | - }; |
| 58 | + const { traceparentData, dynamicSamplingContext } = getTracePropagationData(event); |
68 | 59 |
|
69 | | - let finalTraceLoadContext = { ...traceSharedLoadContext }; |
| 60 | + const traceLoadContext: TransactionContext = { |
| 61 | + op: 'function.sveltekit.load', |
| 62 | + name: routeId ? routeId : event.url.pathname, |
| 63 | + status: 'ok', |
| 64 | + metadata: { |
| 65 | + source: routeId ? 'route' : 'url', |
| 66 | + dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
| 67 | + }, |
| 68 | + ...traceparentData, |
| 69 | + }; |
70 | 70 |
|
71 | | - if (isServerOnlyLoad(event)) { |
72 | | - const sentryTraceHeader = event.request.headers.get('sentry-trace'); |
73 | | - const baggageHeader = event.request.headers.get('baggage'); |
74 | | - const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined; |
75 | | - const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader); |
| 71 | + return trace(traceLoadContext, () => wrappingTarget.apply(thisArg, args), sendErrorToSentry); |
| 72 | + }, |
| 73 | + }); |
| 74 | +} |
76 | 75 |
|
77 | | - const traceSeverOnlyLoadContext = { |
78 | | - ...traceparentData, |
79 | | - metadata: { |
80 | | - ...traceSharedLoadContext.metadata, |
81 | | - dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
82 | | - }, |
83 | | - }; |
| 76 | +function getTracePropagationData(event: ServerLoadEvent | LoadEvent): { |
| 77 | + traceparentData?: TraceparentData; |
| 78 | + dynamicSamplingContext?: Partial<DynamicSamplingContext>; |
| 79 | +} { |
| 80 | + if (!isServerOnlyLoad(event)) { |
| 81 | + return {}; |
| 82 | + } |
84 | 83 |
|
85 | | - finalTraceLoadContext = { ...traceSharedLoadContext, ...traceSeverOnlyLoadContext }; |
86 | | - } |
| 84 | + const sentryTraceHeader = event.request.headers.get('sentry-trace'); |
| 85 | + const baggageHeader = event.request.headers.get('baggage'); |
| 86 | + const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined; |
| 87 | + const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader); |
87 | 88 |
|
88 | | - return trace(finalTraceLoadContext, () => wrappingTarget.apply(thisArg, args), sendErrorToSentry); |
89 | | - })(); |
90 | | - }, |
91 | | - }); |
| 89 | + return { traceparentData, dynamicSamplingContext }; |
92 | 90 | } |
93 | 91 |
|
94 | 92 | /** |
|
0 commit comments