Skip to content
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

fix(core): Use consistent continueTrace implementation in core #14813

Merged
merged 5 commits into from
Dec 20, 2024

Conversation

mydea
Copy link
Member

@mydea mydea commented Dec 20, 2024

We have a different implementation of continueTrace for OTEL/Node. Until now we relied on actually using the import from @sentry/node vs @sentry/core to ensure this. However, this is a footgun, and actually lead to a problem in NextJS because we used the core variant there. Also, it is simply not isomorphic.

So to fix this, this PR puts continueTrace on the ACS so we can consistently use the core variant in all environments.

Fixes #14787

@mydea mydea self-assigned this Dec 20, 2024
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

Copy link
Contributor

github-actions bot commented Dec 20, 2024

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.16 KB - -
@sentry/browser - with treeshaking flags 21.83 KB - -
@sentry/browser (incl. Tracing) 35.66 KB - -
@sentry/browser (incl. Tracing, Replay) 72.91 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 63.31 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 77.32 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 89.71 KB - -
@sentry/browser (incl. Feedback) 39.91 KB - -
@sentry/browser (incl. sendFeedback) 27.77 KB - -
@sentry/browser (incl. FeedbackAsync) 32.53 KB - -
@sentry/react 25.88 KB - -
@sentry/react (incl. Tracing) 38.51 KB -0.01% -1 B 🔽
@sentry/vue 27.38 KB - -
@sentry/vue (incl. Tracing) 37.51 KB - -
@sentry/svelte 23.31 KB - -
CDN Bundle 24.26 KB +0.09% +21 B 🔺
CDN Bundle (incl. Tracing) 35.81 KB +0.05% +15 B 🔺
CDN Bundle (incl. Tracing, Replay) 70.92 KB +0.03% +18 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 76.21 KB +0.03% +17 B 🔺
CDN Bundle - uncompressed 71.32 KB +0.12% +81 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 106.78 KB +0.08% +81 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.85 KB +0.04% +81 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 234.05 KB +0.04% +81 B 🔺
@sentry/nextjs (client) 38.79 KB - -
@sentry/sveltekit (client) 36.18 KB - -
@sentry/node 157.98 KB +0.02% +32 B 🔺
@sentry/node - without tracing 98.87 KB +0.04% +32 B 🔺
@sentry/aws-serverless 126.6 KB +0.03% +33 B 🔺

View base workflow run

Copy link

codecov bot commented Dec 20, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
689 1 688 298
View the top 1 failed tests by shortest run time
transactions.test.ts Should send a wrapped server action as a child of a nextjs transaction
Stack Traces | 0.239s run time
transactions.test.ts:79:5 Should send a wrapped server action as a child of a nextjs transaction

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

@mydea
Copy link
Member Author

mydea commented Dec 20, 2024

Hmm, so with this change next.js tests fail 🤔

This test: Should send a wrapped server action as a child of a nextjs transaction

Fails, because now the POST /server-action and the serverAction/myServerAction both have the same parent_span_id, instead of the server action being the child of the POST. Any idea why this is the case/if/why this is wrong/correct? 🤔 @lforst and @chargome

Adding some logs, I see:

NEXT SPAN IS HERE {
[WebServer]   span_id: '7cbbd9156b3c695e',
[WebServer]   trace_id: 'd59a8a7dd8014861b05937614d443e80',
[WebServer]   data: {
[WebServer]     'next.span_name': 'POST /server-action',
[WebServer]     'next.span_type': 'BaseServer.handleRequest',
[WebServer]     'http.method': 'POST',
[WebServer]     'http.target': '/server-action',
[WebServer]     'sentry.sample_rate': 1,
[WebServer]     'sentry.parentIsRemote': true
[WebServer]   },
[WebServer]   description: 'POST /server-action',
[WebServer]   parent_span_id: 'bd2a69529b385a7e',
[WebServer]   start_timestamp: 1734692699.014
[WebServer] }

[WebServer] {
[WebServer]   sentryTraceHeader: 'd59a8a7dd8014861b05937614d443e80-bd2a69529b385a7e-1',
[WebServer]   baggageHeader: 'sentry-environment=qa,sentry-release=fqG4zegKOdcAQm8W9PhP-,sentry-public_key=3b6c388182fb435097f41d181be2b2ba,sentry-trace_id=d59a8a7dd8014861b05937614d443e80,sentry-sample_rate=1,sentry-sampled=true'
[WebServer] }

[WebServer] {
[WebServer]   span_id: '5e6e2e489641d3b2',
[WebServer]   trace_id: 'd59a8a7dd8014861b05937614d443e80',
[WebServer]   data: {
[WebServer]     'sentry.op': 'function.server_action',
[WebServer]     'sentry.source': 'route',
[WebServer]     'sentry.sample_rate': 1,
[WebServer]     'sentry.parentIsRemote': true
[WebServer]   },
[WebServer]   description: 'serverAction/myServerAction',
[WebServer]   parent_span_id: 'bd2a69529b385a7e',
[WebServer]   start_timestamp: 1734692699.024,
[WebServer]   op: 'function.server_action'
[WebServer] }

Which shows:

  1. the POST span is started by Sentry
  2. Then, continueTrace gets the sentryTrace header, where the span ID is the parent of the POST span
  3. This is "correctly" continued then for the serverAction 🤔

This "seems" correct, but...

@lforst
Copy link
Member

lforst commented Dec 20, 2024

Right, so I guess the desired behavior is that if there is a POST server span we attach the server action span to that one. If there isn't we attach the server action span to the incoming parent span ID.

My first instinct was that we can probably remove the continueTrace call from the server action instrumentation, however some nextjs versions have problems with http instrumentation so we would probably end up with some of the server action spans being detached.

Maybe conditionally calling continueTrace when there is not an active and recording span is the way to go?

@andreiborza
Copy link
Member

I had the same issue in the solidstart sdk and we ended up just dropping continueTrace here https://github.com/getsentry/sentry-javascript/blob/develop/packages/solidstart/src/server/withServerActionInstrumentation.ts#L30.

But yea, it didn't come with a baggage of having to support older versions.

@mydea
Copy link
Member Author

mydea commented Dec 20, 2024

Ahh, that makes sense. I will fix this then, I have an idea (let's see if that works).

@mydea mydea force-pushed the fn/continueTraceACS branch from b3ce32e to 0b5edbd Compare December 20, 2024 12:41
@mydea mydea merged commit 65531f3 into develop Dec 20, 2024
150 of 151 checks passed
@mydea mydea deleted the fn/continueTraceACS branch December 20, 2024 13:25
s1gr1d pushed a commit that referenced this pull request Dec 20, 2024
…14813)

We have a different implementation of `continueTrace` for OTEL/Node.
Until now we relied on actually using the import from `@sentry/node` vs
`@sentry/core` to ensure this. However, this is a footgun, and actually
lead to a problem in NextJS because we used the core variant there.
Also, it is simply not isomorphic.

So to fix this, this PR puts `continueTrace` on the ACS so we can
consistently use the core variant in all environments.

Fixes #14787
s1gr1d pushed a commit that referenced this pull request Dec 20, 2024
…14813)

We have a different implementation of `continueTrace` for OTEL/Node.
Until now we relied on actually using the import from `@sentry/node` vs
`@sentry/core` to ensure this. However, this is a footgun, and actually
lead to a problem in NextJS because we used the core variant there.
Also, it is simply not isomorphic.

So to fix this, this PR puts `continueTrace` on the ACS so we can
consistently use the core variant in all environments.

Fixes #14787
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.

NextJS Pages API Route traces are not continued from incoming trace
4 participants