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

Collect React timings from the browser decoder

Co-authored-by: 's avatarDavid Ilie <david@davidilie.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent ebecaf52
import './app-globals' import './app-globals'
import { NEXT_REQUEST_ID_HEADER } from './components/app-router-headers'
import ReactDOMClient from 'react-dom/client' import ReactDOMClient from 'react-dom/client'
import React from 'react' import React from 'react'
// TODO: Explicitly import from client.browser // TODO: Explicitly import from client.browser
...@@ -32,9 +33,9 @@ import { initializeRouterTransitionModules } from './components/router-transitio ...@@ -32,9 +33,9 @@ import { initializeRouterTransitionModules } from './components/router-transitio
/// <reference types="react-dom/experimental" /> /// <reference types="react-dom/experimental" />
const createFromReadableStream = let createFromReadableStream =
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream'] createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
const createFromFetch = let createFromFetch =
createFromFetchBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromFetch'] createFromFetchBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromFetch']
const appElement: HTMLElement | Document = document const appElement: HTMLElement | Document = document
...@@ -224,6 +225,25 @@ if ( ...@@ -224,6 +225,25 @@ if (
debugChannel = createDebugChannel(undefined) debugChannel = createDebugChannel(undefined)
} }
if (process.env.__NEXT_DEV_SERVER && process.env.__NEXT_REQUEST_INSIGHTS) {
const { createBrowserReactTiming } =
require('./dev/react-render-timing') as typeof import('./dev/react-render-timing')
if (self.__next_r) {
const timing = createBrowserReactTiming({
[NEXT_REQUEST_ID_HEADER]: self.__next_r,
})
const decodeStream = createFromReadableStream
const decodeFetch = createFromFetch
if (debugChannel?.readable) {
debugChannel.readable = timing.wrapStream(debugChannel.readable)
}
createFromReadableStream = (stream, options) =>
timing.run(() => decodeStream(timing.wrapStream(stream), options))
createFromFetch = (response, options) =>
timing.run(() => decodeFetch(timing.wrapResponse(response), options))
}
}
let initialServerResponse: Promise<InitialRSCPayload> let initialServerResponse: Promise<InitialRSCPayload>
if (instantTestStaticFetch) { if (instantTestStaticFetch) {
// Instant Navigation Testing API: hydrate from the static RSC payload // Instant Navigation Testing API: hydrate from the static RSC payload
......
...@@ -693,6 +693,7 @@ function createHaltingFlightResponse<T>( ...@@ -693,6 +693,7 @@ function createHaltingFlightResponse<T>(
// React attaches `_debugInfo` to the returned promise at runtime. // React attaches `_debugInfo` to the returned promise at runtime.
return createFromNextReadableStream<T>(wrapper, headers, { return createFromNextReadableStream<T>(wrapper, headers, {
allowPartialStream: true, allowPartialStream: true,
observeTiming: true,
}) as Promise<T> & { _debugInfo?: Array<any> } }) as Promise<T> & { _debugInfo?: Array<any> }
} }
...@@ -877,23 +878,63 @@ export async function createFetch<T>( ...@@ -877,23 +878,63 @@ export async function createFetch<T>(
export function createFromNextReadableStream<T>( export function createFromNextReadableStream<T>(
flightStream: ReadableStream<Uint8Array>, flightStream: ReadableStream<Uint8Array>,
requestHeaders: RequestHeaders | undefined, requestHeaders: RequestHeaders | undefined,
options?: { allowPartialStream?: boolean } options?: { allowPartialStream?: boolean; observeTiming?: boolean }
): Promise<T> { ): Promise<T> {
return createFromReadableStream(flightStream, { const debugChannel = createDebugChannel && createDebugChannel(requestHeaders)
callServer, if (
findSourceMapURL, process.env.__NEXT_DEV_SERVER &&
debugChannel: createDebugChannel && createDebugChannel(requestHeaders), process.env.__NEXT_REQUEST_INSIGHTS &&
unstable_allowPartialStream: options?.allowPartialStream, options?.observeTiming &&
}) requestHeaders
) {
const { createBrowserReactTiming } =
require('../../dev/react-render-timing') as typeof import('../../dev/react-render-timing')
const timing = createBrowserReactTiming(requestHeaders)
if (debugChannel?.readable) {
debugChannel.readable = timing.wrapStream(debugChannel.readable)
}
return timing.run(() =>
createFromReadableStream<T>(timing.wrapStream(flightStream), {
callServer,
findSourceMapURL,
debugChannel,
unstable_allowPartialStream: options.allowPartialStream,
})
)
} else {
return createFromReadableStream(flightStream, {
callServer,
findSourceMapURL,
debugChannel,
unstable_allowPartialStream: options?.allowPartialStream,
})
}
} }
function createFromNextFetch<T>( function createFromNextFetch<T>(
promiseForResponse: Promise<Response>, promiseForResponse: Promise<Response>,
requestHeaders: RequestHeaders requestHeaders: RequestHeaders
): Promise<T> & { _debugInfo?: Array<any> } { ): Promise<T> & { _debugInfo?: Array<any> } {
return createFromFetch(promiseForResponse, { const debugChannel = createDebugChannel && createDebugChannel(requestHeaders)
callServer, if (process.env.__NEXT_DEV_SERVER && process.env.__NEXT_REQUEST_INSIGHTS) {
findSourceMapURL, const { createBrowserReactTiming } =
debugChannel: createDebugChannel && createDebugChannel(requestHeaders), require('../../dev/react-render-timing') as typeof import('../../dev/react-render-timing')
}) const timing = createBrowserReactTiming(requestHeaders)
if (debugChannel?.readable) {
debugChannel.readable = timing.wrapStream(debugChannel.readable)
}
return timing.run(() =>
createFromFetch<T>(timing.wrapResponse(promiseForResponse), {
callServer,
findSourceMapURL,
debugChannel,
})
)
} else {
return createFromFetch(promiseForResponse, {
callServer,
findSourceMapURL,
debugChannel,
})
}
} }
...@@ -251,15 +251,29 @@ async function fetchServerAction( ...@@ -251,15 +251,29 @@ async function fetchServerAction(
? processFetch(res).then(({ response: r }) => r) ? processFetch(res).then(({ response: r }) => r)
: Promise.resolve(res) : Promise.resolve(res)
const response: ActionFlightResponse = await createFromFetch( const debugChannel = createDebugChannel && createDebugChannel(headers)
responsePromise, let response: ActionFlightResponse
{ if (process.env.__NEXT_DEV_SERVER && process.env.__NEXT_REQUEST_INSIGHTS) {
const { createBrowserReactTiming } =
require('../../../dev/react-render-timing') as typeof import('../../../dev/react-render-timing')
const timing = createBrowserReactTiming(headers)
if (debugChannel?.readable) {
debugChannel.readable = timing.wrapStream(debugChannel.readable)
}
response = await timing.run(() =>
createFromFetch<ActionFlightResponse>(
timing.wrapResponse(responsePromise),
{ callServer, findSourceMapURL, temporaryReferences, debugChannel }
)
)
} else {
response = await createFromFetch(responsePromise, {
callServer, callServer,
findSourceMapURL, findSourceMapURL,
temporaryReferences, temporaryReferences,
debugChannel: createDebugChannel && createDebugChannel(headers), debugChannel,
} })
) }
// An internal redirect can send an RSC response, but does not have a useful `actionResult`. // An internal redirect can send an RSC response, but does not have a useful `actionResult`.
actionResult = redirectLocation ? undefined : response.a actionResult = redirectLocation ? undefined : response.a
......
...@@ -37,7 +37,19 @@ export function createWebSocket( ...@@ -37,7 +37,19 @@ export function createWebSocket(
const sendMessage = (data: string) => { const sendMessage = (data: string) => {
if (webSocket && webSocket.readyState === webSocket.OPEN) { if (webSocket && webSocket.readyState === webSocket.OPEN) {
webSocket.send(data) webSocket.send(data)
return true
} }
return false
}
let setReactTimingSender:
| typeof import('../../react-render-timing').setReactTimingSender
| undefined
if (process.env.__NEXT_DEV_SERVER && process.env.__NEXT_REQUEST_INSIGHTS) {
setReactTimingSender = (
require('../../react-render-timing') as typeof import('../../react-render-timing')
).setReactTimingSender
} else {
setReactTimingSender = undefined
} }
let runtimeErrorStateReporter: ReturnType< let runtimeErrorStateReporter: ReturnType<
typeof import('../runtime-error-state').createRuntimeErrorStateReporter typeof import('../runtime-error-state').createRuntimeErrorStateReporter
...@@ -65,6 +77,7 @@ export function createWebSocket( ...@@ -65,6 +77,7 @@ export function createWebSocket(
function handleOnline() { function handleOnline() {
logQueue.onSocketReady(newWebSocket) logQueue.onSocketReady(newWebSocket)
setReactTimingSender?.(sendMessage)
runtimeErrorStateReporter?.reportCurrent() runtimeErrorStateReporter?.reportCurrent()
reconnections = 0 reconnections = 0
...@@ -127,6 +140,7 @@ export function createWebSocket( ...@@ -127,6 +140,7 @@ export function createWebSocket(
} }
function handleDisconnect() { function handleDisconnect() {
setReactTimingSender?.(undefined)
newWebSocket.onerror = null newWebSocket.onerror = null
newWebSocket.onclose = null newWebSocket.onclose = null
newWebSocket.close() newWebSocket.close()
......
This diff is collapsed.
import { NEXT_REQUEST_ID_HEADER } from '../components/app-router-headers'
import { HMR_MESSAGE_SENT_TO_SERVER } from '../../server/dev/hot-reloader-types'
import type { ReactTimingRecord } from '../../shared/lib/react-debug-timing'
import {
copyBrowserReactTimingRecord,
isBrowserReactTimingRecord,
MAX_BROWSER_REACT_TIMING_BATCH,
MAX_BROWSER_REACT_TIMING_MESSAGE_LENGTH,
MAX_BROWSER_REACT_TIMING_QUEUE,
} from '../../shared/lib/react-debug-timing-transport'
import {
bindReactDecoderPromise,
bindReactDecoderStream,
runWithReactDecoderScope,
type ReactDecoderScope,
} from './react-decoder-host'
let sendMessage: ((message: string) => boolean | void) | undefined
let nextDecoderId = 0
// Only primitive records waiting for the initial HMR connection, never decoded
// roots or responses. Bound the number of pending decoders as well as records.
const pending = new Set<() => void>()
let pendingOverflow = false
export function setReactTimingSender(
sender: ((message: string) => boolean | void) | undefined
): void {
sendMessage = sender
if (sender) {
if (pendingOverflow) {
pendingOverflow = false
console.warn(
'[Request Insights] Some React timings were dropped while the development connection was unavailable.'
)
}
for (const flush of [...pending]) flush()
}
}
export function createBrowserReactTiming(
requestHeaders: Record<string, string>
) {
const requestId = requestHeaders[NEXT_REQUEST_ID_HEADER]
const decoderId = String(nextDecoderId++)
let nextRecordId = 0
let timer: ReturnType<typeof setTimeout> | undefined
let partial = false
let partialReported = false
const records: ReactTimingRecord[] = []
function flush() {
if (!sendMessage) {
if (
pending.size >= MAX_BROWSER_REACT_TIMING_BATCH &&
!pending.has(flush)
) {
records.length = 0
partial = true
pendingOverflow = true
} else {
pending.add(flush)
}
return
}
pending.delete(flush)
while (records.length > 0 || (partial && !partialReported)) {
const batch = records.slice(0, MAX_BROWSER_REACT_TIMING_BATCH)
const payload = {
event: HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS,
requestId,
decoderId,
partial: partial || undefined,
records: batch,
}
let message = JSON.stringify(payload)
while (
message.length > MAX_BROWSER_REACT_TIMING_MESSAGE_LENGTH &&
batch.length > 1
) {
batch.pop()
message = JSON.stringify(payload)
}
let sent = false
try {
sent = sendMessage(message) !== false
} catch {
// A socket can close between readiness checking and send(). Keep this
// bounded batch for the next connection, without a polling timer.
}
if (!sent) {
sendMessage = undefined
pending.add(flush)
return
}
records.splice(0, batch.length)
partialReported ||= partial
}
}
const scope: ReactDecoderScope = {
record(timing) {
const record = { ...timing, id: `decoded:${nextRecordId++}` }
if (
records.length < MAX_BROWSER_REACT_TIMING_QUEUE &&
isBrowserReactTimingRecord(record)
) {
records.push(copyBrowserReactTimingRecord(record))
} else {
partial = true
}
if (timer === undefined) {
timer = setTimeout(() => {
timer = undefined
flush()
}, 0)
}
},
}
return {
run<T>(callback: () => T): T {
return runWithReactDecoderScope(scope, callback)
},
wrapStream<T>(stream: ReadableStream<T>): ReadableStream<T> {
return bindReactDecoderStream(stream, scope)
},
wrapResponse(promise: Promise<Response>): Promise<Response> {
return bindReactDecoderPromise(
promise.then((response) => {
if (!response.body) return response
const body = bindReactDecoderStream(response.body, scope)
return new Proxy(response, {
get(target, key) {
if (key === 'body') return body
const value = Reflect.get(target, key, target)
return typeof value === 'function' ? value.bind(target) : value
},
})
}),
scope
)
},
}
}
...@@ -294,6 +294,7 @@ function getSpanTraceItem( ...@@ -294,6 +294,7 @@ function getSpanTraceItem(
const environment = span.attributes?.['next.rsc.environment'] const environment = span.attributes?.['next.rsc.environment']
const renderId = span.attributes?.['next.rsc.render_id'] const renderId = span.attributes?.['next.rsc.render_id']
const componentPath = span.attributes?.['next.rsc.component_path'] const componentPath = span.attributes?.['next.rsc.component_path']
const outcome = span.attributes?.['next.rsc.outcome']
const incompleteReason = span.attributes?.['next.rsc.incomplete_reason'] const incompleteReason = span.attributes?.['next.rsc.incomplete_reason']
const file = span.attributes?.['next.rsc.source.file'] const file = span.attributes?.['next.rsc.source.file']
const line1 = span.attributes?.['next.rsc.source.line'] const line1 = span.attributes?.['next.rsc.source.line']
...@@ -343,7 +344,7 @@ function getSpanTraceItem( ...@@ -343,7 +344,7 @@ function getSpanTraceItem(
category: getSpanCategory(span), category: getSpanCategory(span),
label, label,
fullLabel: reactTiming fullLabel: reactTiming
? `${label} · ${reactTiming}${typeof environment === 'string' && environment ? ` · ${environment}` : ''}${environment === 'Cache' ? ' · May include replayed timings from cached output' : ''}` ? `${label} · ${reactTiming}${typeof environment === 'string' && environment ? ` · ${environment}` : ''}${outcome === 'errored' ? ' · Errored' : outcome === 'aborted' ? ' · Aborted' : ''}${environment === 'Cache' ? ' · May include replayed timings from cached output' : ''}`
: undefined, : undefined,
startTime: span.startTime, startTime: span.startTime,
durationMs: span.durationMs, durationMs: span.durationMs,
......
...@@ -998,7 +998,6 @@ async function generateDynamicFlightRenderResult( ...@@ -998,7 +998,6 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences, temporaryReferences: options?.temporaryReferences,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal, signal: requestAbortSignal,
} }
) )
...@@ -1035,7 +1034,6 @@ async function generateDynamicFlightRenderResult( ...@@ -1035,7 +1034,6 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences, temporaryReferences: options?.temporaryReferences,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal, signal: requestAbortSignal,
} }
) )
...@@ -1542,7 +1540,6 @@ async function generateDynamicFlightRenderResultWithStagesInDev( ...@@ -1542,7 +1540,6 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
onError: onError, onError: onError,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) )
} }
...@@ -1971,7 +1968,6 @@ async function finalRuntimeServerPrerender( ...@@ -1971,7 +1968,6 @@ async function finalRuntimeServerPrerender(
onError, onError,
signal: finalServerController.signal, signal: finalServerController.signal,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) )
...@@ -3825,7 +3821,6 @@ async function renderToStream( ...@@ -3825,7 +3821,6 @@ async function renderToStream(
onError: serverComponentsErrorHandler, onError: serverComponentsErrorHandler,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) )
reactServerResult = new ReactServerResult(serverStream) reactServerResult = new ReactServerResult(serverStream)
...@@ -4026,7 +4021,6 @@ async function renderToStream( ...@@ -4026,7 +4021,6 @@ async function renderToStream(
filterStackFrame, filterStackFrame,
onError: serverComponentsErrorHandler, onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) )
) )
...@@ -4069,7 +4063,6 @@ async function renderToStream( ...@@ -4069,7 +4063,6 @@ async function renderToStream(
filterStackFrame, filterStackFrame,
onError: serverComponentsErrorHandler, onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) )
) )
...@@ -5683,7 +5676,6 @@ async function streamStagedRenderInDev({ ...@@ -5683,7 +5676,6 @@ async function streamStagedRenderInDev({
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal, signal: requestAbortSignal,
} }
) as Readable ) as Readable
...@@ -5855,7 +5847,6 @@ async function renderWithWarmCachesForValidationInDev( ...@@ -5855,7 +5847,6 @@ async function renderWithWarmCachesForValidationInDev(
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: validationAbortSignal, signal: validationAbortSignal,
} }
) as Readable ) as Readable
...@@ -5987,7 +5978,6 @@ async function prerenderWithWarmCachesForStaticValidationInDev( ...@@ -5987,7 +5978,6 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
startTime, startTime,
filterStackFrame, filterStackFrame,
debugChannel: debugChannel?.serverSide, debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
} }
) as Readable ) as Readable
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import { PassThrough, Writable, type Readable } from 'node:stream' import { PassThrough, Writable, type Readable } from 'node:stream'
import type { DebugChannelServer } from './debug-channel-server.web' import type { DebugChannelServer } from './debug-channel-server.web'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
export { createWebDebugChannel } from './debug-channel-server.web' export { createWebDebugChannel } from './debug-channel-server.web'
...@@ -17,7 +16,6 @@ export { createWebDebugChannel } from './debug-channel-server.web' ...@@ -17,7 +16,6 @@ export { createWebDebugChannel } from './debug-channel-server.web'
export type NodeDebugChannelPair = { export type NodeDebugChannelPair = {
serverSide: DebugChannelServer serverSide: DebugChannelServer
clientSide: { readable: Readable } clientSide: { readable: Readable }
localRenderTiming?: LocalRenderTiming
} }
/** /**
...@@ -26,14 +24,6 @@ export type NodeDebugChannelPair = { ...@@ -26,14 +24,6 @@ export type NodeDebugChannelPair = {
* which expects debugChannel to be a Node.js stream with a .write() method. * which expects debugChannel to be a Node.js stream with a .write() method.
*/ */
export function createNodeDebugChannel(): NodeDebugChannelPair { export function createNodeDebugChannel(): NodeDebugChannelPair {
let localRenderTiming: LocalRenderTiming | undefined
if (process.env.__NEXT_DEV_SERVER) {
const { createLocalRenderTiming } =
require('../lib/trace/react-render-timing') as typeof import('../lib/trace/react-render-timing')
localRenderTiming = createLocalRenderTiming()
} else {
localRenderTiming = undefined
}
// The readable side is a PassThrough that the client reads from. The // The readable side is a PassThrough that the client reads from. The
// server-side write target is a separate, write-only Writable that forwards // server-side write target is a separate, write-only Writable that forwards
// into it rather than the PassThrough itself: React's renderToPipeableStream // into it rather than the PassThrough itself: React's renderToPipeableStream
...@@ -52,24 +42,15 @@ export function createNodeDebugChannel(): NodeDebugChannelPair { ...@@ -52,24 +42,15 @@ export function createNodeDebugChannel(): NodeDebugChannelPair {
const writable = new Writable({ const writable = new Writable({
write(chunk, encoding, callback) { write(chunk, encoding, callback) {
localRenderTiming?.readDebugChunk(chunk)
passthrough.write(chunk, encoding, callback) passthrough.write(chunk, encoding, callback)
}, },
final(callback) { final(callback) {
localRenderTiming?.finishDebug()
passthrough.end(callback) passthrough.end(callback)
}, },
}) })
writable.once('close', () => {
if (!writable.writableFinished) localRenderTiming?.abort()
})
passthrough.once('close', () => {
if (!passthrough.readableEnded) localRenderTiming?.abort()
})
return { return {
serverSide: writable, serverSide: writable,
clientSide: { readable: passthrough }, clientSide: { readable: passthrough },
localRenderTiming,
} }
} }
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
*/ */
import type { AnyStream } from './app-render-prerender-utils' import type { AnyStream } from './app-render-prerender-utils'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
export type DebugChannelPair = { export type DebugChannelPair = {
serverSide: DebugChannelServer serverSide: DebugChannelServer
clientSide: DebugChannelClient clientSide: DebugChannelClient
localRenderTiming?: LocalRenderTiming
} }
export type DebugChannelServer = any export type DebugChannelServer = any
...@@ -24,44 +22,29 @@ type DebugChannelClient = { ...@@ -24,44 +22,29 @@ type DebugChannelClient = {
* which expects debugChannel = { writable: WritableStream }. * which expects debugChannel = { writable: WritableStream }.
*/ */
export function createWebDebugChannel(): DebugChannelPair { export function createWebDebugChannel(): DebugChannelPair {
let localRenderTiming: LocalRenderTiming | undefined
if (process.env.__NEXT_DEV_SERVER && process.env.NEXT_RUNTIME !== 'edge') {
const { createLocalRenderTiming } =
require('../lib/trace/react-render-timing') as typeof import('../lib/trace/react-render-timing')
localRenderTiming = createLocalRenderTiming()
} else {
localRenderTiming = undefined
}
let readableController: ReadableStreamDefaultController | undefined let readableController: ReadableStreamDefaultController | undefined
const clientSideReadable = new ReadableStream<Uint8Array>({ const clientSideReadable = new ReadableStream<Uint8Array>({
start(controller) { start(controller) {
readableController = controller readableController = controller
}, },
cancel() {
localRenderTiming?.abort()
},
}) })
return { return {
serverSide: { serverSide: {
writable: new WritableStream<Uint8Array>({ writable: new WritableStream<Uint8Array>({
write(chunk) { write(chunk) {
localRenderTiming?.readDebugChunk(chunk)
readableController?.enqueue(chunk) readableController?.enqueue(chunk)
}, },
close() { close() {
localRenderTiming?.finishDebug()
readableController?.close() readableController?.close()
}, },
abort(err) { abort(err) {
localRenderTiming?.abort()
readableController?.error(err) readableController?.error(err)
}, },
}), }),
}, },
clientSide: { readable: clientSideReadable }, clientSide: { readable: clientSideReadable },
localRenderTiming,
} }
} }
......
...@@ -547,7 +547,6 @@ export function renderToNodeFlightStream( ...@@ -547,7 +547,6 @@ export function renderToNodeFlightStream(
opts: FlightRenderOptions opts: FlightRenderOptions
): AnyStream { ): AnyStream {
if (!ComponentMod.renderToPipeableStream) { if (!ComponentMod.renderToPipeableStream) {
opts.localRenderTiming?.abort()
throw new Error('renderToPipeableStream is not implemented') throw new Error('renderToPipeableStream is not implemented')
} }
...@@ -556,21 +555,9 @@ export function renderToNodeFlightStream( ...@@ -556,21 +555,9 @@ export function renderToNodeFlightStream(
// the returned pipeable ourselves when it fires. We drop the listener when // the returned pipeable ourselves when it fires. We drop the listener when
// the passthrough closes so a finished render's `pipeable` isn't retained by // the passthrough closes so a finished render's `pipeable` isn't retained by
// the request signal, which can outlive it. // the request signal, which can outlive it.
const { signal, localRenderTiming, ...renderOptions } = opts ?? {} const { signal, ...renderOptions } = opts ?? {}
localRenderTiming?.start() const pt = new PassThrough()
const pt = localRenderTiming
? new Transform({
transform(chunk, _encoding, callback) {
localRenderTiming.readFlightChunk(chunk)
callback(null, chunk)
},
flush(callback) {
localRenderTiming.finishFlight()
callback()
},
})
: new PassThrough()
let pipeable: ReturnType< let pipeable: ReturnType<
NonNullable<FlightComponentMod['renderToPipeableStream']> NonNullable<FlightComponentMod['renderToPipeableStream']>
> >
...@@ -581,7 +568,6 @@ export function renderToNodeFlightStream( ...@@ -581,7 +568,6 @@ export function renderToNodeFlightStream(
renderOptions renderOptions
) )
} catch (error) { } catch (error) {
localRenderTiming?.abort()
pt.destroy() pt.destroy()
throw error throw error
} }
...@@ -592,7 +578,6 @@ export function renderToNodeFlightStream( ...@@ -592,7 +578,6 @@ export function renderToNodeFlightStream(
// know; the listener is registered before piping so it runs before React's. // know; the listener is registered before piping so it runs before React's.
pt.once('close', () => { pt.once('close', () => {
if (!pt.writableFinished) { if (!pt.writableFinished) {
localRenderTiming?.abort()
pipeable.abort(new ResponseAborted()) pipeable.abort(new ResponseAborted())
} }
}) })
...@@ -600,20 +585,15 @@ export function renderToNodeFlightStream( ...@@ -600,20 +585,15 @@ export function renderToNodeFlightStream(
try { try {
pipeable.pipe(pt) pipeable.pipe(pt)
} catch (error) { } catch (error) {
localRenderTiming?.abort()
pt.destroy() pt.destroy()
throw error throw error
} }
if (signal) { if (signal) {
if (signal.aborted) { if (signal.aborted) {
localRenderTiming?.abort()
pipeable.abort(signal.reason) pipeable.abort(signal.reason)
} else { } else {
const onAbort = () => { const onAbort = () => pipeable.abort(signal.reason)
localRenderTiming?.abort()
pipeable.abort(signal.reason)
}
signal.addEventListener('abort', onAbort, { once: true }) signal.addEventListener('abort', onAbort, { once: true })
pt.on('close', () => signal.removeEventListener('abort', onAbort)) pt.on('close', () => signal.removeEventListener('abort', onAbort))
} }
......
...@@ -11,7 +11,6 @@ import type { PostponedState, PrerenderOptions } from 'react-dom/static' ...@@ -11,7 +11,6 @@ import type { PostponedState, PrerenderOptions } from 'react-dom/static'
import { resume, renderToReadableStream } from 'react-dom/server' import { resume, renderToReadableStream } from 'react-dom/server'
import { prerender } from 'react-dom/static' import { prerender } from 'react-dom/static'
import type { renderToReadableStream as flightRenderToReadableStream } from 'react-server-dom-webpack/server' import type { renderToReadableStream as flightRenderToReadableStream } from 'react-server-dom-webpack/server'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
import { import {
renderToInitialFizzStream, renderToInitialFizzStream,
...@@ -81,7 +80,7 @@ export type FlightClientModules = Parameters<FlightRenderToReadableStream>[1] ...@@ -81,7 +80,7 @@ export type FlightClientModules = Parameters<FlightRenderToReadableStream>[1]
*/ */
export type FlightRenderOptions = NonNullable< export type FlightRenderOptions = NonNullable<
Parameters<FlightRenderToReadableStream>[2] Parameters<FlightRenderToReadableStream>[2]
> & { localRenderTiming?: LocalRenderTiming } >
export type FizzStreamResult = { export type FizzStreamResult = {
stream: AnyStream stream: AnyStream
...@@ -253,74 +252,7 @@ export function renderToWebFlightStream( ...@@ -253,74 +252,7 @@ export function renderToWebFlightStream(
clientModules: FlightClientModules, clientModules: FlightClientModules,
opts: FlightRenderOptions opts: FlightRenderOptions
): AnyStream { ): AnyStream {
const { localRenderTiming, ...renderOptions } = opts return ComponentMod.renderToReadableStream(payload, clientModules, opts)
localRenderTiming?.start()
let stream: ReadableStream<Uint8Array>
try {
stream = ComponentMod.renderToReadableStream(
payload,
clientModules,
renderOptions
)
} catch (error) {
localRenderTiming?.abort()
throw error
}
if (!localRenderTiming) return stream
let reader: ReadableStreamDefaultReader<Uint8Array>
try {
reader = stream.getReader()
} catch (error) {
localRenderTiming.abort()
throw error
}
const signal = renderOptions.signal
const onAbort = () => localRenderTiming.abort()
if (signal?.aborted) onAbort()
else signal?.addEventListener('abort', onAbort, { once: true })
let closed = false
function release() {
signal?.removeEventListener('abort', onAbort)
reader.releaseLock()
}
return new ReadableStream<Uint8Array>(
{
async pull(controller) {
try {
const result = await reader.read()
if (closed) return
if (result.done) {
closed = true
localRenderTiming.finishFlight()
release()
controller.close()
} else {
localRenderTiming.readFlightChunk(result.value)
controller.enqueue(result.value)
}
} catch (error) {
if (closed) return
closed = true
localRenderTiming.abort()
release()
controller.error(error)
}
},
async cancel(reason) {
if (closed) return
closed = true
localRenderTiming.abort()
try {
await reader.cancel(reason)
} finally {
release()
}
},
},
{ highWaterMark: 0 }
)
} }
export async function streamToString(stream: AnyStream): Promise<string> { export async function streamToString(stream: AnyStream): Promise<string> {
......
...@@ -895,8 +895,16 @@ export default abstract class Server< ...@@ -895,8 +895,16 @@ export default abstract class Server<
'http.target': req.url, 'http.target': req.url,
}, },
}, },
async (span) => async (span) => {
this.handleRequestImpl(req, res, parsedUrl).finally(() => { if (process.env.__NEXT_DEV_SERVER) {
const { setRequestInsightRootParent } =
require('./lib/trace/request-insights') as typeof import('./lib/trace/request-insights')
setRequestInsightRootParent(
getRequestMeta(req, 'requestInsightsIdentity'),
span?.spanContext()
)
}
return this.handleRequestImpl(req, res, parsedUrl).finally(() => {
if (!span) return if (!span) return
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
...@@ -955,6 +963,7 @@ export default abstract class Server< ...@@ -955,6 +963,7 @@ export default abstract class Server<
span.updateName(isRSCRequest ? `RSC ${method}` : `${method}`) span.updateName(isRSCRequest ? `RSC ${method}` : `${method}`)
} }
}) })
}
) )
}) })
......
...@@ -145,6 +145,7 @@ import { handleErrorStateResponse } from '../mcp/tools/get-errors' ...@@ -145,6 +145,7 @@ import { handleErrorStateResponse } from '../mcp/tools/get-errors'
import { handlePageMetadataResponse } from '../mcp/tools/get-page-metadata' import { handlePageMetadataResponse } from '../mcp/tools/get-page-metadata'
import { setStackFrameResolver } from '../mcp/tools/utils/format-errors' import { setStackFrameResolver } from '../mcp/tools/utils/format-errors'
import { createRuntimeErrorStateHandler } from './runtime-error-state' import { createRuntimeErrorStateHandler } from './runtime-error-state'
import { createBrowserReactTimingReceiver } from '../lib/trace/browser-react-timings'
import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker' import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker'
import { getFileLogger } from './browser-logs/file-logger' import { getFileLogger } from './browser-logs/file-logger'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator' import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
...@@ -1506,6 +1507,10 @@ export async function createHotReloaderTurbopack( ...@@ -1506,6 +1507,10 @@ export async function createHotReloaderTurbopack(
const htmlRequestId = req.url const htmlRequestId = req.url
? new URL(req.url, 'http://n').searchParams.get('id') ? new URL(req.url, 'http://n').searchParams.get('id')
: null : null
const reactTimings = createBrowserReactTimingReceiver(
htmlRequestId,
nextConfig.experimental.requestInsights === true
)
// Clients with a request ID are inferred App Router clients. If Cache // Clients with a request ID are inferred App Router clients. If Cache
// Components is not enabled, we consider those legacy clients. Pages // Components is not enabled, we consider those legacy clients. Pages
...@@ -1565,6 +1570,7 @@ export async function createHotReloaderTurbopack( ...@@ -1565,6 +1570,7 @@ export async function createHotReloaderTurbopack(
: undefined : undefined
client.on('close', () => { client.on('close', () => {
reactTimings.dispose()
runtimeErrorStateHandler?.dispose() runtimeErrorStateHandler?.dispose()
// Remove active subscriptions // Remove active subscriptions
...@@ -1588,6 +1594,10 @@ export async function createHotReloaderTurbopack( ...@@ -1588,6 +1594,10 @@ export async function createHotReloaderTurbopack(
// Next.js messages // Next.js messages
switch (parsedData.event) { switch (parsedData.event) {
case HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS: {
reactTimings.receive(parsedData, data.toString().length)
break
}
case 'span-end': { case 'span-end': {
hotReloaderSpan.manualTraceChild( hotReloaderSpan.manualTraceChild(
parsedData.spanName, parsedData.spanName,
......
...@@ -57,6 +57,15 @@ export const enum HMR_MESSAGE_SENT_TO_SERVER { ...@@ -57,6 +57,15 @@ export const enum HMR_MESSAGE_SENT_TO_SERVER {
RUNTIME_ERRORS = 'runtimeErrors', RUNTIME_ERRORS = 'runtimeErrors',
MCP_PAGE_METADATA_RESPONSE = 'mcp-page-metadata-response', MCP_PAGE_METADATA_RESPONSE = 'mcp-page-metadata-response',
PING = 'ping', PING = 'ping',
REACT_DEBUG_TIMINGS = 'react-debug-timings',
}
export interface BrowserReactTimingsMessage {
event: HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS
requestId: string
decoderId: string
partial?: true
records: import('../../shared/lib/react-debug-timing').ReactTimingRecord[]
} }
export interface ServerErrorMessage { export interface ServerErrorMessage {
......
...@@ -78,8 +78,10 @@ import { ...@@ -78,8 +78,10 @@ import {
import { RouteKind } from '../route-kind' import { RouteKind } from '../route-kind'
import { import {
HMR_MESSAGE_SENT_TO_BROWSER, HMR_MESSAGE_SENT_TO_BROWSER,
HMR_MESSAGE_SENT_TO_SERVER,
type NextJsHotReloaderInterface, type NextJsHotReloaderInterface,
} from './hot-reloader-types' } from './hot-reloader-types'
import { createBrowserReactTimingReceiver } from '../lib/trace/browser-react-timings'
import type { HmrMessageSentToBrowser } from './hot-reloader-types' import type { HmrMessageSentToBrowser } from './hot-reloader-types'
import type { WebpackError } from 'webpack' import type { WebpackError } from 'webpack'
import { PAGE_TYPES } from '../../lib/page-types' import { PAGE_TYPES } from '../../lib/page-types'
...@@ -460,6 +462,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -460,6 +462,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
const htmlRequestId = req.url const htmlRequestId = req.url
? new URL(req.url, 'http://n').searchParams.get('id') ? new URL(req.url, 'http://n').searchParams.get('id')
: null : null
const reactTimings = createBrowserReactTimingReceiver(
htmlRequestId,
this.config.experimental.requestInsights === true
)
if (!this.webpackHotMiddleware) { if (!this.webpackHotMiddleware) {
throw new InvariantError('Did not start HotReloaderWebpack.') throw new InvariantError('Did not start HotReloaderWebpack.')
...@@ -498,6 +504,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -498,6 +504,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
| undefined | undefined
switch (payload.event) { switch (payload.event) {
case HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS: {
reactTimings.receive(payload, data.length)
break
}
case 'span-end': { case 'span-end': {
traceChild = { traceChild = {
name: payload.spanName, name: payload.spanName,
...@@ -676,6 +686,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -676,6 +686,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
} }
client.on('close', () => { client.on('close', () => {
reactTimings.dispose()
this.webpackHotMiddleware?.deleteClient(client, htmlRequestId) this.webpackHotMiddleware?.deleteClient(client, htmlRequestId)
if (htmlRequestId) { if (htmlRequestId) {
......
...@@ -90,6 +90,7 @@ import { ...@@ -90,6 +90,7 @@ import {
appendRequestInsightUpdateToJournal, appendRequestInsightUpdateToJournal,
closeRequestInsightsJournal, closeRequestInsightsJournal,
configureRequestInsightsJournal, configureRequestInsightsJournal,
getArchivedRequestInsightDebugContext,
getRequestInsightsHistory as getJournalHistory, getRequestInsightsHistory as getJournalHistory,
readRequestInsightsJournal, readRequestInsightsJournal,
} from '../lib/trace/request-insights-journal' } from '../lib/trace/request-insights-journal'
...@@ -289,6 +290,7 @@ export default class DevServer extends Server { ...@@ -289,6 +290,7 @@ export default class DevServer extends Server {
await configureRequestInsightsJournal(this.distDir) await configureRequestInsightsJournal(this.distDir)
configureRequestInsightsHistoryProvider({ configureRequestInsightsHistoryProvider({
append: appendRequestInsightToJournal, append: appendRequestInsightToJournal,
getDebugRequest: getArchivedRequestInsightDebugContext,
appendUpdate: appendRequestInsightUpdateToJournal, appendUpdate: appendRequestInsightUpdateToJournal,
appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal, appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal,
getHistory: (query) => getJournalHistory(this.distDir, query), getHistory: (query) => getJournalHistory(this.distDir, query),
......
import { HMR_MESSAGE_SENT_TO_SERVER } from '../../dev/hot-reloader-types'
import type { BrowserReactTimingsMessage } from '../../dev/hot-reloader-types'
import type { ReactTimingRecord } from '../../../shared/lib/react-debug-timing'
import {
copyBrowserReactTimingRecord,
isBrowserReactTimingRecord,
MAX_BROWSER_REACT_TIMING_BATCH,
MAX_BROWSER_REACT_TIMING_MESSAGE_LENGTH,
} from '../../../shared/lib/react-debug-timing-transport'
import {
getValidatedDevHtmlRequestId,
getValidatedDevRequestId,
} from '../dev-request-id'
import {
getRequestInsightForDebugRequest,
isRequestInsightsEnabled,
} from './request-insights'
import { createReactTimingSpanRecords } from './react-render-timing'
import { createLocalSpanId } from './local-span-recorder'
import { recordSpans } from './span-store'
type Decoder = {
renderId: string
highestRecordId: number
partialReported: boolean
}
export function createBrowserReactTimingReceiver(
htmlRequestId: string | null,
enabled: boolean
) {
const documentId = getValidatedDevHtmlRequestId(htmlRequestId ?? undefined)
const decoders = new Map<string, Decoder>()
let received = 0
let windowStart = Date.now()
return {
receive(value: unknown, messageLength: number) {
if (
!enabled ||
!documentId ||
!isRequestInsightsEnabled() ||
!Number.isSafeInteger(messageLength) ||
messageLength < 0 ||
messageLength > MAX_BROWSER_REACT_TIMING_MESSAGE_LENGTH ||
!value ||
typeof value !== 'object'
) {
return
}
const message = value as BrowserReactTimingsMessage
if (
message.event !== HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS ||
(!getValidatedDevRequestId(message.requestId) &&
message.requestId !== documentId) ||
typeof message.decoderId !== 'string' ||
!/^\d{1,16}$/.test(message.decoderId) ||
!Array.isArray(message.records) ||
(message.records.length === 0 && message.partial !== true) ||
(message.partial !== undefined && message.partial !== true) ||
message.records.length > MAX_BROWSER_REACT_TIMING_BATCH ||
!message.records.every(isBrowserReactTimingRecord)
) {
return
}
const target = getRequestInsightForDebugRequest(
message.requestId,
documentId
)
if (!target?.parent) return
const { identity, insight, parent } = target
const now = Date.now()
if (now - windowStart >= 60_000) {
windowStart = now
received = 0
}
received += Math.max(1, message.records.length)
const key = `${identity.requestId}:${message.decoderId}`
let decoder = decoders.get(key)
const rateLimited = received > 8192
if (rateLimited && !decoder) return
if (!decoder) {
if (decoders.size >= 128) {
decoders.delete(decoders.keys().next().value!)
}
decoder = {
renderId: createLocalSpanId(),
highestRecordId: -1,
partialReported: false,
}
} else {
decoders.delete(key)
}
decoders.set(key, decoder)
const records: ReactTimingRecord[] = []
if (!rateLimited) {
for (const record of message.records) {
const id = Number(record.id.slice(8))
if (id <= decoder.highestRecordId) continue
decoder.highestRecordId = id
records.push(copyBrowserReactTimingRecord(record))
}
}
const context = {
requestId: identity.requestId,
requestInsightKind: identity.kind,
requestInsightSource: insight?.source ?? identity.source,
requestInsightProxyStatus: insight?.proxyStatus ?? identity.proxyStatus,
htmlRequestId: identity.htmlRequestId,
route: insight?.route ?? identity.route,
url: insight?.url ?? identity.url,
traceId: parent.traceId,
parentSpanId: parent.spanId,
}
const spans = createReactTimingSpanRecords(
records,
context,
decoder.renderId
)
if ((message.partial || rateLimited) && !decoder.partialReported) {
decoder.partialReported = true
spans.push({
...context,
name: 'ReactServerComponents.incomplete',
spanId: createLocalSpanId(parent.spanId),
timestamp: performance.timeOrigin + performance.now(),
attributes: {
'next.span_type': 'ReactServerComponents.incomplete',
'next.span_name': 'Some decoded React observations were dropped',
'next.span_category': 'application',
'next.rsc.render_id': decoder.renderId,
'next.rsc.observation': 'decoded',
'next.rsc.observation_partial': true,
},
})
}
if (spans.length > 0) recordSpans(spans)
},
dispose() {
decoders.clear()
},
}
}
import { getTracer } from './tracer'
import { import {
captureLocalSpanContext, type captureLocalSpanContext,
createLocalSpanId, createLocalSpanId,
} from './local-span-recorder' } from './local-span-recorder'
import { import type { SpanStoreRecord } from './span-store'
captureLocalSpanRecorder, import type { ReactTimingRecord } from '../../../shared/lib/react-debug-timing'
isLocalSpanRecordingEnabled,
} from './span-store'
import { createReactTimingCollector } from './react-debug-timing'
import { warnOnce } from '../../../build/output/log'
export type LocalRenderTiming = NonNullable<
ReturnType<typeof createLocalRenderTiming>
>
export function createLocalRenderTiming() {
if (!isLocalSpanRecordingEnabled()) return undefined
let collector: ReturnType<typeof createReactTimingCollector> | undefined
let flightFinished = false
let debugFinished = false
let closed = false
function finish() {
if (flightFinished && debugFinished) {
closed = true
collector?.finish()
collector = undefined
}
}
return {
start() {
if (closed || collector) return
try {
const span = getTracer().getActiveScopeSpan()
const parent = span?.spanContext()
if (!parent) return
const context = captureLocalSpanContext(parent)
const record = captureLocalSpanRecorder()
const renderId = createLocalSpanId(parent.spanId)
const startTime = performance.timeOrigin + performance.now()
collector = createReactTimingCollector( export function createReactTimingSpanRecords(
(timings) => { timings: readonly ReactTimingRecord[],
record( context: ReturnType<typeof captureLocalSpanContext>,
timings.map((timing) => ({ renderId: string
...context, ): SpanStoreRecord[] {
name: `ReactServerComponents.${timing.kind}`, return timings.map((timing) => ({
spanId: createLocalSpanId(parent.spanId), ...context,
timestamp: timing.startTime + timing.durationMs, name: `ReactServerComponents.${timing.kind}`,
startTime: timing.startTime, spanId: createLocalSpanId(context.parentSpanId),
durationMs: timing.durationMs, timestamp: timing.startTime + timing.durationMs,
status: 'ok', startTime: timing.startTime,
attributes: { durationMs: timing.durationMs,
'next.span_type': `ReactServerComponents.${timing.kind}`, status:
'next.span_name': timing.outcome === 'errored'
timing.kind === 'component' ? 'error'
? `render ${timing.name}` : timing.outcome === 'completed'
: `await ${timing.name}`, ? 'ok'
'next.span_category': 'application', : undefined,
'next.rsc.kind': timing.kind, attributes: {
'next.rsc.render_id': renderId, 'next.span_type': `ReactServerComponents.${timing.kind}`,
'next.rsc.environment': timing.environment, 'next.span_name': `${timing.kind === 'component' ? 'render' : 'await'} ${timing.name}`,
...(timing.source 'next.span_category': 'application',
? { 'next.rsc.kind': timing.kind,
'next.rsc.source.file': timing.source.file, 'next.rsc.render_id': renderId,
'next.rsc.source.line': timing.source.line, 'next.rsc.environment': timing.environment,
'next.rsc.source.column': timing.source.column, 'next.rsc.observation': 'decoded',
'next.rsc.source.name': timing.source.methodName, ...(timing.outcome ? { 'next.rsc.outcome': timing.outcome } : {}),
} 'next.rsc.timing':
: {}), timing.kind === 'component' ? 'render-interval' : 'await',
...(timing.ownerName ...(timing.source
? { 'next.rsc.owner': timing.ownerName } ? {
: {}), 'next.rsc.source.file': timing.source.file,
...(timing.componentPath 'next.rsc.source.line': timing.source.line,
? { 'next.rsc.component_path': timing.componentPath } 'next.rsc.source.column': timing.source.column,
: {}), 'next.rsc.source.name': timing.source.methodName,
'next.rsc.timing':
timing.kind === 'component' ? 'render-interval' : 'await',
},
}))
)
},
(reason) => {
record([
{
...context,
name: 'ReactServerComponents.incomplete',
spanId: createLocalSpanId(parent.spanId),
timestamp: startTime,
startTime,
durationMs: 0,
status: 'ok',
attributes: {
'next.span_type': 'ReactServerComponents.incomplete',
'next.rsc.render_id': renderId,
'next.rsc.incomplete_reason': reason,
},
},
])
warnOnce(
reason === 'budget'
? 'React render timing collection reached its diagnostic budget. Request Insights shows only the recorded intervals.'
: 'Some React render timings could not be decoded. Request Insights shows only the recorded intervals.'
)
} }
) : {}),
} catch { ...(timing.ownerName ? { 'next.rsc.owner': timing.ownerName } : {}),
closed = true ...(timing.componentPath
} ? { 'next.rsc.component_path': timing.componentPath }
}, : {}),
readFlightChunk(chunk: Uint8Array | string) {
collector?.readFlightChunk(chunk)
},
readDebugChunk(chunk: Uint8Array | string) {
collector?.readDebugChunk(chunk)
},
finishFlight() {
flightFinished = true
finish()
},
finishDebug() {
debugFinished = true
finish()
},
abort() {
closed = true
collector?.abort()
collector = undefined
}, },
} }))
} }
...@@ -10,7 +10,10 @@ import { ...@@ -10,7 +10,10 @@ import {
REQUEST_INSIGHT_REQUEST_SPAN_TYPE, REQUEST_INSIGHT_REQUEST_SPAN_TYPE,
type RequestInsight, type RequestInsight,
} from '../../../shared/lib/request-insights' } from '../../../shared/lib/request-insights'
import type { RequestInsightUpdate } from './request-insights' import type {
RequestInsightDebugContext,
RequestInsightUpdate,
} from './request-insights'
import { import {
getRequestInsightTags, getRequestInsightTags,
matchesRequestInsightFilters, matchesRequestInsightFilters,
...@@ -51,6 +54,7 @@ type HistoryFilter = { ...@@ -51,6 +54,7 @@ type HistoryFilter = {
type IndexedSummary = { type IndexedSummary = {
sequence: number sequence: number
request: RequestInsightSummary request: RequestInsightSummary
debugContext?: RequestInsightDebugContext
} }
type PendingAppend = { type PendingAppend = {
...@@ -58,6 +62,7 @@ type PendingAppend = { ...@@ -58,6 +62,7 @@ type PendingAppend = {
byteLength: number byteLength: number
key: string key: string
summary: RequestInsightSummary summary: RequestInsightSummary
debugContext?: RequestInsightDebugContext
} }
type AppendBatch = { type AppendBatch = {
...@@ -77,14 +82,21 @@ class RequestInsightsJournal { ...@@ -77,14 +82,21 @@ class RequestInsightsJournal {
private size = 0 private size = 0
private sequence = 0 private sequence = 0
private summaries = new Map<string, IndexedSummary>() private summaries = new Map<string, IndexedSummary>()
private pendingRequests = new Set<string>() private pendingRequests = new Map<
string,
RequestInsightDebugContext | undefined
>()
private debugRequests = new Map<string, RequestInsightDebugContext | null>()
private sessionId = createSessionId() private sessionId = createSessionId()
private generation = 0 private generation = 0
private truncated = false private truncated = false
constructor(readonly file: string) {} constructor(readonly file: string) {}
append(request: RequestInsight): void { append(
request: RequestInsight,
debugContext?: RequestInsightDebugContext
): void {
let line: string let line: string
try { try {
line = `${JSON.stringify({ version: 1, request } satisfies JournalRecord)}\n` line = `${JSON.stringify({ version: 1, request } satisfies JournalRecord)}\n`
...@@ -95,7 +107,7 @@ class RequestInsightsJournal { ...@@ -95,7 +107,7 @@ class RequestInsightsJournal {
const byteLength = Buffer.byteLength(line, 'utf8') const byteLength = Buffer.byteLength(line, 'utf8')
const summary = summarizeRequestInsight(request) const summary = summarizeRequestInsight(request)
const key = getRequestInsightKey(request) const key = getRequestInsightKey(request)
this.pendingRequests.add(key) this.pendingRequests.set(key, debugContext)
let batch = this.pendingAppends let batch = this.pendingAppends
if (!batch || batch.byteLength + byteLength > JOURNAL_WRITE_BATCH_BYTES) { if (!batch || batch.byteLength + byteLength > JOURNAL_WRITE_BATCH_BYTES) {
batch = { records: [], byteLength: 0 } batch = { records: [], byteLength: 0 }
...@@ -116,7 +128,7 @@ class RequestInsightsJournal { ...@@ -116,7 +128,7 @@ class RequestInsightsJournal {
} }
}) })
} }
batch.records.push({ line, byteLength, key, summary }) batch.records.push({ line, byteLength, key, summary, debugContext })
batch.byteLength += byteLength batch.byteLength += byteLength
} }
...@@ -148,11 +160,55 @@ class RequestInsightsJournal { ...@@ -148,11 +160,55 @@ class RequestInsightsJournal {
this.summaries.set(record.key, { this.summaries.set(record.key, {
sequence: ++this.sequence, sequence: ++this.sequence,
request: record.summary, request: record.summary,
debugContext: record.debugContext,
}) })
this.indexDebugRequest(record.debugContext)
} }
} }
} }
private indexDebugRequest(
context: RequestInsightDebugContext | undefined
): void {
if (!context) return
const key = `${context.identity.htmlRequestId}:${context.debugRequestId}`
const previous = this.debugRequests.get(key)
this.debugRequests.set(
key,
previous === null ||
(previous &&
getRequestInsightKey(previous.identity) !==
getRequestInsightKey(context.identity))
? null
: context
)
}
getDebugRequest(
debugRequestId: string,
htmlRequestId: string
): RequestInsightDebugContext | null | undefined {
let context = this.debugRequests.get(`${htmlRequestId}:${debugRequestId}`)
// A completed request can leave the live window before its queued journal
// append runs. Keep its authoritative parent available during that write.
for (const pending of this.pendingRequests.values()) {
if (
pending?.debugRequestId !== debugRequestId ||
pending.identity.htmlRequestId !== htmlRequestId
)
continue
if (
context === null ||
(context &&
getRequestInsightKey(context.identity) !==
getRequestInsightKey(pending.identity))
)
return null
context = pending
}
return context
}
appendArchivedUpdate( appendArchivedUpdate(
identity: Pick<RequestInsight, 'requestId' | 'kind'>, identity: Pick<RequestInsight, 'requestId' | 'kind'>,
update: RequestInsightUpdate update: RequestInsightUpdate
...@@ -182,7 +238,8 @@ class RequestInsightsJournal { ...@@ -182,7 +238,8 @@ class RequestInsightsJournal {
const fetchCount = request?.fetches.length const fetchCount = request?.fetches.length
this.writes = this.writes this.writes = this.writes
.then(async () => { .then(async () => {
const previous = this.summaries.get(key)?.request const previousEntry = this.summaries.get(key)
const previous = previousEntry?.request
if (!previous && !snapshot) { if (!previous && !snapshot) {
return return
} }
...@@ -276,7 +333,9 @@ class RequestInsightsJournal { ...@@ -276,7 +333,9 @@ class RequestInsightsJournal {
this.summaries.set(key, { this.summaries.set(key, {
sequence: this.summaries.get(key)?.sequence ?? ++this.sequence, sequence: this.summaries.get(key)?.sequence ?? ++this.sequence,
request: updatedSummary, request: updatedSummary,
debugContext: previousEntry?.debugContext,
}) })
this.indexDebugRequest(previousEntry?.debugContext)
}) })
.catch((error) => { .catch((error) => {
console.warn('Failed to write Request Insights journal', error) console.warn('Failed to write Request Insights journal', error)
...@@ -287,6 +346,7 @@ class RequestInsightsJournal { ...@@ -287,6 +346,7 @@ class RequestInsightsJournal {
await writeFile(this.file, '') await writeFile(this.file, '')
this.size = 0 this.size = 0
this.summaries.clear() this.summaries.clear()
this.debugRequests.clear()
this.generation++ this.generation++
this.truncated = true this.truncated = true
} }
...@@ -298,6 +358,7 @@ class RequestInsightsJournal { ...@@ -298,6 +358,7 @@ class RequestInsightsJournal {
this.size = 0 this.size = 0
this.sequence = 0 this.sequence = 0
this.summaries.clear() this.summaries.clear()
this.debugRequests.clear()
this.sessionId = createSessionId() this.sessionId = createSessionId()
this.generation = 0 this.generation = 0
this.truncated = false this.truncated = false
...@@ -415,8 +476,21 @@ type JournalRegistry = { ...@@ -415,8 +476,21 @@ type JournalRegistry = {
journals: Map<string, RequestInsightsJournal> journals: Map<string, RequestInsightsJournal>
} }
export function appendRequestInsightToJournal(request: RequestInsight): void { export function appendRequestInsightToJournal(
getJournalRegistry().configured?.append(request) request: RequestInsight,
debugContext?: RequestInsightDebugContext
): void {
getJournalRegistry().configured?.append(request, debugContext)
}
export function getArchivedRequestInsightDebugContext(
debugRequestId: string,
htmlRequestId: string
): RequestInsightDebugContext | null | undefined {
return getJournalRegistry().configured?.getDebugRequest(
debugRequestId,
htmlRequestId
)
} }
export function appendRequestInsightUpdateToJournal( export function appendRequestInsightUpdateToJournal(
......
...@@ -4,6 +4,8 @@ import { ...@@ -4,6 +4,8 @@ import {
completeRequestInsight, completeRequestInsight,
configureRequestInsightsHistoryProvider, configureRequestInsightsHistoryProvider,
getRequestInsightsSnapshot, getRequestInsightsSnapshot,
getRequestInsightForDebugRequest,
setRequestInsightRootParent,
importRequestInsightSpans, importRequestInsightSpans,
recordRequestInsightFetch, recordRequestInsightFetch,
registerRequestInsightsExporter, registerRequestInsightsExporter,
...@@ -17,15 +19,21 @@ import { ...@@ -17,15 +19,21 @@ import {
appendRequestInsightUpdateToJournal, appendRequestInsightUpdateToJournal,
closeRequestInsightsJournal, closeRequestInsightsJournal,
configureRequestInsightsJournal, configureRequestInsightsJournal,
getArchivedRequestInsightDebugContext,
getRequestInsightsHistory, getRequestInsightsHistory,
initializeRequestInsightsJournal, initializeRequestInsightsJournal,
readRequestInsightsJournal, readRequestInsightsJournal,
resetRequestInsightsJournalForTest, resetRequestInsightsJournalForTest,
StaleRequestInsightsHistoryCursorError, StaleRequestInsightsHistoryCursorError,
} from './request-insights-journal' } from './request-insights-journal'
import { recordSpan } from './span-store' import { recordSpan, recordSpans } from './span-store'
import { createLocalRenderTiming } from './react-render-timing' import { createReactTimingSpanRecords } from './react-render-timing'
import { registerLocalSpanRecorder } from './local-span-recorder' import { createBrowserReactTimingReceiver } from './browser-react-timings'
import { HMR_MESSAGE_SENT_TO_SERVER } from '../../dev/hot-reloader-types'
import {
captureLocalSpanContext,
registerLocalSpanRecorder,
} from './local-span-recorder'
import { AppRenderSpan } from './constants' import { AppRenderSpan } from './constants'
import { getTracer } from './tracer' import { getTracer } from './tracer'
import { import {
...@@ -723,6 +731,10 @@ describe('request insights', () => { ...@@ -723,6 +731,10 @@ describe('request insights', () => {
createRequestId: () => requestId, createRequestId: () => requestId,
}) })
recordSpan({ name: 'original', requestId }) recordSpan({ name: 'original', requestId })
setRequestInsightRootParent(identity, {
traceId: 'trace',
spanId: 'parent',
})
completeRequestInsight(identity) completeRequestInsight(identity)
} }
...@@ -739,6 +751,9 @@ describe('request insights', () => { ...@@ -739,6 +751,9 @@ describe('request insights', () => {
requestId: 'discarded', requestId: 'discarded',
}) })
).toHaveLength(1) ).toHaveLength(1)
expect(
getRequestInsightForDebugRequest('discarded', 'discarded')
).toBeDefined()
const fs = require('fs/promises') as typeof import('fs/promises') const fs = require('fs/promises') as typeof import('fs/promises')
const currentStat = await fs.stat( const currentStat = await fs.stat(
...@@ -757,6 +772,9 @@ describe('request insights', () => { ...@@ -757,6 +772,9 @@ describe('request insights', () => {
requestId: 'discarded', requestId: 'discarded',
}) })
).toHaveLength(0) ).toHaveLength(0)
expect(
getRequestInsightForDebugRequest('discarded', 'discarded')
).toBeUndefined()
if (update === 'span') { if (update === 'span') {
recordSpan({ name: 'late work', requestId: 'discarded' }) recordSpan({ name: 'late work', requestId: 'discarded' })
...@@ -912,13 +930,13 @@ describe('request insights', () => { ...@@ -912,13 +930,13 @@ describe('request insights', () => {
await initializeRequestInsightsJournal(distDir) await initializeRequestInsightsJournal(distDir)
configureJournalProvider(distDir) configureJournalProvider(distDir)
startRequestInsight(identity) startRequestInsight(identity)
let render!: NonNullable<ReturnType<typeof createLocalRenderTiming>> let context!: ReturnType<typeof captureLocalSpanContext>
let parentSpanId!: string let parentSpanId!: string
runWithRequestInsightsIdentity(identity, () => runWithRequestInsightsIdentity(identity, () =>
getTracer().trace(AppRenderSpan.renderToReadableStream, (span) => { getTracer().trace(AppRenderSpan.renderToReadableStream, (span) => {
parentSpanId = span!.spanContext().spanId parentSpanId = span!.spanContext().spanId
render = createLocalRenderTiming()! context = captureLocalSpanContext(span!.spanContext())
render.start() setRequestInsightRootParent(identity, span!.spanContext())
}) })
) )
completeRequestInsight(identity) completeRequestInsight(identity)
...@@ -928,11 +946,22 @@ describe('request insights', () => { ...@@ -928,11 +946,22 @@ describe('request insights', () => {
durationMs: completed.durationMs, durationMs: completed.durationMs,
} }
render.readFlightChunk('0:D"$1"\n0:D"$2"\n0:D"$3"\n') const component = {
render.readDebugChunk( id: 'decoded:0',
':N1000\n1:{"time":2}\n2:{"name":"Page","env":"Server","owner":"$5","stack":[["Layout","file:///app/layout.tsx",12,7]]}\n3:{"time":5}\n5:{"name":"Layout"}\n' kind: 'component' as const,
) name: 'Page',
await Promise.resolve() environment: 'Server',
componentPath: 'Layout › Page',
source: {
methodName: 'Layout',
file: 'file:///app/layout.tsx',
line: 12,
column: 7,
},
startTime: 2,
durationMs: 3,
}
recordSpans(createReactTimingSpanRecords([component], context, 'render'))
expect(getRequestInsightsSnapshot().requests[0]).toMatchObject({ expect(getRequestInsightsSnapshot().requests[0]).toMatchObject({
...responseTiming, ...responseTiming,
spans: expect.arrayContaining([ spans: expect.arrayContaining([
...@@ -946,10 +975,22 @@ describe('request insights', () => { ...@@ -946,10 +975,22 @@ describe('request insights', () => {
recordSpan({ name: 'GET /', ...other }) recordSpan({ name: 'GET /', ...other })
completeRequestInsight(other) completeRequestInsight(other)
} }
render.readFlightChunk('f:D"$1"\nf:D"$2"\nf:D"$3"\n') expect(
render.readDebugChunk('4:{"name":"' + 'x'.repeat(1024 * 1024)) getRequestInsightForDebugRequest('react-stream', 'react-stream')
render.finishFlight() ).toMatchObject({ parent: { spanId: parentSpanId } })
render.finishDebug() await readRequestInsightsJournal(distDir, identity)
expect(
getRequestInsightForDebugRequest('react-stream', 'react-stream')
).toMatchObject({ parent: { spanId: parentSpanId } })
const receiver = createBrowserReactTimingReceiver('react-stream', true)
const message = {
event: HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS,
requestId: 'react-stream',
decoderId: '0',
records: [{ ...component, id: 'decoded:1', startTime: 6 }],
}
receiver.receive(message, JSON.stringify(message).length)
receiver.dispose()
const [stored] = await readRequestInsightsJournal(distDir, identity) const [stored] = await readRequestInsightsJournal(distDir, identity)
expect(stored).toMatchObject(responseTiming) expect(stored).toMatchObject(responseTiming)
...@@ -957,19 +998,6 @@ describe('request insights', () => { ...@@ -957,19 +998,6 @@ describe('request insights', () => {
(span) => span.name === 'ReactServerComponents.component' (span) => span.name === 'ReactServerComponents.component'
) )
expect(intervals).toHaveLength(2) expect(intervals).toHaveLength(2)
expect(stored.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'ReactServerComponents.incomplete',
parentSpanId,
attributes: expect.objectContaining({
'next.rsc.render_id':
intervals[0].attributes!['next.rsc.render_id'],
'next.rsc.incomplete_reason': 'budget',
}),
}),
])
)
for (const interval of intervals) { for (const interval of intervals) {
expect(interval).toMatchObject({ expect(interval).toMatchObject({
parentSpanId, parentSpanId,
...@@ -1672,6 +1700,7 @@ describe('request insights', () => { ...@@ -1672,6 +1700,7 @@ describe('request insights', () => {
function configureJournalProvider(distDir: string): void { function configureJournalProvider(distDir: string): void {
configureRequestInsightsHistoryProvider({ configureRequestInsightsHistoryProvider({
append: appendRequestInsightToJournal, append: appendRequestInsightToJournal,
getDebugRequest: getArchivedRequestInsightDebugContext,
appendUpdate: appendRequestInsightUpdateToJournal, appendUpdate: appendRequestInsightUpdateToJournal,
appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal, appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal,
getHistory: (query) => getRequestInsightsHistory(distDir, query), getHistory: (query) => getRequestInsightsHistory(distDir, query),
......
...@@ -41,6 +41,17 @@ const REQUEST_INSIGHTS_HISTORY_PROVIDER_KEY = Symbol.for( ...@@ -41,6 +41,17 @@ const REQUEST_INSIGHTS_HISTORY_PROVIDER_KEY = Symbol.for(
const CLIENT_COMPONENT_LOADING_SPAN_TYPE = const CLIENT_COMPONENT_LOADING_SPAN_TYPE =
'NextNodeServer.clientComponentLoading' 'NextNodeServer.clientComponentLoading'
type DebugRequestIdentity = RequestInsightIdentity & {
debugRequestId?: string
htmlRequestId?: string
}
export type RequestInsightDebugContext = {
debugRequestId: string
identity: DebugRequestIdentity & { requestId: string }
parent: LocalSpanParent | undefined
}
type RequestInsightsListener = (insight: RequestInsight) => void type RequestInsightsListener = (insight: RequestInsight) => void
type RequestInsightIdentity = Readonly<{ type RequestInsightIdentity = Readonly<{
requestId?: string requestId?: string
...@@ -68,7 +79,14 @@ export type RequestInsightsJournalQuery = { ...@@ -68,7 +79,14 @@ export type RequestInsightsJournalQuery = {
} }
export type RequestInsightsHistoryProvider = { export type RequestInsightsHistoryProvider = {
append(request: RequestInsight): void append(
request: RequestInsight,
debugContext?: RequestInsightDebugContext
): void
getDebugRequest?(
debugRequestId: string,
htmlRequestId: string
): RequestInsightDebugContext | null | undefined
appendUpdate?(request: RequestInsight, update: RequestInsightUpdate): void appendUpdate?(request: RequestInsight, update: RequestInsightUpdate): void
appendArchivedUpdate?( appendArchivedUpdate?(
identity: Pick<RequestInsight, 'requestId' | 'kind'>, identity: Pick<RequestInsight, 'requestId' | 'kind'>,
...@@ -105,6 +123,9 @@ const SAFE_SPAN_ATTRIBUTE_KEYS = new Set([ ...@@ -105,6 +123,9 @@ const SAFE_SPAN_ATTRIBUTE_KEYS = new Set([
'next.rsc.owner', 'next.rsc.owner',
'next.rsc.component_path', 'next.rsc.component_path',
'next.rsc.environment', 'next.rsc.environment',
'next.rsc.observation',
'next.rsc.outcome',
'next.rsc.observation_partial',
'next.rsc.timing', 'next.rsc.timing',
'next.rsc.source.file', 'next.rsc.source.file',
'next.rsc.source.line', 'next.rsc.source.line',
...@@ -125,8 +146,14 @@ class InMemoryRequestInsightsStore { ...@@ -125,8 +146,14 @@ class InMemoryRequestInsightsStore {
private readonly requestOrder: string[] = [] private readonly requestOrder: string[] = []
private readonly completedRequestOrder: string[] = [] private readonly completedRequestOrder: string[] = []
private readonly listeners = new Set<RequestInsightsListener>() private readonly listeners = new Set<RequestInsightsListener>()
private readonly debugRequestKeys = new Map<string, string>()
private readonly debugRequestParents = new Map<string, LocalSpanParent>()
private readonly debugRequests = new Map<
string,
{ requests: Map<string, DebugRequestIdentity>; ambiguous: boolean }
>()
startRequest(identity: RequestInsightIdentity): void { startRequest(identity: DebugRequestIdentity): void {
if (!identity.requestId) { if (!identity.requestId) {
return return
} }
...@@ -134,6 +161,28 @@ class InMemoryRequestInsightsStore { ...@@ -134,6 +161,28 @@ class InMemoryRequestInsightsStore {
requestId: identity.requestId, requestId: identity.requestId,
kind: identity.kind, kind: identity.kind,
}) })
const debugRequestId =
identity.debugRequestId ??
(identity.requestId === identity.htmlRequestId
? identity.requestId
: undefined)
if (
debugRequestId &&
identity.htmlRequestId &&
getRequestInsightKind(identity) === 'request'
) {
const debugKey = `${identity.htmlRequestId}:${debugRequestId}`
let entry = this.debugRequests.get(debugKey)
if (!entry) {
entry = { requests: new Map(), ambiguous: false }
this.debugRequests.set(debugKey, entry)
}
if (entry.requests.size > 0 && !entry.requests.has(insightKey)) {
entry.ambiguous = true
}
entry.requests.set(insightKey, identity)
this.debugRequestKeys.set(insightKey, debugKey)
}
if (this.requests.get(insightKey)?.completedAt === undefined) { if (this.requests.get(insightKey)?.completedAt === undefined) {
this.activeRequests.add(insightKey) this.activeRequests.add(insightKey)
} }
...@@ -321,6 +370,50 @@ class InMemoryRequestInsightsStore { ...@@ -321,6 +370,50 @@ class InMemoryRequestInsightsStore {
} }
} }
getForDebugRequest(debugRequestId: string, htmlRequestId: string) {
const entry = this.debugRequests.get(`${htmlRequestId}:${debugRequestId}`)
if (entry?.ambiguous) return undefined
const archived = getRequestInsightsHistoryProvider()?.getDebugRequest?.(
debugRequestId,
htmlRequestId
)
if (archived === null) return undefined
if (!entry) return archived && { ...archived, insight: undefined }
if (entry.requests.size !== 1) return undefined
const [insightKey, identity] = entry.requests.entries().next().value!
if (archived && getRequestInsightKey(archived.identity) !== insightKey) {
return undefined
}
return {
identity,
insight: this.requests.get(insightKey),
parent: this.debugRequestParents.get(insightKey),
}
}
setDebugRequestParent(
identity: RequestInsightIdentity,
parent: LocalSpanParent
) {
if (
!identity.requestId ||
identity.source === 'proxy' ||
!parent.traceId ||
!parent.spanId
)
return
const key = getRequestInsightKey({
requestId: identity.requestId,
kind: identity.kind,
})
if (this.debugRequestKeys.has(key) && !this.debugRequestParents.has(key)) {
this.debugRequestParents.set(key, {
traceId: parent.traceId,
spanId: parent.spanId,
})
}
}
subscribe(listener: RequestInsightsListener): () => void { subscribe(listener: RequestInsightsListener): () => void {
this.listeners.add(listener) this.listeners.add(listener)
return () => { return () => {
...@@ -334,6 +427,9 @@ class InMemoryRequestInsightsStore { ...@@ -334,6 +427,9 @@ class InMemoryRequestInsightsStore {
this.requestTimings.clear() this.requestTimings.clear()
this.requestOrder.length = 0 this.requestOrder.length = 0
this.completedRequestOrder.length = 0 this.completedRequestOrder.length = 0
this.debugRequestKeys.clear()
this.debugRequestParents.clear()
this.debugRequests.clear()
} }
private updateTiming( private updateTiming(
...@@ -462,7 +558,25 @@ class InMemoryRequestInsightsStore { ...@@ -462,7 +558,25 @@ class InMemoryRequestInsightsStore {
this.activeRequests.delete(insightKey) this.activeRequests.delete(insightKey)
insight.completedAt = completedAt insight.completedAt = completedAt
this.completedRequestOrder.push(insightKey) this.completedRequestOrder.push(insightKey)
appendCompletedRequestInsight(insight) const completedDebugKey = this.debugRequestKeys.get(insightKey)
const identity =
completedDebugKey &&
this.debugRequests.get(completedDebugKey)?.requests.get(insightKey)
appendCompletedRequestInsight(
insight,
identity
? {
debugRequestId: identity.debugRequestId ?? identity.requestId!,
identity: {
...identity,
requestId: insight.requestId,
route: insight.route,
url: insight.url,
},
parent: this.debugRequestParents.get(insightKey),
}
: undefined
)
const requestIndex = this.requestOrder.indexOf(insightKey) const requestIndex = this.requestOrder.indexOf(insightKey)
if (requestIndex !== -1) { if (requestIndex !== -1) {
...@@ -475,6 +589,14 @@ class InMemoryRequestInsightsStore { ...@@ -475,6 +589,14 @@ class InMemoryRequestInsightsStore {
) { ) {
const completedInsightKey = this.completedRequestOrder.shift() const completedInsightKey = this.completedRequestOrder.shift()
if (completedInsightKey) { if (completedInsightKey) {
const debugKey = this.debugRequestKeys.get(completedInsightKey)
if (debugKey) {
const entry = this.debugRequests.get(debugKey)
entry?.requests.delete(completedInsightKey)
if (entry?.requests.size === 0) this.debugRequests.delete(debugKey)
this.debugRequestKeys.delete(completedInsightKey)
this.debugRequestParents.delete(completedInsightKey)
}
this.requests.delete(completedInsightKey) this.requests.delete(completedInsightKey)
this.requestTimings.delete(completedInsightKey) this.requestTimings.delete(completedInsightKey)
const completedIndex = this.requestOrder.indexOf(completedInsightKey) const completedIndex = this.requestOrder.indexOf(completedInsightKey)
...@@ -517,10 +639,13 @@ function sanitizeRecordedSpan(span: SpanStoreRecord): RequestInsightSpan { ...@@ -517,10 +639,13 @@ function sanitizeRecordedSpan(span: SpanStoreRecord): RequestInsightSpan {
} }
} }
function appendCompletedRequestInsight(insight: RequestInsight): void { function appendCompletedRequestInsight(
insight: RequestInsight,
debugContext: RequestInsightDebugContext | undefined
): void {
if (process.env.__NEXT_DEV_SERVER) { if (process.env.__NEXT_DEV_SERVER) {
if (process.env.__NEXT_REQUEST_INSIGHTS) { if (process.env.__NEXT_REQUEST_INSIGHTS) {
getRequestInsightsHistoryProvider()?.append(insight) getRequestInsightsHistoryProvider()?.append(insight, debugContext)
} }
} else { } else {
return return
...@@ -639,12 +764,32 @@ export function completeRequestInsight(identity: RequestInsightIdentity): void { ...@@ -639,12 +764,32 @@ export function completeRequestInsight(identity: RequestInsightIdentity): void {
getRequestInsightsStore().completeRequest(identity) getRequestInsightsStore().completeRequest(identity)
} }
export function startRequestInsight(identity: RequestInsightIdentity): void { export function startRequestInsight(identity: DebugRequestIdentity): void {
if (isRequestInsightsEnabled()) { if (isRequestInsightsEnabled()) {
getRequestInsightsStore().startRequest(identity) getRequestInsightsStore().startRequest(identity)
} }
} }
export function getRequestInsightForDebugRequest(
debugRequestId: string,
htmlRequestId: string
) {
if (!isRequestInsightsEnabled()) return undefined
return getRequestInsightsStore().getForDebugRequest(
debugRequestId,
htmlRequestId
)
}
export function setRequestInsightRootParent(
identity: RequestInsightIdentity | undefined,
parent: LocalSpanParent | undefined
): void {
if (isRequestInsightsEnabled() && identity && parent) {
getRequestInsightsStore().setDebugRequestParent(identity, parent)
}
}
export function getRequestInsightsSnapshot(): RequestInsightsSnapshot { export function getRequestInsightsSnapshot(): RequestInsightsSnapshot {
return getRequestInsightsStore().getSnapshot() return getRequestInsightsStore().getSnapshot()
} }
......
import type { ReactTimingRecord } from './react-debug-timing'
export const MAX_BROWSER_REACT_TIMING_BATCH = 32
export const MAX_BROWSER_REACT_TIMING_MESSAGE_LENGTH = 64 * 1024
export const MAX_BROWSER_REACT_TIMING_QUEUE = 256
function isText(value: unknown, maxLength: number): value is string {
return (
typeof value === 'string' &&
value.length <= maxLength &&
!/[\r\n\0]/.test(value)
)
}
export function isBrowserReactTimingRecord(
value: unknown
): value is ReactTimingRecord {
if (!value || typeof value !== 'object') return false
const record = value as ReactTimingRecord
return (
isText(record.id, 64) &&
/^decoded:(0|[1-9]\d{0,15})$/.test(record.id) &&
Number.isSafeInteger(Number(record.id.slice(8))) &&
(record.kind === 'component' || record.kind === 'await') &&
isText(record.name, 256) &&
isText(record.environment, 64) &&
(record.outcome === undefined ||
record.outcome === 'completed' ||
record.outcome === 'errored' ||
record.outcome === 'aborted') &&
(record.ownerName === undefined || isText(record.ownerName, 256)) &&
(record.componentPath === undefined ||
isText(record.componentPath, 2048)) &&
Number.isFinite(record.startTime) &&
record.startTime >= 0 &&
record.startTime <= Number.MAX_SAFE_INTEGER &&
Number.isFinite(record.durationMs) &&
record.durationMs >= 0 &&
record.durationMs <= 24 * 60 * 60 * 1000 &&
(record.source === undefined ||
(record.source !== null &&
typeof record.source === 'object' &&
isText(record.source.file, 2048) &&
isText(record.source.methodName, 256) &&
Number.isSafeInteger(record.source.line) &&
record.source.line >= 0 &&
Number.isSafeInteger(record.source.column) &&
record.source.column >= 0))
)
}
export function copyBrowserReactTimingRecord(
record: ReactTimingRecord
): ReactTimingRecord {
return {
id: record.id,
kind: record.kind,
name: record.name,
environment: record.environment,
outcome: record.outcome,
ownerName: record.ownerName,
componentPath: record.componentPath,
startTime: record.startTime,
durationMs: record.durationMs,
source: record.source && {
file: record.source.file,
methodName: record.source.methodName,
line: record.source.line,
column: record.source.column,
},
}
}
export type ReactTimingRecord = {
id: string
kind: 'component' | 'await'
name: string
environment: string
outcome?: 'completed' | 'errored' | 'aborted'
ownerName?: string
componentPath?: string
source?: {
file: string
methodName: string
line: number
column: number
}
startTime: number
durationMs: number
}
...@@ -2239,7 +2239,37 @@ export async function precompile(task, opts) { ...@@ -2239,7 +2239,37 @@ export async function precompile(task, opts) {
export async function copy_ncced(task) { export async function copy_ncced(task) {
// we don't ncc every time we build since these won't change // we don't ncc every time we build since these won't change
// that often and can be committed to the repo saving build time // that often and can be committed to the repo saving build time
await task.source('src/compiled/**/*').target('dist/compiled') await task
.source('src/compiled/**/*')
// eslint-disable-next-line require-yield
.run({ every: true }, function* (file) {
const match =
/^react-server-dom-(webpack|turbopack)-client\.browser\.development\.js$/.exec(
file.base
)
if (!match) return
const loader =
match[1] === 'webpack'
? '__webpack_chunk_load__'
: '__turbopack_load_by_url__'
// Next owns the host bindings; the vendored decoder body stays verbatim.
// Keep a single module instance and its module/chunk caches in each runtime.
file.data = `var nextReactDecoderHost;
if (process.env.__NEXT_DEV_SERVER && process.env.__NEXT_REQUEST_INSIGHTS && typeof window !== 'undefined') {
nextReactDecoderHost = require('../../../client/dev/react-decoder-host');
}
(function(console, performance, setTimeout, Promise, ${loader}) {
${file.data.toString()}
})(
nextReactDecoderHost ? nextReactDecoderHost.decoderConsole : console,
nextReactDecoderHost ? nextReactDecoderHost.decoderPerformance : performance,
nextReactDecoderHost ? nextReactDecoderHost.decoderSetTimeout : setTimeout,
nextReactDecoderHost ? nextReactDecoderHost.DecoderPromise : Promise,
nextReactDecoderHost ? nextReactDecoderHost.bindReactDecoderChunkLoader((chunk) => ${loader}(chunk)) : (chunk) => ${loader}(chunk)
);
`
})
.target('dist/compiled')
} }
export async function ncc(task, opts) { export async function ncc(task, opts) {
......
'use server'
import { revalidatePath } from 'next/cache'
export async function rerender() {
revalidatePath('/decoder-observations/destination')
}
import { randomUUID } from 'node:crypto'
import { Suspense } from 'react'
import { connection } from 'next/server'
import { rerender } from './actions'
async function DecoderObservedContent() {
await connection()
return (
<>
<p id="decoder-generation">{randomUUID()}</p>
<form action={rerender}>
<button id="decoder-rerender">Observe action rerender</button>
</form>
</>
)
}
export default function DecoderObservationDestinationPage() {
return (
<Suspense fallback={<p>Loading observed content</p>}>
<DecoderObservedContent />
</Suspense>
)
}
import Link from 'next/link'
export default function DecoderObservationsPage() {
return (
<Link
id="decoder-navigation"
href="/decoder-observations/destination"
prefetch={false}
>
Observe navigation
</Link>
)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
agentRules: false,
experimental: { requestInsights: process.env.NODE_ENV === 'production' },
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
import { readdir, readFile } from 'fs/promises'
import path from 'path'
describe('browser React timings disabled and production bundles', () => {
const { next, isNextDeploy } = nextTestSetup({ files: __dirname })
it('renders without including the decoder host in browser chunks', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
if (isNextDeploy) return
const chunks = path.join(next.testDir, next.distDir, 'static')
const files = await readdir(chunks, { recursive: true })
const javascript = files.filter((file) => file.endsWith('.js'))
expect(javascript.length).toBeGreaterThan(0)
for (const file of javascript) {
const code = await readFile(path.join(chunks, file), 'utf8')
expect(code).not.toMatch(
/react-decoder-host|bindReactDecoderStream|runWithReactDecoderScope|Some React timings were dropped/
)
}
})
})
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