Skip to content

fix(nextjs): Handle Middleware.execute root spans on Node.js runtime - #22013

Merged
chargome merged 1 commit into
developfrom
cg/nextjs-proxy-instrument
Jul 7, 2026
Merged

fix(nextjs): Handle Middleware.execute root spans on Node.js runtime#22013
chargome merged 1 commit into
developfrom
cg/nextjs-proxy-instrument

Conversation

@chargome

@chargome chargome commented Jul 7, 2026

Copy link
Copy Markdown
Member

Middleware spans on node (middelware/proxy) now run in a detached otel context making Middleware.execute become root spans.

We basically now mirror what we did in edge before by also calling enhanceMiddlewareRootSpan on node. Additionally we had to fork isolation scopes on span start for these spans as these would scope leak otherwise.

ref vercel/next.js#95357
closes #22001
closes #22002

@chargome chargome self-assigned this Jul 7, 2026
@chargome

chargome commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 260bd40. Configure here.

@chargome
chargome marked this pull request as ready for review July 7, 2026 11:26
@chargome
chargome requested a review from a team as a code owner July 7, 2026 11:26
@chargome
chargome requested review from JPeer264, logaretm, mydea, nicohrubec and s1gr1d and removed request for a team and mydea July 7, 2026 11:26
@chargome
chargome merged commit 39d05cd into develop Jul 7, 2026
86 checks passed
@chargome
chargome deleted the cg/nextjs-proxy-instrument branch July 7, 2026 12:59
chargome added a commit that referenced this pull request Jul 22, 2026
…middleware root spans (#22232)

On the edge runtime, Next.js's OTel instrumentation creates and samples
the `Middleware.execute` root span before the Sentry middleware wrapper
runs. So `normalizedRequest` was never on the isolation scope at
sampling time, and tracesSampler received undefined.

This adds a `beforeSampling` hook in the edge SDK which populates
`normalizedRequest` (method, URL, query string) based on HTTP span
attributes. It also extends the edge `spanStart` handler to fork the
isolation scope for `Middleware.execute` root spans (similar to what
#22013 did on the
Node side after vercel/next.js#95357 made
middleware a detached root span).

The isolation-scope fork is extracted into a shared
`maybeForkIsolationScopeForRootSpan` utility used by both the Node and
edge handlers.

Fixes #22200

---------

Co-authored-by: Charly Gomez <charly.gomez1310@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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-16 (canary-webpack) Test Failed nextjs-16 (canary) Test Failed

2 participants