Unverified Commit 68725018 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Run the dev Cache Components validation in a trace of its own

The background validation runs after the response has finished. Its spans
inherited the render's context, so they outlived their request's spans. Run
it as the children of a new root span, validate route (app) <page>, linked
to the render span.
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent d1238274
......@@ -297,6 +297,18 @@ Attributes:
- `next.span_type`
- `next.route`
### `validate route (app) [next.route]`
- `next.span_type`: `AppRender.devValidation`
This span represents the validation `next dev` runs in the background after rendering a route with [Cache Components](/docs/app/api-reference/config/next-config-js/cacheComponents). It runs after the response has finished, so it is the root of a trace of its own, with a link to the `render route (app) [next.route]` span of the render it validates. The spans of the validation render are its children.
Attributes:
- `next.span_name`
- `next.span_type`
- `next.route`
### `fetch [http.method] [http.url]`
- `next.span_type`: `AppRender.fetch`
......
......@@ -97,6 +97,7 @@ enum AppRenderSpan {
instantInsights = 'AppRender.instantInsights',
instantInsightsPrepareValidation = 'AppRender.instantInsights.prepareValidation',
instantInsightsRunValidation = 'AppRender.instantInsights.runValidation',
devValidation = 'AppRender.devValidation',
}
enum DevBundlerServiceSpan {
......@@ -167,6 +168,7 @@ export const NextVanillaSpanAllowlist = new Set([
RenderSpan.getStaticProps,
AppRenderSpan.fetch,
AppRenderSpan.getBodyResult,
AppRenderSpan.devValidation,
LoadComponentsSpan.loadRouteModule,
RenderSpan.renderDocument,
NodeSpan.runHandler,
......
......@@ -3,6 +3,7 @@ import { randomBytes } from 'crypto'
import { isNextDeploy, isNextDev, nextTestSetup } from 'e2e-utils'
import type { NextInstance } from 'e2e-utils'
import cheerio from 'cheerio'
import { retry } from 'next-test-utils'
import {
isOneTimeSpan,
printSpanTree,
......@@ -12,6 +13,7 @@ import {
import {
collectSpanGroups,
collectSpans,
parseSpanEvents,
type SpanGroup,
type SpanGroupResult,
} from 'otel-span-capture'
......@@ -302,6 +304,49 @@ function expectNoInternalHeaders(res: Response) {
}
}
// Self-hosted dev: the span tree of the Cache Components validation of a
// request's render (`renderName`), from the server output. The validation
// runs in the background after the response, as the root of a trace of its
// own, with a link to the render span. Fails unless exactly one such root
// exists, without a parent.
async function getDevValidationTree(
next: NextInstance,
res: Response,
renderName: string
) {
const traceId = res.headers.get('x-mw-trace-id')
const endEvents = () =>
parseSpanEvents(next.cliOutput).events.filter(
(event) => event.event === 'end'
)
const render = await retry(() => {
const found = endEvents().filter(
(event) => event.traceId === traceId && event.name === renderName
)
expect(found).toHaveLength(1)
return found[0]
})
const root = await retry(() => {
const found = endEvents().filter((event) =>
event.links?.some(
(link) => link.traceId === render.traceId && link.spanId === render.id
)
)
expect(found).toHaveLength(1)
return found[0]
}, 10_000)
expect(root.parentId).toBeUndefined()
expect(root.traceId).not.toBe(render.traceId)
return printSpanTree(
await collectSpans(next, {
traceIds: [root.traceId],
expected: [root.type!, 'NextNodeServer.createComponentTree'],
since: new Date(Date.now() - LOGS_WINDOW_MS),
}),
{ omit: isOneTimeSpan }
)
}
// The environment a test runs in, which selects its span tree snapshot: how
// the app runs (`start`, `dev`, or `deploy`, deployed with the adapter or
// without it, `(no adapter)`), the bundler (Turbopack, or ` (webpack)`),
......@@ -1119,18 +1164,14 @@ describe.each(VARIANTS)(
) {
expect(tree).toMatchInlineSnapshot(`
"[trace A] GET (BaseServer.handleRequest) {http.target=/rewrite-isr, http.status_code=200} [error: doesn't cover descendants]
├─ GET /isr (BaseServer.handleRequest) {http.target=/rewrite-isr, http.route=/isr, next.route=/isr, http.status_code=200} [error: doesn't cover descendants]
├─ GET /isr (BaseServer.handleRequest) {http.target=/rewrite-isr, http.route=/isr, next.route=/isr, http.status_code=200}
│ ├─ NextNodeServer.clientComponentLoading
│ ├─ prepare route module (RouteModule.prepare)
│ ├─ render route (app) /isr (AppRender.getBodyResult) {next.route=/isr} [error: doesn't cover descendants]
│ │ ├─ build component tree (NextNodeServer.createComponentTree)
│ │ │ ├─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
│ │ │ └─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
│ ├─ render route (app) /isr (AppRender.getBodyResult) {next.route=/isr}
│ │ ├─ build component tree (NextNodeServer.createComponentTree)
│ │ │ ├─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
│ │ │ └─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
│ │ ├─ generateMetadata /layout (ResolveMetadata.generateMetadata)
│ │ ├─ generateMetadata /layout (ResolveMetadata.generateMetadata)
│ │ └─ start response (NextNodeServer.startResponse)
│ └─ resolve page components (NextNodeServer.findPageComponents) {next.route=/isr}
│ └─ load route module (LoadComponents.loadRouteModule)
......@@ -1158,6 +1199,20 @@ describe.each(VARIANTS)(
// Not observed yet (filled from CI).
expect(tree).toMatchInlineSnapshot()
}
// In dev, Cache Components validate the page in the background after
// the response: in a trace of its own, linked to the render.
if (isNextDev && process.env.__NEXT_CACHE_COMPONENTS) {
expect(
await getDevValidationTree(next, res, 'render route (app) /isr')
).toMatchInlineSnapshot(`
"[trace A] validate route (app) /isr (AppRender.devValidation) {next.route=/isr}
├─ build component tree (NextNodeServer.createComponentTree)
│ ├─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
│ └─ resolve segment modules (NextNodeServer.getLayoutOrPageModule)
└─ generateMetadata /layout (ResolveMetadata.generateMetadata)"
`)
}
}
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment