Skip to content

feat(nextjs): Keep requests through the proxy in one trace - #27086

Draft
chargome wants to merge 3 commits into
developfrom
feat/nextjs-proxy-render-trace-link
Draft

chargome wants to merge 3 commits into
developfrom
feat/nextjs-proxy-render-trace-link

Conversation

@chargome

@chargome chargome commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What

With Next.js 16.4.3 and later, a request that goes through the proxy now ends up in one trace, with next start
and where the proxy runs on its own, such as on Vercel. When the request brings no trace, as for a page load,
the proxy's root span now also parents the render. With an incoming trace, proxy and render continue it as
siblings, as before.

The proxy hands its trace to the render in traceparent and tracestate

A request through the proxy on Vercel, in Sentry: the render (GET /api/endpoint-behind-proxy) is a child of the proxy's root span, next to the middleware span.

Sentry trace view: the render segment nested under the middleware segment

Why

Since 16.4.3, Next.js hands the proxy's trace to the render only in the W3C traceparent and tracestate
headers, which the SDK neither wrote nor read, so proxy and render ended up in two traces. The SDK now puts
the Sentry trace and sampling context in its own tracestate entries and only continues a traceparent that
carries them, so incoming traces from other services behave as before.

A browser trace with Replay needs about 290 characters of sampling context, more than one tracestate entry
may hold (256). Instead of dropping keys, the SDK splits it into entries of at most 128 characters, which the
spec says vendors keep longest when they truncate the header. If an entry gets lost, the render continues the
trace without the sampling context. Not covered: edge-runtime middleware, and custom adapters that run the
proxy under a span of their own.

Closes: #26980

Since Next.js 16.4.3, the proxy hands its trace over to the render in the
W3C `traceparent` and `tracestate` request headers. The SDK now writes and
reads this hand-off, so the render segment becomes a child of the proxy
segment, with `next start` and where the proxy runs on its own (Vercel).

Fixes #26980
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome chargome self-assigned this Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 990c723 (PR test merge using develop at 29e62cd)

Against: 1c10c6f (develop) · base run #68574

⚠️ Baseline mismatch: 5 commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once the baseline build for 29e62cd finishes.

Path Size % Change Change
@sentry/browser 30.64 kB - -
@sentry/browser - with treeshaking flags 28.73 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.62 kB - -
@sentry/browser (incl. Tracing) 53.98 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing Core) 40.82 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 53.98 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing, Profiling) 56.86 kB - -
@sentry/browser (incl. Tracing, Replay) 93.96 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 82.52 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 98.8 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 111.75 kB - -
@sentry/browser (incl. Feedback) 48.51 kB +0.01% +1 B 🔺
@sentry/browser (incl. sendFeedback) 31.2 kB - -
@sentry/browser (incl. FeedbackAsync) 36.26 kB +0.01% +1 B 🔺
@sentry/browser (incl. Metrics) 31.96 kB - -
@sentry/browser (incl. Logs) 32.25 kB - -
@sentry/browser (incl. Metrics & Logs) 32.91 kB - -
@sentry/react 32.5 kB - -
@sentry/react (incl. Tracing) 56.46 kB - -
@sentry/vue 38.99 kB - -
@sentry/vue (incl. Tracing) 57.05 kB -0.01% -1 B 🔽
@sentry/svelte 30.68 kB - -
@sentry/effect (client) 42.21 kB -0.01% -1 B 🔽
@sentry/effect (client, incl. Tracing) 55.02 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 66.5 kB - -
@sentry/remix (Remix 3 client bundle) 59.47 kB -0.01% -1 B 🔽
CDN Bundle 32.45 kB - -
CDN Bundle (incl. Tracing) 54.12 kB - -
CDN Bundle (incl. Logs, Metrics) 35.2 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 56.57 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 76.15 kB - -
CDN Bundle (incl. Tracing, Replay) 91.93 kB -0.01% -1 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 94.44 kB +0.01% +1 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 97.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.85 kB - -
CDN Bundle - uncompressed 94.67 kB - -
CDN Bundle (incl. Tracing) - uncompressed 160.02 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 103.01 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 167.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 233.4 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 280.45 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 288.08 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 300 kB - -
@sentry/nextjs (client) 59.75 kB - -
@sentry/sveltekit (client) 54.8 kB - -
@sentry/astro (client) - rolldown, without tracing 35.73 kB - -
@sentry/core/server 45.91 kB - -
@sentry/core/browser 14.55 kB - -
@sentry/node 157.66 kB +0.01% +1 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.63 kB - -
@sentry/node - without tracing 89.66 kB +0.01% +1 B 🔺
@sentry/node - without channel injection 146.99 kB +0.01% +1 B 🔺
@sentry/aws-serverless 100.57 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 212.87 kB - -
@sentry/cloudflare (withSentry) 493.43 kB - -
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 215.81 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 241.03 kB +1.15% +2.73 kB 🔺

View base workflow run

@chargome
chargome added this pull request to stack #27088 December 2, 2026 15:43
@chargome

chargome commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

@sentry-junior review

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

All optional checks were successful. 7 successful. See the run.

Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E vinext-cf-workers (workers cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-14-supabase Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details

A browser trace with Replay needs about 290 characters of dynamic sampling
context, more than one tracestate entry may hold. Instead of dropping keys,
split it into entries of at most 128 characters, which vendors keep when
they truncate tracestate. A missing entry continues the trace without it.

Refs #26980
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@sentry-junior sentry-junior Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the human reviewer

  • The separately invoked proxy (Next.request, Vercel or adapters) is covered only by unit tests. Has it been checked on a real deployment? This review didn't run it.


/** @inheritDoc */
public extract(ctx: Context, carrier: unknown, getter: TextMapGetter): Context {
const handOff = extractTraceHandOff(getter.get(carrier, 'traceparent'), getter.get(carrier, 'tracestate'));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[m] packages/nextjs/src/server/nextSentryPropagator.ts:76: A proxy that forwards its request headers reparents the downstream service

In next start, the middleware-invoke pass writes the hand-off traceparent/tracestate onto req.headers before the proxy runs, so they are in the proxy's request.headers. If the proxy does fetch(url, { headers: request.headers }) (or rewrites) to another service running @sentry/nextjs, the hand-off survives: Sentry's outgoing fetch instrumentation adds sentry-trace, but it never overwrites an existing traceparent and never touches tracestate (packages/node/src/utils/outgoingFetchRequest.ts:98, packages/core/src/fetch.ts:233). The downstream extract then prefers the hand-off over the sentry-trace of the http.client span, as the test prefers the hand-off over an incoming sentry-trace header asserts. The result is that the downstream request gets parented to the upstream BaseServer.handleRequest span instead of the outgoing request span. That also goes against the description line "only continues a traceparent that carries that entry, so incoming traces from other services behave as before".

https://github.com/vercel/next.js/blob/v16.4.3/packages/next/src/server/base-server.ts#L926-L948

With next start, the hand-off is on the request headers the proxy sees. A
proxy that forwards them to another service sent the hand-off along with
the sentry-trace of its outgoing request, and the service continued the
hand-off. The hand-off now names the parent of the proxy span, and a
sentry-trace other than that parent wins over it.

Refs #26980
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Next.js: Verify the new single-trace shape for proxy/middleware requests and cover it in E2E

1 participant