Skip to content

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

Merged
merged 11 commits into from
Mar 22, 2024

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Mar 15, 2024

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 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:

  • Removed the unnecessary continueTrace call in our load instrumentation. Server-side, load spans will never be root spans because they're always wrapped by http.server spans from the sentryHandle hook.
  • We now only call continueTrace if we're not in a sub request
  • We added the built-in isSubRequest 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 data load function that runs on the server (e.g. during pageload or a navigation). If such a load function makes a fetch 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)
image

closes #11125
ref #11041

@Lms24 Lms24 self-assigned this Mar 15, 2024
Copy link
Contributor

github-actions bot commented Mar 15, 2024

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) 80.9 KB (added)
@sentry/browser (incl. Tracing, Replay) 72.24 KB (added)
@sentry/browser (incl. Tracing, Replay with Canvas) 76.04 KB (added)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 65.81 KB (added)
@sentry/browser (incl. Tracing) 36.83 KB (added)
@sentry/browser (incl. browserTracingIntegration) 36.83 KB (added)
@sentry/browser (incl. feedbackIntegration) 31.35 KB (added)
@sentry/browser (incl. feedbackModalIntegration) 31.47 KB (added)
@sentry/browser (incl. feedbackScreenshotIntegration) 31.48 KB (added)
@sentry/browser (incl. sendFeedback) 27.43 KB (added)
@sentry/browser 22.6 KB (added)
CDN Bundle (incl. Tracing, Replay, Feedback) 75.21 KB (added)
CDN Bundle (incl. Tracing, Replay) 70.05 KB (added)
CDN Bundle (incl. Tracing) 36.41 KB (added)
CDN Bundle 23.97 KB (added)
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.04 KB (added)
CDN Bundle (incl. Tracing) - uncompressed 109.98 KB (added)
CDN Bundle - uncompressed 71 KB (added)
@sentry/react (incl. Tracing, Replay) 72.22 KB (added)
@sentry/react 22.63 KB (added)

@Lms24 Lms24 force-pushed the lms/ref-sveltekit-isSubRequest branch 2 times, most recently from a899d27 to 53b5dc0 Compare March 20, 2024 12:36
@Lms24 Lms24 changed the title ref(sveltekit): Use isSubRequest flag to decide on server root span creation fix(sveltekit): Ensure sub requests are recorded as child spans of parent request Mar 20, 2024
@Lms24 Lms24 requested review from a team, stephanie-anderson, s1gr1d and AbhiPrasad and removed request for a team March 20, 2024 12:44
@Lms24 Lms24 marked this pull request as ready for review March 20, 2024 12:44
@Lms24 Lms24 requested review from lforst and removed request for stephanie-anderson March 21, 2024 12:30
Copy link
Contributor

@lforst lforst left a 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);
Copy link
Contributor

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();
Copy link
Contributor

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 😂

Copy link
Member Author

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,
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

@Lms24 Lms24 force-pushed the lms/ref-sveltekit-isSubRequest branch from f4174fd to 8d38097 Compare March 22, 2024 10:58
@Lms24 Lms24 enabled auto-merge (squash) March 22, 2024 10:59
@Lms24 Lms24 merged commit 7c2f2b4 into develop Mar 22, 2024
@Lms24 Lms24 deleted the lms/ref-sveltekit-isSubRequest branch March 22, 2024 11:15
c298lee pushed a commit that referenced this pull request Mar 22, 2024
…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.
c298lee pushed a commit that referenced this pull request Mar 23, 2024
…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.
cadesalaberry pushed a commit to cadesalaberry/sentry-javascript that referenced this pull request Apr 19, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use new isSubRequest flag in sentryHandle to decide on new root span creation
2 participants