Repository navigation
test: capture finished OTel spans from console output in otel-middleware-trace - #100664
Conversation
Tests PassedCommit: af7213d |
d210ba5 to
959f779
Compare
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/redirect} |
There was a problem hiding this comment.
That span is unnecessary. A redirect should not be wrapped in a Next.request.
Next.request should only be used for requests rewriting.
There was a problem hiding this comment.
Agreed. This PR only records what canary does today: adapter.ts inserts the root before the middleware runs, so before the outcome is known. #100665 now emits Next.request only when the request continues to a render (NextResponse.next() or a same-origin rewrite), and the render emits it. For a redirect, a direct response, an external rewrite, or a traceparent set by middleware, nothing is emitted: the deployed trees in #100665 show [remote] with only the middleware span under it. Regression test: should not insert a request span for a direct middleware response in test/e2e/opentelemetry/instrumentation.
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/direct} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/rewrite-external} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| `) | ||
| } else if (['deploy, middleware', 'deploy, proxy'].includes(env)) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/user-traceparent} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/user-traceparent} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/user-traceparent} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| ].includes(env) | ||
| ) { | ||
| expect(tree).toMatchInlineSnapshot(` | ||
| "[trace A] GET (Next.request) {http.target=/user-traceparent} |
There was a problem hiding this comment.
Removed in #100665 (same change as the redirect thread above): no Next.request when the request doesn't continue to a render.
| ended?: boolean | ||
| } | ||
|
|
||
| export const NOT_COVERING_BADGE = "[error: doesn't cover descendants]" |
There was a problem hiding this comment.
| export const NOT_COVERING_BADGE = "[error: doesn't cover descendants]" | |
| export const NOT_COVERING_BADGE = '[error: doesn't cover descendants]' |
Isn't prettier running on that file?
There was a problem hiding this comment.
Prettier does run on this file (lint-staged plus CI). It prefers single quotes, but switches to double quotes when the string contains a single quote, as doesn't does. The suggested '[error: doesn't cover descendants]' wouldn't parse; the single-quoted form would be '[error: doesn\'t cover descendants]', and Prettier rewrites that back to double quotes.
959f779 to
d123827
Compare
Make the otel-middleware-trace fixture write span lifecycle events
(`[otel-span] {json}`) to the console instead of reporting still-running
spans in response headers and page bodies. Tests collect the finished
spans of a request after the response: from `next.cliOutput` self-hosted,
and from `vercel logs --json` when deployed (new `getRuntimeLogs` on the
deploy instance).
`printSpanTree` now badges spans that don't cover all spans below them
(`[error: doesn't cover descendants]`) or haven't ended. Times within the
same millisecond count as covered, and `NextNodeServer.startResponse` is
left out of the check because its timing against the render span is
flaky. The snapshots are updated to the complete trees; the self-hosted
middleware-invoke root is badged.
Co-authored-by: Tobias Koppers <[email protected]>
Co-authored-by: Luke Sandberg <[email protected]>
Co-authored-by: Niklas Mischkulnig <[email protected]>
…s output Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Luke Sandberg <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Luke Sandberg <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
…and an ISR page Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Luke Sandberg <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]> Co-authored-by: Luke Sandberg <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
d123827 to
af7213d
Compare
…100690) ## What? In `next dev` with Cache Components, the background validation of a rendered route now runs in an OpenTelemetry trace of its own: its spans are children of a new root span, `validate route (app) <page>` (`next.span_type`: `AppRender.devValidation`), with a span link to the request's `render route (app) <page>` span. ## Why? `runDevValidationInBackground` (`packages/next/src/server/app-render/app-render.tsx`) waits for the response to finish, then renders the route again (`build component tree`, `generateMetadata`, …). Those spans inherited the render's context, so they were children of `render route (app)` and `BaseServer.handleRequest` even though they ran after both had ended. In `otel-middleware-trace` (#100664), that shows as 2 envelope badges on the `dev, cache components` ISR tree. ## How? - The render's span context is captured when the validation is scheduled. After `waitForFinish()` (and the existing abort check), the validation runs inside `tracer.trace(AppRenderSpan.devValidation, { root: true, links: [render span] })`, which ends the span once the validation settles, with an error status if it rejects. Validation behavior and its error handling are unchanged (the diff is mostly re-indentation). - With Request Insights the validation already runs in an isolated local trace, so that path is unchanged. With OpenTelemetry disabled nothing new runs. The code path is dev only. - The new span type is added to `NextVanillaSpanAllowlist` and documented in the OpenTelemetry guide. ## Tests In `otel-middleware-trace`: - The `dev, cache components` ISR request trees no longer contain the validation spans and lose the 2 badges they caused. The remaining root badge is the known self-hosted middleware-invoke request span (unchanged, out of scope). - A new dev Cache Components assertion finds exactly one validation root by its link to the request's render span, with no parent and in a different trace, and snapshots its tree. Stacked: #100591 ← #100664 ← **this PR** ← #100665. ## Verification - `otel-middleware-trace` in `start`/`dev` × Turbopack/webpack and with Cache Components (`start`/`dev` Turbopack): all pass. Before the fix, the new assertion fails (no linked root). - Temporary negative control (validation throws, not committed): the root ends exactly once per validation, with error status. - `test/e2e/opentelemetry/instrumentation` (`test-dev-turbo`): passes. That suite is excluded from Cache Components runs (`test/cache-components-tests-manifest.json`). - Not run: a superseding-navigation abort; it settles through the same path. - Deployed trees are unaffected (dev only): deploy CI produced the same trees as #100664. <!-- 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 <[email protected]> Co-authored-by: Luke Sandberg <[email protected]> Co-authored-by: Niklas Mischkulnig <[email protected]>
What?
Test infrastructure for
test/e2e/app-dir/otel-middleware-trace: the fixture now writes span lifecycle events to the console, and tests collect the finished spans of a request after the response, then check that every span covers all spans below it.Why?
The fixture reported spans while they were still running (in an
x-mw-spansresponse header from inside the middleware, and in the page or route body during rendering). Span timing couldn't be checked, so the test couldn't see that the insertedNext.requestroot ends before its render child. This is the baseline for the next PR in the stack, which fixes that root.How?
trace-recorder.ts): a span processor writes one[otel-span] {json}line perstartandendevent, with a unique event ID. It works in the Node.js runtime, the Edge runtime and when deployed.test/lib/otel-span-capture.ts):next.cliOutput. Deployed, it readsvercel logs --jsonthrough a newNextDeployInstance.getRuntimeLogs, using a paged reader intest/lib/vercel-runtime-logs.ts.endevents for one span fail the test with a diagnostic.vercel logsread is recorded and retried at the next poll; each call is capped at 60 s.describevariant sends every test's request in abeforeAll(in test order, so the first request stays the cold one), then collects the spans of all of them in one poll loop, with one unfilteredvercel logsread of the deployment per poll. Each test then asserts on its own response and tree, so failures stay per test. Self-hosted, each test still requests, collects and asserts in turn. This replaced per-test polling, which added up the ingestion delay and timed out late in the suite (1–3 tests per job on the first deploy runs, including a hanging call and an API 504).vercel logscall is logged (arguments with any token redacted, page, exit status, duration, record count), and every captured[otel-span]event is printed once with its times, so the raw data behind a tree is in the job log.printSpanTree):[error: doesn't cover descendants]when a descendant starts before it or ends after it, and[error: not ended]when it hasn't ended.NextNodeServer.startResponseis left out of the check, because its timing against the render span is flaky. It's still printed.x-mw-trace-idresponse header and the render's body. Incoming and middleware-settraceparents are new per request, so collection only sees that request's spans.GET (BaseServer.handleRequest)), which ends before the render it is the parent of. Fixing it is out of scope here.GET (Next.request)root of requests that continue to a render, which ends before the render. The PR above fixes that. No deploy assertions are skipped.https://example.vercel.sh), to a static page (app/static) and to an ISR page (app/isr,revalidate = 3600; with Cache Components a'use cache'+cacheLife('hours')variant). They assert the status, the page marker, that no internal headers leak, and the tree. Instart, the static and ISR pages are served from the cache (norender route (app)span). The PR above shows what changes for them deployed.dev, cache components, the ISR tree has 2 more badges, onGET /israndrender route (app) /isr. They come from Cache Components' background validation, which renders again after the response, under the render's context. Run the dev Cache Components validation in an OTel trace of its own #100690 (above) fixes that.packages/nextchanges.Testing
The rewrite edge cases (36 tests / 34 snapshots) pass in all 6 local modes (start/dev × turbo/webpack, Cache Components start/dev turbo). Before the fixture changes they failed (404). Their deploy snapshots are filled from CI. Deployed (both builders, with and without Cache Components), all three trees are just
GET (Next.request)with the middleware span: the static and ISR pages are served without invoking a function, so there are no render spans.Earlier runs against the base framework without the PR above (
otel-middleware-trace, 30 tests / 28 snapshots):All 11 runs passed, along with 41 unit tests (
test/unit/otel-span-tree.test.ts,test/unit/vercel-runtime-logs.test.ts) andpnpm typescript. The batch collection was also run locally by temporarily forcing it on for self-hosted (start and dev turbo): same snapshots, including the cold first request.Stack: #100591 ← this PR ← #100690 ← #100665. Stacked on #100591, so deploy CI runs this with both the adapter and the non-adapter builder.