Unverified Commit ec8b5435 authored by David Alexandru Ilie's avatar David Alexandru Ilie Committed by GitHub

Trace development route compilation (#96454)

## Summary

- Trace development route compilation through
`DevBundlerService.ensurePage` after route preparation selects a route.
- Present the existing development bundler work as `compile route` in
Request Insights so cold compilation can be distinguished from route
matching.
- Keep the span internal by default without expanding the default public
OpenTelemetry allowlist.
- Preserve compilation error propagation and the existing
route-preparation parentage.

Warm requests can still record the span when `ensurePage` runs, but the
work is normally negligible after the route is compiled.

## Verification

- `HEADLESS=true pnpm test-dev-turbo
test/development/app-dir/request-insights-route-preparation/request-insights-route-preparation.test.ts`
- `HEADLESS=true pnpm test-dev-webpack
test/development/app-dir/request-insights-route-preparation/request-insights-route-preparation.test.ts`
- Both bundlers passed 2/2 tests at the final stack head.
- `pnpm --filter=next build`
parent f0166228
...@@ -403,6 +403,7 @@ describe('request insights trace viewer', () => { ...@@ -403,6 +403,7 @@ describe('request insights trace viewer', () => {
{ label: 'GET', depth: 0 }, { label: 'GET', depth: 0 },
{ label: 'match route', depth: 1 }, { label: 'match route', depth: 1 },
{ label: 'prepare route', depth: 2 }, { label: 'prepare route', depth: 2 },
{ label: 'compile route', depth: 3 },
{ label: 'reload route matchers', depth: 2 }, { label: 'reload route matchers', depth: 2 },
{ label: 'render', depth: 2 }, { label: 'render', depth: 2 },
{ label: 'load components', depth: 3 }, { label: 'load components', depth: 3 },
......
...@@ -32,6 +32,7 @@ const DEFAULT_VISIBLE_SPAN_TYPES = new Set([ ...@@ -32,6 +32,7 @@ const DEFAULT_VISIBLE_SPAN_TYPES = new Set([
'NextNodeServer.matchRoute', 'NextNodeServer.matchRoute',
'DevRouteMatcherManager.ensureRoute', 'DevRouteMatcherManager.ensureRoute',
'DevRouteMatcherManager.reloadMatchers', 'DevRouteMatcherManager.reloadMatchers',
'DevBundlerService.ensurePage',
'BaseServer.render', 'BaseServer.render',
'LoadComponents.loadComponents', 'LoadComponents.loadComponents',
'AppRender.prepareAppPageResponse', 'AppRender.prepareAppPageResponse',
......
...@@ -9,7 +9,9 @@ import { ...@@ -9,7 +9,9 @@ import {
type HmrMessageSentToBrowser, type HmrMessageSentToBrowser,
type NextJsHotReloaderInterface, type NextJsHotReloaderInterface,
} from '../dev/hot-reloader-types' } from '../dev/hot-reloader-types'
import { DevBundlerServiceSpan } from './trace/constants'
import { subscribeRequestInsights } from './trace/request-insights' import { subscribeRequestInsights } from './trace/request-insights'
import { getTracer } from './trace/tracer'
/** /**
* The DevBundlerService provides an interface to perform tasks with the * The DevBundlerService provides an interface to perform tasks with the
...@@ -61,7 +63,11 @@ export class DevBundlerService { ...@@ -61,7 +63,11 @@ export class DevBundlerService {
definition definition
) => { ) => {
// TODO: remove after ensure is pulled out of server // TODO: remove after ensure is pulled out of server
return await this.bundler.hotReloader.ensurePage(definition) return await getTracer().trace(
DevBundlerServiceSpan.ensurePage,
{ spanName: 'compile route' },
() => this.bundler.hotReloader.ensurePage(definition)
)
} }
public getServerComponentsHmrRefreshHash(): string | undefined { public getServerComponentsHmrRefreshHash(): string | undefined {
......
...@@ -99,6 +99,10 @@ enum DevRouteMatcherManagerSpan { ...@@ -99,6 +99,10 @@ enum DevRouteMatcherManagerSpan {
reloadMatchers = 'DevRouteMatcherManager.reloadMatchers', reloadMatchers = 'DevRouteMatcherManager.reloadMatchers',
} }
enum DevBundlerServiceSpan {
ensurePage = 'DevBundlerService.ensurePage',
}
enum RouterSpan { enum RouterSpan {
executeRoute = 'Router.executeRoute', executeRoute = 'Router.executeRoute',
} }
...@@ -130,6 +134,7 @@ type SpanTypes = ...@@ -130,6 +134,7 @@ type SpanTypes =
| `${RouterSpan}` | `${RouterSpan}`
| `${AppRenderSpan}` | `${AppRenderSpan}`
| `${DevRouteMatcherManagerSpan}` | `${DevRouteMatcherManagerSpan}`
| `${DevBundlerServiceSpan}`
| `${NodeSpan}` | `${NodeSpan}`
| `${AppRouteRouteHandlersSpan}` | `${AppRouteRouteHandlersSpan}`
| `${ResolveMetadataSpan}` | `${ResolveMetadataSpan}`
...@@ -173,6 +178,7 @@ export { ...@@ -173,6 +178,7 @@ export {
RouterSpan, RouterSpan,
AppRenderSpan, AppRenderSpan,
DevRouteMatcherManagerSpan, DevRouteMatcherManagerSpan,
DevBundlerServiceSpan,
NodeSpan, NodeSpan,
AppRouteRouteHandlersSpan, AppRouteRouteHandlersSpan,
ResolveMetadataSpan, ResolveMetadataSpan,
......
...@@ -25,6 +25,7 @@ describe('request-insights-route-preparation', () => { ...@@ -25,6 +25,7 @@ describe('request-insights-route-preparation', () => {
const routePreparationSpanType = 'DevRouteMatcherManager.ensureRoute' const routePreparationSpanType = 'DevRouteMatcherManager.ensureRoute'
const matcherReloadSpanType = 'DevRouteMatcherManager.reloadMatchers' const matcherReloadSpanType = 'DevRouteMatcherManager.reloadMatchers'
const routeCompilationSpanType = 'DevBundlerService.ensurePage'
async function getRequestInsights() { async function getRequestInsights() {
return (await next return (await next
...@@ -67,6 +68,10 @@ describe('request-insights-route-preparation', () => { ...@@ -67,6 +68,10 @@ describe('request-insights-route-preparation', () => {
insight.spans.some( insight.spans.some(
(span) => (span) =>
span.attributes?.['next.span_type'] === matcherReloadSpanType span.attributes?.['next.span_type'] === matcherReloadSpanType
) &&
insight.spans.some(
(span) =>
span.attributes?.['next.span_type'] === routeCompilationSpanType
) )
) )
...@@ -140,6 +145,32 @@ describe('request-insights-route-preparation', () => { ...@@ -140,6 +145,32 @@ describe('request-insights-route-preparation', () => {
} }
expect(ancestor?.spanId).toBe(rootSpan?.spanId) expect(ancestor?.spanId).toBe(rootSpan?.spanId)
} }
const routePreparationSpan = routePreparationSpans[0]
const routeCompilationSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] === routeCompilationSpanType &&
span.parentSpanId === routePreparationSpan.spanId
)
expect(routeCompilationSpans).toHaveLength(1)
const routeCompilationSpan = routeCompilationSpans[0]
expect(routeCompilationSpan).toEqual(
expect.objectContaining({
name: 'compile route',
durationMs: expect.any(Number),
status: 'ok',
parentSpanId: routePreparationSpan.spanId,
attributes: {
'next.span_category': 'nextjs',
'next.span_name': 'compile route',
'next.span_type': routeCompilationSpanType,
},
})
)
expect(Number.isFinite(routeCompilationSpan.durationMs)).toBe(true)
expect(routeCompilationSpan.durationMs).toBeGreaterThanOrEqual(0)
expect(routeCompilationSpan.traceId).toBe(rootSpan?.traceId)
} }
it('records route preparation for first and subsequent App Page requests', async () => { it('records route preparation for first and subsequent App Page requests', async () => {
......
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