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

Show route preparation in Request Insights

Co-authored-by: 's avatarDavid Ilie <david@davidilie.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 3b47db01
...@@ -304,6 +304,28 @@ describe('request insights trace viewer', () => { ...@@ -304,6 +304,28 @@ describe('request insights trace viewer', () => {
) )
}) })
it('uses a generic code-loading label without changing the OTel span', () => {
const loadComponentsSpan = {
name: 'LoadComponents.loadComponents',
startTime: 100,
durationMs: 10,
attributes: {
'next.span_type': 'LoadComponents.loadComponents',
},
}
const request = createRequest({ spans: [loadComponentsSpan] })
expect(getTraceItems(request, false)[0]?.label).toBe('load code')
expect(loadComponentsSpan).toEqual(
expect.objectContaining({
name: 'LoadComponents.loadComponents',
attributes: expect.objectContaining({
'next.span_type': 'LoadComponents.loadComponents',
}),
})
)
})
it('distinguishes same-origin and external fetches in the trace', () => { it('distinguishes same-origin and external fetches in the trace', () => {
const request = createRequest({ const request = createRequest({
fetches: [ fetches: [
...@@ -402,18 +424,10 @@ describe('request insights trace viewer', () => { ...@@ -402,18 +424,10 @@ describe('request insights trace viewer', () => {
durationMs: 5, durationMs: 5,
attributes: { 'next.span_type': 'BaseServer.prepareRequest' }, attributes: { 'next.span_type': 'BaseServer.prepareRequest' },
}, },
{
name: 'match route',
spanId: 'match',
parentSpanId: 'prepare',
startTime: 106,
durationMs: 5,
attributes: { 'next.span_type': 'NextNodeServer.matchRoute' },
},
{ {
name: 'compile route', name: 'compile route',
spanId: 'compile-route', spanId: 'compile-route',
parentSpanId: 'match', parentSpanId: 'prepare',
startTime: 107.1, startTime: 107.1,
durationMs: 1.5, durationMs: 1.5,
attributes: { attributes: {
...@@ -423,7 +437,7 @@ describe('request insights trace viewer', () => { ...@@ -423,7 +437,7 @@ describe('request insights trace viewer', () => {
{ {
name: 'render', name: 'render',
spanId: 'base-render', spanId: 'base-render',
parentSpanId: 'match', parentSpanId: 'root',
startTime: 110, startTime: 110,
durationMs: 85, durationMs: 85,
attributes: { 'next.span_type': 'BaseServer.render' }, attributes: { 'next.span_type': 'BaseServer.render' },
...@@ -448,6 +462,61 @@ describe('request insights trace viewer', () => { ...@@ -448,6 +462,61 @@ describe('request insights trace viewer', () => {
'next.span_type': 'LoadComponents.loadComponents', 'next.span_type': 'LoadComponents.loadComponents',
}, },
}, },
{
name: 'load route module',
spanId: 'load-route-module',
parentSpanId: 'load-components',
startTime: 110.25,
durationMs: 0.1,
attributes: {
'next.span_name': 'load route module',
'next.span_type': 'LoadComponents.loadRouteModule',
},
},
{
name: 'prepare route module',
spanId: 'prepare-route-module',
parentSpanId: 'render-response-components',
startTime: 110.3,
durationMs: 0.8,
attributes: {
'next.span_name': 'prepare route module',
'next.span_type': 'RouteModule.prepare',
},
},
{
name: 'load route manifests',
spanId: 'load-route-manifests',
parentSpanId: 'prepare-route-module',
startTime: 110.4,
durationMs: 0.5,
attributes: {
'next.span_name': 'load route manifests',
'next.span_type': 'RouteModule.loadManifests',
},
},
{
name: 'BaseServer.renderToResponseWithComponents',
spanId: 'render-response-components',
parentSpanId: 'base-render',
startTime: 110.28,
durationMs: 1.2,
attributes: {
'next.span_type': 'BaseServer.renderToResponseWithComponents',
},
},
{
name: 'load app route module',
spanId: 'load-app-route-module',
parentSpanId: 'render-response-components',
startTime: 111.12,
durationMs: 0.2,
attributes: {
'next.route': '/api/source',
'next.span_name': 'load app route module',
'next.span_type': 'AppRouteRouteModule.loadUserland',
},
},
{ {
name: 'prepare app page response', name: 'prepare app page response',
spanId: 'prepare-app-page', spanId: 'prepare-app-page',
...@@ -520,26 +589,32 @@ describe('request insights trace viewer', () => { ...@@ -520,26 +589,32 @@ describe('request insights trace viewer', () => {
})) }))
).toEqual([ ).toEqual([
{ label: 'GET', depth: 0 }, { label: 'GET', depth: 0 },
{ label: 'match route', depth: 1 }, { label: 'compile route', depth: 1 },
{ label: 'compile route', depth: 2 }, { label: 'render', depth: 1 },
{ label: 'render', depth: 2 }, { label: 'load code', depth: 2 },
{ label: 'load components', depth: 3 }, { label: 'load route module', depth: 3 },
{ label: 'prepare app page response', depth: 3 }, { label: 'prepare route module', depth: 2 },
{ label: 'initialize app render', depth: 3 }, { label: 'load app route module', depth: 2 },
{ label: 'render route (app) /', depth: 3 }, { label: 'prepare app page response', depth: 2 },
{ label: 'render RSC response', depth: 4 }, { label: 'initialize app render', depth: 2 },
{ label: 'wait for RSC render task', depth: 4 }, { label: 'render route (app) /', depth: 2 },
{ label: 'render HTML shell', depth: 4 }, { label: 'render RSC response', depth: 3 },
{ label: 'wait for HTML completion', depth: 4 }, { label: 'wait for RSC render task', depth: 3 },
{ label: 'render HTML shell', depth: 3 },
{ label: 'wait for HTML completion', depth: 3 },
]) ])
expect(getTraceItems(request, true).map((item) => item.label)).toEqual([ expect(getTraceItems(request, true).map((item) => item.label)).toEqual([
'GET', 'GET',
'prepare request', 'prepare request',
'match route',
'compile route', 'compile route',
'render', 'render',
'resolve page components', 'resolve page components',
'load components', 'load code',
'load route module',
'render to response with components',
'prepare route module',
'load route manifests',
'load app route module',
'prepare app page response', 'prepare app page response',
'initialize app render', 'initialize app render',
'render route (app) /', 'render route (app) /',
......
...@@ -54,14 +54,17 @@ export function getTraceNavigationIndex( ...@@ -54,14 +54,17 @@ export function getTraceNavigationIndex(
type UnnestedTraceItem = Omit<TraceItem, 'depth'> type UnnestedTraceItem = Omit<TraceItem, 'depth'>
const FETCH_SPAN_TYPE = 'AppRender.fetch' const FETCH_SPAN_TYPE = 'AppRender.fetch'
const LOAD_COMPONENTS_SPAN_TYPE = 'LoadComponents.loadComponents'
const MIDDLEWARE_SPAN_TYPE = 'Middleware.execute' const MIDDLEWARE_SPAN_TYPE = 'Middleware.execute'
const DEFAULT_VISIBLE_SPAN_TYPES = new Set([ const DEFAULT_VISIBLE_SPAN_TYPES = new Set([
'BaseServer.handleRequest', 'BaseServer.handleRequest',
MIDDLEWARE_SPAN_TYPE, MIDDLEWARE_SPAN_TYPE,
'NextNodeServer.matchRoute',
'DevBundlerService.ensurePage', 'DevBundlerService.ensurePage',
'BaseServer.render', 'BaseServer.render',
'LoadComponents.loadComponents', LOAD_COMPONENTS_SPAN_TYPE,
'LoadComponents.loadRouteModule',
'RouteModule.prepare',
'AppRouteRouteModule.loadUserland',
'AppRender.prepareAppPageResponse', 'AppRender.prepareAppPageResponse',
'AppRender.initializeRender', 'AppRender.initializeRender',
'AppRender.getBodyResult', 'AppRender.getBodyResult',
...@@ -388,6 +391,10 @@ function getSpanLabel(span: RequestInsightSpan): string { ...@@ -388,6 +391,10 @@ function getSpanLabel(span: RequestInsightSpan): string {
.replace(FIZZ_WORD, 'HTML') .replace(FIZZ_WORD, 'HTML')
.replace(FLIGHT_WORD, 'RSC') .replace(FLIGHT_WORD, 'RSC')
if (span.attributes?.['next.span_type'] === LOAD_COMPONENTS_SPAN_TYPE) {
return 'load code'
}
if (span.attributes?.['next.span_type'] === MIDDLEWARE_SPAN_TYPE) { if (span.attributes?.['next.span_type'] === MIDDLEWARE_SPAN_TYPE) {
const method = span.attributes['http.method'] const method = span.attributes['http.method']
return typeof method === 'string' && method.length > 0 return typeof method === 'string' && method.length > 0
......
...@@ -30,6 +30,7 @@ type RequestInsight = { ...@@ -30,6 +30,7 @@ type RequestInsight = {
traceId?: string traceId?: string
spanId?: string spanId?: string
parentSpanId?: string parentSpanId?: string
durationMs?: number
status?: 'ok' | 'error' status?: 'ok' | 'error'
attributes?: Record<string, string | number | boolean> attributes?: Record<string, string | number | boolean>
}> }>
...@@ -184,6 +185,144 @@ describe('request insights', () => { ...@@ -184,6 +185,144 @@ describe('request insights', () => {
}) })
}) })
it('records route module loading and preparation spans', async () => {
const existingRequestIds = new Set(
(
(await next
.fetch('/_next/development/request-insights')
.then((response) => response.json())) as {
requests: RequestInsight[]
}
).requests.map((request) => request.requestId)
)
const response = await next.fetch('/api/source?route-module-spans=1')
expect(response.status).toBe(200)
await response.json()
await retry(async () => {
const snapshot = (await next
.fetch('/_next/development/request-insights')
.then((insightsResponse) => insightsResponse.json())) as {
requests: RequestInsight[]
}
const matchingRequests = snapshot.requests.filter(
(request) =>
!existingRequestIds.has(request.requestId) &&
request.url === '/api/source?query=redacted' &&
request.kind !== 'instant-insights'
)
expect(matchingRequests).toHaveLength(1)
const request = matchingRequests[0]
const loadSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] ===
'LoadComponents.loadRouteModule'
)
const loadComponentsSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] ===
'LoadComponents.loadComponents'
)
const prepareSpans = request.spans.filter(
(span) => span.attributes?.['next.span_type'] === 'RouteModule.prepare'
)
const renderResponseComponentsSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] ===
'BaseServer.renderToResponseWithComponents'
)
const userlandSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] ===
'AppRouteRouteModule.loadUserland'
)
const manifestSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] === 'RouteModule.loadManifests'
)
expect(loadComponentsSpans).toHaveLength(1)
expect(prepareSpans).toHaveLength(1)
expect(renderResponseComponentsSpans).toHaveLength(1)
const rootSpans = request.spans.filter(
(span) =>
span.attributes?.['next.span_type'] === 'BaseServer.handleRequest' &&
span.traceId === prepareSpans[0].traceId
)
expect(rootSpans).toHaveLength(1)
const rootSpan = rootSpans[0]
expect(loadSpans).toEqual([
expect.objectContaining({
name: 'load route module',
parentSpanId: loadComponentsSpans[0].spanId,
durationMs: expect.any(Number),
status: 'ok',
traceId: rootSpan.traceId,
attributes: {
'next.span_category': 'nextjs',
'next.span_name': 'load route module',
'next.span_type': 'LoadComponents.loadRouteModule',
},
}),
])
expect(prepareSpans).toEqual([
expect.objectContaining({
name: 'prepare route module',
parentSpanId: renderResponseComponentsSpans[0].spanId,
durationMs: expect.any(Number),
status: 'ok',
traceId: rootSpan.traceId,
attributes: {
'next.span_category': 'nextjs',
'next.span_name': 'prepare route module',
'next.span_type': 'RouteModule.prepare',
},
}),
])
expect(userlandSpans).toEqual([
expect.objectContaining({
name: 'load app route module',
parentSpanId: renderResponseComponentsSpans[0].spanId,
durationMs: expect.any(Number),
status: 'ok',
traceId: rootSpan.traceId,
attributes: {
'next.route': '/api/source',
'next.span_category': 'nextjs',
'next.span_name': 'load app route module',
'next.span_type': 'AppRouteRouteModule.loadUserland',
},
}),
])
expect(manifestSpans).toEqual([
expect.objectContaining({
name: 'load route manifests',
parentSpanId: prepareSpans[0].spanId,
durationMs: expect.any(Number),
status: 'ok',
traceId: rootSpan.traceId,
attributes: {
'next.span_category': 'nextjs',
'next.span_name': 'load route manifests',
'next.span_type': 'RouteModule.loadManifests',
},
}),
])
for (const span of [
...loadSpans,
...prepareSpans,
...userlandSpans,
...manifestSpans,
]) {
expect(Number.isFinite(span.durationMs)).toBe(true)
expect(span.durationMs).toBeGreaterThanOrEqual(0)
}
})
})
it('classifies static files as assets in the serialized snapshot', async () => { it('classifies static files as assets in the serialized snapshot', async () => {
const response = await next.fetch('/request-insights-asset.svg') const response = await next.fetch('/request-insights-asset.svg')
expect(response.status).toBe(200) expect(response.status).toBe(200)
......
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