Skip to content

Commit 62dd95d

Browse files
committed
remove domain usage from load
1 parent 85b56dc commit 62dd95d

File tree

3 files changed

+82
-83
lines changed

3 files changed

+82
-83
lines changed

packages/core/src/tracing/trace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function trace<T>(
3131
const scope = hub.getScope();
3232

3333
const parentSpan = scope.getSpan();
34+
3435
const activeSpan = parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx);
3536
scope.setSpan(activeSpan);
3637

packages/sveltekit/src/server/load.ts

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
22
import { trace } from '@sentry/core';
33
import { captureException } from '@sentry/node';
4-
import type { TransactionContext } from '@sentry/types';
4+
import type { DynamicSamplingContext, TraceparentData, TransactionContext } from '@sentry/types';
55
import {
66
addExceptionMechanism,
77
baggageHeaderToDynamicSamplingContext,
88
extractTraceparentData,
99
objectify,
1010
} from '@sentry/utils';
1111
import type { HttpError, Load, LoadEvent, ServerLoad, ServerLoadEvent } from '@sveltejs/kit';
12-
import * as domain from 'domain';
1312

1413
function isHttpError(err: unknown): err is HttpError {
1514
return typeof err === 'object' && err !== null && 'status' in err && 'body' in err;
@@ -53,42 +52,41 @@ function sendErrorToSentry(e: unknown): unknown {
5352
export function wrapLoadWithSentry<T extends Load | ServerLoad>(origLoad: T): T {
5453
return new Proxy(origLoad, {
5554
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;
5957

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);
6859

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+
};
7070

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+
}
7675

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+
}
8483

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);
8788

88-
return trace(finalTraceLoadContext, () => wrappingTarget.apply(thisArg, args), sendErrorToSentry);
89-
})();
90-
},
91-
});
89+
return { traceparentData, dynamicSamplingContext };
9290
}
9391

9492
/**

packages/sveltekit/test/server/load.test.ts

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,57 @@ describe('wrapLoadWithSentry', () => {
136136
expect(mockCaptureException).toHaveBeenCalledTimes(1);
137137
});
138138

139+
describe('with error() helper', () => {
140+
it.each([
141+
// [statusCode, timesCalled]
142+
[400, 0],
143+
[401, 0],
144+
[403, 0],
145+
[404, 0],
146+
[409, 0],
147+
[429, 0],
148+
[499, 0],
149+
[500, 1],
150+
[501, 1],
151+
[503, 1],
152+
[504, 1],
153+
])('error with status code %s calls captureException %s times', async (code, times) => {
154+
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
155+
throw error(code, params.id);
156+
}
157+
158+
const wrappedLoad = wrapLoadWithSentry(load);
159+
const res = wrappedLoad(MOCK_LOAD_ARGS);
160+
await expect(res).rejects.toThrow();
161+
162+
expect(mockCaptureException).toHaveBeenCalledTimes(times);
163+
});
164+
});
165+
166+
it('adds an exception mechanism', async () => {
167+
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
168+
void callback({}, { event_id: 'fake-event-id' });
169+
return mockScope;
170+
});
171+
172+
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
173+
return {
174+
post: getById(params.id),
175+
};
176+
}
177+
178+
const wrappedLoad = wrapLoadWithSentry(load);
179+
const res = wrappedLoad(MOCK_LOAD_ARGS);
180+
await expect(res).rejects.toThrow();
181+
182+
expect(addEventProcessorSpy).toBeCalledTimes(1);
183+
expect(mockAddExceptionMechanism).toBeCalledTimes(1);
184+
expect(mockAddExceptionMechanism).toBeCalledWith(
185+
{},
186+
{ handled: false, type: 'sveltekit', data: { function: 'load' } },
187+
);
188+
});
189+
139190
describe('calls trace', () => {
140191
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
141192
return {
@@ -256,55 +307,4 @@ describe('wrapLoadWithSentry', () => {
256307
);
257308
});
258309
});
259-
260-
describe('with error() helper', () => {
261-
it.each([
262-
// [statusCode, timesCalled]
263-
[400, 0],
264-
[401, 0],
265-
[403, 0],
266-
[404, 0],
267-
[409, 0],
268-
[429, 0],
269-
[499, 0],
270-
[500, 1],
271-
[501, 1],
272-
[503, 1],
273-
[504, 1],
274-
])('error with status code %s calls captureException %s times', async (code, times) => {
275-
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
276-
throw error(code, params.id);
277-
}
278-
279-
const wrappedLoad = wrapLoadWithSentry(load);
280-
const res = wrappedLoad(MOCK_LOAD_ARGS);
281-
await expect(res).rejects.toThrow();
282-
283-
expect(mockCaptureException).toHaveBeenCalledTimes(times);
284-
});
285-
});
286-
287-
it('adds an exception mechanism', async () => {
288-
const addEventProcessorSpy = vi.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => {
289-
void callback({}, { event_id: 'fake-event-id' });
290-
return mockScope;
291-
});
292-
293-
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
294-
return {
295-
post: getById(params.id),
296-
};
297-
}
298-
299-
const wrappedLoad = wrapLoadWithSentry(load);
300-
const res = wrappedLoad(MOCK_LOAD_ARGS);
301-
await expect(res).rejects.toThrow();
302-
303-
expect(addEventProcessorSpy).toBeCalledTimes(1);
304-
expect(mockAddExceptionMechanism).toBeCalledTimes(1);
305-
expect(mockAddExceptionMechanism).toBeCalledWith(
306-
{},
307-
{ handled: false, type: 'sveltekit', data: { function: 'load' } },
308-
);
309-
});
310310
});

0 commit comments

Comments
 (0)