Skip to content

Preserve deferred middleware instrumentation spans when the adapter throws - #100552

Merged
mischnic merged 2 commits into
canaryfrom
fleet/middleware-deferred-spans-error-path
Oct 22, 2026
Merged

mischnic merged 2 commits into
canaryfrom
fleet/middleware-deferred-spans-error-path

Conversation

@sokra

@sokra sokra commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

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

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: c40fee2

@sokra
sokra requested a review from mischnic October 22, 2026 06:05
@sokra
sokra marked this pull request as ready for review October 22, 2026 06:05
Co-authored-by: Niklas Mischkulnig <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]>
@mischnic
mischnic merged commit b0d0150 into canary Oct 22, 2026
120 checks passed
@mischnic
mischnic deleted the fleet/middleware-deferred-spans-error-path branch October 22, 2026 09:15
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]>
@github-actions github-actions Bot added the locked label Nov 5, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants