|
1 |
| -/* eslint-disable @sentry-internal/sdk/no-optional-chaining */ |
2 | 1 | import {
|
3 | 2 | SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
|
4 | 3 | SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
|
5 |
| - getCurrentScope, |
| 4 | + continueTrace, |
6 | 5 | startSpan,
|
7 | 6 | } from '@sentry/core';
|
8 | 7 | import { captureException } from '@sentry/node-experimental';
|
9 | 8 | import { addNonEnumerableProperty, objectify } from '@sentry/utils';
|
10 | 9 | import type { LoadEvent, ServerLoadEvent } from '@sveltejs/kit';
|
11 | 10 |
|
12 |
| -import type { TransactionContext } from '@sentry/types'; |
13 | 11 | import type { SentryWrappedFlag } from '../common/utils';
|
14 | 12 | import { isHttpError, isRedirect } from '../common/utils';
|
15 | 13 | import { flushIfServerless, getTracePropagationData } from './utils';
|
@@ -70,18 +68,19 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
|
70 | 68 |
|
71 | 69 | const routeId = event.route && event.route.id;
|
72 | 70 |
|
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 |
| - }; |
81 |
| - |
82 | 71 | try {
|
83 | 72 | // We need to await before returning, otherwise we won't catch any errors thrown by the load function
|
84 |
| - 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 | + ); |
85 | 84 | } catch (e) {
|
86 | 85 | sendErrorToSentry(e);
|
87 | 86 | throw e;
|
@@ -133,34 +132,30 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
|
133 | 132 | // https://github.com/sveltejs/kit/blob/e133aba479fa9ba0e7f9e71512f5f937f0247e2c/packages/kit/src/runtime/server/page/load_data.js#L111C3-L124
|
134 | 133 | const routeId = event.route && (Object.getOwnPropertyDescriptor(event.route, 'id')?.value as string | undefined);
|
135 | 134 |
|
136 |
| - const { dynamicSamplingContext, traceparentData, propagationContext } = getTracePropagationData(event); |
137 |
| - getCurrentScope().setPropagationContext(propagationContext); |
138 |
| - |
139 |
| - const traceLoadContext: TransactionContext = { |
140 |
| - op: 'function.sveltekit.server.load', |
141 |
| - attributes: { |
142 |
| - [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit', |
143 |
| - [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: routeId ? 'route' : 'url', |
144 |
| - }, |
145 |
| - name: routeId ? routeId : event.url.pathname, |
146 |
| - metadata: { |
147 |
| - dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
148 |
| - }, |
149 |
| - data: { |
150 |
| - 'http.method': event.request.method, |
151 |
| - }, |
152 |
| - ...traceparentData, |
153 |
| - }; |
154 |
| - |
155 |
| - try { |
156 |
| - // We need to await before returning, otherwise we won't catch any errors thrown by the load function |
157 |
| - return await startSpan(traceLoadContext, () => wrappingTarget.apply(thisArg, args)); |
158 |
| - } catch (e: unknown) { |
159 |
| - sendErrorToSentry(e); |
160 |
| - throw e; |
161 |
| - } finally { |
162 |
| - await flushIfServerless(); |
163 |
| - } |
| 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 | + }); |
164 | 159 | },
|
165 | 160 | });
|
166 | 161 | }
|
0 commit comments