Repository navigation
Preserve deferred middleware instrumentation spans when the adapter throws - #100552
Merged
Merged
Conversation
Co-authored-by: Tobias Koppers <[email protected]>
Contributor
Tests PassedCommit: c40fee2 |
sokra
marked this pull request as ready for review
October 22, 2026 06:05
mischnic
reviewed
Oct 22, 2026
Co-authored-by: Niklas Mischkulnig <[email protected]> Co-authored-by: Tobias Koppers <[email protected]>
mischnic
approved these changes
Oct 22, 2026
mischnic
pushed a commit
that referenced
this pull request
Oct 22, 2026
…hrows (#100552) ### What? Direct Node.js middleware/proxy entrypoints now emit the deferred `instrumentation.ts` lifecycle spans (`load instrumentation module`, `register instrumentation`) even when the adapter rejects the request before it runs the middleware. ### Why? Follow-up to #100350. When a request to the Node.js middleware entrypoint triggers `instrumentation.ts` registration, the lifecycle spans are deferred and emitted inside the `Middleware.execute` span. If adapter setup throws first (e.g. the configured cache handler throws while the adapter builds the request's incremental cache), the emitter is never called. Registration has already completed, so no later request receives it either, and those spans are lost for the lifetime of the instance. This only affects direct entrypoints (adapters, deployments). `next start` / `next dev` register instrumentation at startup. ### How? - The middleware entrypoint template, which owns the deferred emitter, now awaits the adapter in a `try/catch`. On error it calls the emitter, then rethrows the original error unchanged. The adapter and its options are unchanged. - The emitter already emits at most once, so the fallback does nothing when the spans were already emitted inside `Middleware.execute` (e.g. the user's middleware threw). Successful requests keep their existing span parentage. In the fallback case there is no middleware span, so the spans are emitted in the caller's active context (or as roots). - The fallback call is guarded so that a failure while tracing can't replace the adapter's error. **Test:** this reuses the existing direct-entrypoint OpenTelemetry harness (`test/e2e/opentelemetry/instrumentation`, `next start` only). The harness calls the built middleware entrypoint's `handler` with a real `Request`, like the Vercel adapter does. - The fixture's custom cache handler throws from its constructor when a test-only request header is set. The adapter constructs it per request, before running the middleware. - A new suite, in a fresh process, sends that request through the normal middleware flow and asserts that: - the original error propagates; - both lifecycle spans are collected exactly once, with no `Middleware.execute` span; - a later valid request succeeds without emitting them again. - The test fails without the fix. ### Verification - New test plus the existing "should trace instrumentation registration in the first middleware request" variants: `pnpm test-start-turbo` and `pnpm test-start-webpack` on `test/e2e/opentelemetry/instrumentation/opentelemetry.test.ts` - `pnpm jest packages/next/src/server/lib/router-utils/instrumentation-globals.test.ts` - `pnpm --filter=next types`, `pnpm typescript` <!-- NEXT_JS_LLM --> <!-- fleet bb16cbee-5cda-4702-b442-ea49bf648fe0 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]> (cherry picked from commit b0d0150) Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Luke Sandberg <[email protected]>
mischnic
pushed a commit
that referenced
this pull request
Oct 22, 2026
…0294, #100324, #100350, #100552, #100583) (#100542) Backports the OpenTelemetry request-trace stack to `releases/lts/active`: - #100349 Start the request span before route module preparation - #100294 Keep middleware and render in one OpenTelemetry trace as siblings (re-lands #100136, whose LTS backport #100226 was reverted in #100269) - #100324 docs: note that instrumentation-time spans don't join request traces - #100350 Emit instrumentation lifecycle spans in the request that registered it - #100552 Preserve deferred middleware instrumentation spans when the adapter throws - #100583 test: add non-adapter deploy snapshots to otel-middleware-trace. Without it, `otel-middleware-trace` fails the release deploy tests that deploy with `NEXT_ENABLE_ADAPTER=0`, as on v16.5.0-canary.23. The six canary merge commits are cherry-picked in order with `-x`, and all applied cleanly. The patch content is identical to canary's; only blob IDs and line offsets differ. ## Verification - `test/e2e/opentelemetry/instrumentation/`: `test-start-turbo` (122 passed) and `test-dev-turbo` (67 passed). The #100552 regression test fails without its `middleware.ts` change. - `test/e2e/app-dir/otel-middleware-trace`: `test-start-turbo`, `test-dev-turbo` and `test-start-webpack` (30 passed each, inline snapshots unchanged). The #100583 commit leaves the test file byte-identical to canary's. - Unit tests for `instrumentation-globals`, `tracer` and `forward-trace-context` (40 passed), plus `pnpm --filter=next types` - Built with `CI=true pnpm build-all` against the published `@next/swc` 16.4.2 binary. The backport touches no Rust. - PR CI deploys only through the adapter, so the non-adapter snapshots from #100583 are confirmed by the next release deploy run. <!-- NEXT_JS_LLM --> <!-- fleet ce0ecb23-b14c-4679-92a9-e676130f618e --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Luke Sandberg <[email protected]>
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Direct Node.js middleware/proxy entrypoints now emit the deferred
instrumentation.tslifecycle spans (load instrumentation module,register instrumentation) even when the adapter rejects the request before it runs the middleware.Why?
Follow-up to #100350. When a request to the Node.js middleware entrypoint triggers
instrumentation.tsregistration, the lifecycle spans are deferred and emitted inside theMiddleware.executespan. If adapter setup throws first (e.g. the configured cache handler throws while the adapter builds the request's incremental cache), the emitter is never called. Registration has already completed, so no later request receives it either, and those spans are lost for the lifetime of the instance.This only affects direct entrypoints (adapters, deployments).
next start/next devregister instrumentation at startup.How?
try/catch. On error it calls the emitter, then rethrows the original error unchanged. The adapter and its options are unchanged.Middleware.execute(e.g. the user's middleware threw). Successful requests keep their existing span parentage. In the fallback case there is no middleware span, so the spans are emitted in the caller's active context (or as roots).Test: this reuses the existing direct-entrypoint OpenTelemetry harness (
test/e2e/opentelemetry/instrumentation,next startonly). The harness calls the built middleware entrypoint'shandlerwith a realRequest, like the Vercel adapter does.Middleware.executespan;Verification
pnpm test-start-turboandpnpm test-start-webpackontest/e2e/opentelemetry/instrumentation/opentelemetry.test.tspnpm jest packages/next/src/server/lib/router-utils/instrumentation-globals.test.tspnpm --filter=next types,pnpm typescript