-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(sveltekit): Ensure sub requests are recorded as child spans of parent request #11130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
size-limit report 📦
|
a899d27
to
53b5dc0
Compare
isSubRequest
flag to decide on server root span creationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! one medium important concern about sveltekit 1 + otel http instrumentation
}, | ||
}); | ||
|
||
expect(spans).toHaveLength(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: I'd argue that this assertion is redundant since we already have the one checking for the two relevant spans below. Just pointing out because it may create unnecessary test failures in case there is ever gonna be additional spans because of instrumentation changes. Same for the sveltekit 2 test!
// behavior. | ||
// As a fallback for Kit < 1.21.0, we check if there is an active span only if there's none, | ||
// we create a new execution context. | ||
const isSubRequest = typeof input.event.isSubRequest === 'boolean' ? input.event.isSubRequest : !!getActiveSpan(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yo this is surprisingly convenient 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was really happy when I found sveltejs/kit#10170 😅
// We want the `http.server` span of that nested call to be a child span of the | ||
// currently active span instead of a new root span to correctly reflect this | ||
// behavior. | ||
// As a fallback for Kit < 1.21.0, we check if there is an active span only if there's none, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am worried about sveltekit 1 here because shouldn't there always be a span with the OTEL http instrumentation? Or was it that the http integration doesn't work in sveltekit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The http
integration doesn't seem to do anything in Kit :(
But you're raising a good point. I'll check what getActiveSpan
now returns going forward. i.e. if it returns something like a NonRecordingSpan
if there previously was no span. In that case, we need to rework the check a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked for Kit < 1.21.0 and we still return undefined
in this case. So the logic works correctly here. To make sure we cover this correctly in the future, I downgraded to kit 1.20.5 in our Kit 1.x E2E test version.
f4174fd
to
8d38097
Compare
…rent request (#11130) When switching the SvelteKit server side SDK to `@sentry/node` powered by Otel, the semantics behind `continueTrace` changed as outlined in #11199. TLDR: We previously called `continueTrace` in a nested way when dealing with sub-requests`*` in SvelteKit. In our old Node SDK, this did nothing; in the new SDK, this currently causes a new root span/transaction to be created for the sub request. This patch now ensures that we continue to send sub request spans as child spans of the top-level/parent request.
…rent request (#11130) When switching the SvelteKit server side SDK to `@sentry/node` powered by Otel, the semantics behind `continueTrace` changed as outlined in #11199. TLDR: We previously called `continueTrace` in a nested way when dealing with sub-requests`*` in SvelteKit. In our old Node SDK, this did nothing; in the new SDK, this currently causes a new root span/transaction to be created for the sub request. This patch now ensures that we continue to send sub request spans as child spans of the top-level/parent request.
…rent request (getsentry#11130) When switching the SvelteKit server side SDK to `@sentry/node` powered by Otel, the semantics behind `continueTrace` changed as outlined in getsentry#11199. TLDR: We previously called `continueTrace` in a nested way when dealing with sub-requests`*` in SvelteKit. In our old Node SDK, this did nothing; in the new SDK, this currently causes a new root span/transaction to be created for the sub request. This patch now ensures that we continue to send sub request spans as child spans of the top-level/parent request.
When switching the SvelteKit server side SDK to
@sentry/node
powered by Otel, the semantics behindcontinueTrace
changed as outlined in #11199. TLDR: We previously calledcontinueTrace
in a nested way when dealing with sub-requests*
in SvelteKit. In our old Node SDK, this did nothing; in the new SDK, this currently causes a new root span/transaction to be created for the sub request.This PR now ensures that we continue to send sub request spans as child spans of the top-level/parent request by making the following changes:
continueTrace
call in ourload
instrumentation. Server-side, load spans will never be root spans because they're always wrapped byhttp.server
spans from thesentryHandle
hook.continueTrace
if we're not in a sub requestisSubRequest
flag to our sub request check. This flag is available since kit 1.21.0 and we should leverage it whenever it's available. Otherwise, we fall back to checking for an active span as previously.*
What is a sub request?In SvelteKit, you can make
fetch
calls within a dataload
function that runs on the server (e.g. during pageload or a navigation). If such aload
function makes afetch
call to a same-origin server (API) route, SveteKit detects that it's not necessary to make another http request but basically redirects the fetch call to directly re-enter the server request handler. This new request is called a sub request.Example (from this Sentry event)

closes #11125
ref #11041