Keep middleware and render in one OpenTelemetry trace as siblings (#100294)
## Summary Stacked on #100349 (request span before route module preparation). Relands #100136 (reverted in f1a69d7d) and changes the span shape so middleware and the route are **siblings** in one trace per request. ### Resulting shapes ``` next start / next dev: handleRequest GET (bubble, root) ├─ middleware GET └─ GET /page next start / next dev with host span / incoming traceparent P: P └─ handleRequest GET (bubble) ├─ middleware GET └─ GET /page separately-invoked middleware (Vercel, adapters, direct entrypoints), no parent: GET (Next.request, root, inserted by the middleware adapter) ├─ middleware GET └─ GET /page separately-invoked middleware with host span / incoming traceparent P: P ├─ middleware GET └─ GET /page ``` ### How - **Self-hosted:** next-server's middleware-invoke `handleRequest` pass writes its W3C context onto the request (`traceparent`, and `tracestate` only together with it: a received `tracestate` is removed when the context has none). Middleware (Node in-process, or the Edge sandbox via the header) and the render pass both continue it. The render pass forces that extraction even under an active host span (custom server / APM), so it nests under the bubble span; a different `traceparent` the middleware explicitly set still wins. - **Middleware adapter:** an active host span wins over a request `traceparent`; otherwise the request's `traceparent` is continued, and the middleware never changes it. Without any valid context (and OTEL enabled) the adapter inserts a `Next.request` SERVER span (new `NextSpan` enum) named after the method (`http.target` keeps the path). When the downstream request doesn't already carry that shared parent, its `traceparent`/`tracestate` are forwarded via the existing `x-middleware-request-*` overrides, always as a pair: a user-set `traceparent` keeps exactly the trace headers the user set. No headers are added without OTEL. - Request-header overrides replace the whole header set, so when the user set no overrides, the received headers are listed alongside the trace headers. There's no API to add a single header yet; that needs a server and platform change. - Node middleware is no longer detached via `ROOT_CONTEXT` (the 16.3 regression from #95306). The inserted `Next.request` root ends when the middleware finishes, so the render child (a separate invocation) can outlive it. ## Verification - Trace shapes are asserted as printed span trees (`test/lib/otel-span-tree.ts`, from #100349) in inline snapshots. Span IDs are normalized away, but distinct traces keep distinct labels, so a split trace shows up as `[trace A]` / `[trace B]`. - `test/e2e/app-dir/otel-middleware-trace` (added in #100349 with the previous behavior): this PR's snapshot diff shows middleware and render becoming siblings in one trace, in dev, start, Cache Components and deploy, for Node.js and Edge middleware and behind a custom server with a host span (including an incoming `tracestate` not being paired with the host span's context). - Collector suite `test/e2e/opentelemetry/instrumentation`: the middleware test asserts the full span tree in next start/dev, direct entrypoints with/without host span, and the custom server (host span, user-set `traceparent`). - Unit tests for the tracer and `forwardTraceContextToRequest` changes. - `test/e2e/middleware-request-header-overrides`, `test/production/typescript-basic`, `test/production/middleware-typescript` pass. <!-- 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:
Tobias Koppers <1365881+sokra@users.noreply.github.com> Co-authored-by:
Luke Sandberg <210140+lukesandberg@users.noreply.github.com> Co-authored-by:
Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
Showing
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment