Skip to content

Commit 3092d95

Browse files
committed
ref(sveltekit): Use isSubRequest flag to decide on server root span creation
1 parent ad9d6ab commit 3092d95

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/sveltekit/src/server/handle.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
149149
};
150150

151151
const sentryRequestHandler: Handle = input => {
152+
// event.isSubRequest was added in SvelteKit 1.21.0 and we can use it to check
153+
// if we should create a new execution context or not.
154+
// In case of a same-origin `fetch` call within a server`load` function,
155+
// SvelteKit will actually just re-enter the `handle` function and set `isSubRequest`
156+
// to `true` so that no additional network call is made.
157+
// We want the `http.server` span of that nested call to be a child span of the
158+
// currently active span instead of a new root span to correctly reflect this
159+
// behavior.
160+
// Type-casting to boolean | undefined to reflect that kit <1.21.0 doesn't have this property
161+
const isSubRequest = input.event.isSubRequest as boolean | undefined;
162+
if (isSubRequest) {
163+
return instrumentHandle(input, options);
164+
}
165+
if (isSubRequest === false) {
166+
return withIsolationScope(() => instrumentHandle(input, options));
167+
}
168+
169+
// Fallback for Sveltekit < 1.21.0
152170
// if there is an active span, we know that this handle call is nested and hence
153171
// we don't create a new execution context for it.
154172
// If we created one, nested server calls would create new root span instead

0 commit comments

Comments
 (0)