Skip to content

Commit 76854e8

Browse files
authored
ref(sveltekit): Clean up sub-request check (#15251)
Removes a no longer necessary fallback check that we only needed in SvelteKit 1.26.0 or older. For Kit 2.x, we can rely on the `event.isSubRequest` flag to identify sub vs. actual requests in our request handler. fixes #15244
1 parent bb7237a commit 76854e8

File tree

2 files changed

+2
-56
lines changed

2 files changed

+2
-56
lines changed

packages/sveltekit/src/server/handle.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
44
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
55
continueTrace,
6-
getActiveSpan,
76
getCurrentScope,
87
getDefaultIsolationScope,
98
getIsolationScope,
@@ -100,19 +99,13 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
10099
};
101100

102101
const sentryRequestHandler: Handle = input => {
103-
// event.isSubRequest was added in SvelteKit 1.21.0 and we can use it to check
104-
// if we should create a new execution context or not.
105102
// In case of a same-origin `fetch` call within a server`load` function,
106103
// SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
107104
// to `true` so that no additional network call is made.
108105
// We want the `http.server` span of that nested call to be a child span of the
109106
// currently active span instead of a new root span to correctly reflect this
110107
// behavior.
111-
// As a fallback for Kit < 1.21.0, we check if there is an active span only if there's none,
112-
// we create a new execution context.
113-
const isSubRequest = typeof input.event.isSubRequest === 'boolean' ? input.event.isSubRequest : !!getActiveSpan();
114-
115-
if (isSubRequest) {
108+
if (input.event.isSubRequest) {
116109
return instrumentHandle(input, options);
117110
}
118111

packages/sveltekit/test/server/handle.test.ts

+1-48
Original file line numberDiff line numberDiff line change
@@ -149,53 +149,6 @@ describe('sentryHandle', () => {
149149
expect(spans).toHaveLength(1);
150150
});
151151

152-
it('[kit>=1.21.0] creates a child span for nested server calls (i.e. if there is an active span)', async () => {
153-
let _span: Span | undefined = undefined;
154-
let txnCount = 0;
155-
client.on('spanEnd', span => {
156-
if (span === getRootSpan(span)) {
157-
_span = span;
158-
++txnCount;
159-
}
160-
});
161-
162-
try {
163-
await sentryHandle()({
164-
event: mockEvent(),
165-
resolve: async _ => {
166-
// simulating a nested load call:
167-
await sentryHandle()({
168-
event: mockEvent({ route: { id: 'api/users/details/[id]', isSubRequest: true } }),
169-
resolve: resolve(type, isError),
170-
});
171-
return mockResponse;
172-
},
173-
});
174-
} catch (e) {
175-
//
176-
}
177-
178-
expect(txnCount).toEqual(1);
179-
expect(_span!).toBeDefined();
180-
181-
expect(spanToJSON(_span!).description).toEqual('GET /users/[id]');
182-
expect(spanToJSON(_span!).op).toEqual('http.server');
183-
expect(spanToJSON(_span!).status).toEqual(isError ? 'internal_error' : 'ok');
184-
expect(spanToJSON(_span!).data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]).toEqual('route');
185-
186-
expect(spanToJSON(_span!).timestamp).toBeDefined();
187-
188-
const spans = getSpanDescendants(_span!).map(spanToJSON);
189-
190-
expect(spans).toHaveLength(2);
191-
expect(spans).toEqual(
192-
expect.arrayContaining([
193-
expect.objectContaining({ op: 'http.server', description: 'GET /users/[id]' }),
194-
expect.objectContaining({ op: 'http.server', description: 'GET api/users/details/[id]' }),
195-
]),
196-
);
197-
});
198-
199152
it('creates a child span for nested server calls (i.e. if there is an active span)', async () => {
200153
let _span: Span | undefined = undefined;
201154
let txnCount = 0;
@@ -212,7 +165,7 @@ describe('sentryHandle', () => {
212165
resolve: async _ => {
213166
// simulating a nested load call:
214167
await sentryHandle()({
215-
event: mockEvent({ route: { id: 'api/users/details/[id]' } }),
168+
event: mockEvent({ route: { id: 'api/users/details/[id]', isSubRequest: true } }),
216169
resolve: resolve(type, isError),
217170
});
218171
return mockResponse;

0 commit comments

Comments
 (0)