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 { NEXT_REQUEST_ID_HEADER } from './components/app-router-headers'
import ReactDOMClient from 'react-dom/client'
import React from 'react'
// TODO: Explicitly import from client.browser
......@@ -32,9 +33,9 @@ import { initializeRouterTransitionModules } from './components/router-transitio
/// <reference types="react-dom/experimental" />
const createFromReadableStream =
let 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']
const appElement: HTMLElement | Document = document
......@@ -224,6 +225,25 @@ if (
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>
if (instantTestStaticFetch) {
// Instant Navigation Testing API: hydrate from the static RSC payload
......
......@@ -693,6 +693,7 @@ function createHaltingFlightResponse<T>(
// React attaches `_debugInfo` to the returned promise at runtime.
return createFromNextReadableStream<T>(wrapper, headers, {
allowPartialStream: true,
observeTiming: true,
}) as Promise<T> & { _debugInfo?: Array<any> }
}
......@@ -877,23 +878,63 @@ export async function createFetch<T>(
export function createFromNextReadableStream<T>(
flightStream: ReadableStream<Uint8Array>,
requestHeaders: RequestHeaders | undefined,
options?: { allowPartialStream?: boolean }
options?: { allowPartialStream?: boolean; observeTiming?: boolean }
): Promise<T> {
return createFromReadableStream(flightStream, {
callServer,
findSourceMapURL,
debugChannel: createDebugChannel && createDebugChannel(requestHeaders),
unstable_allowPartialStream: options?.allowPartialStream,
})
const debugChannel = createDebugChannel && createDebugChannel(requestHeaders)
if (
process.env.__NEXT_DEV_SERVER &&
process.env.__NEXT_REQUEST_INSIGHTS &&
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>(
promiseForResponse: Promise<Response>,
requestHeaders: RequestHeaders
): Promise<T> & { _debugInfo?: Array<any> } {
return createFromFetch(promiseForResponse, {
callServer,
findSourceMapURL,
debugChannel: createDebugChannel && createDebugChannel(requestHeaders),
})
const debugChannel = createDebugChannel && createDebugChannel(requestHeaders)
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(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(
? processFetch(res).then(({ response: r }) => r)
: Promise.resolve(res)
const response: ActionFlightResponse = await createFromFetch(
responsePromise,
{
const debugChannel = createDebugChannel && createDebugChannel(headers)
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,
findSourceMapURL,
temporaryReferences,
debugChannel: createDebugChannel && createDebugChannel(headers),
}
)
debugChannel,
})
}
// An internal redirect can send an RSC response, but does not have a useful `actionResult`.
actionResult = redirectLocation ? undefined : response.a
......
......@@ -37,7 +37,19 @@ export function createWebSocket(
const sendMessage = (data: string) => {
if (webSocket && webSocket.readyState === webSocket.OPEN) {
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<
typeof import('../runtime-error-state').createRuntimeErrorStateReporter
......@@ -65,6 +77,7 @@ export function createWebSocket(
function handleOnline() {
logQueue.onSocketReady(newWebSocket)
setReactTimingSender?.(sendMessage)
runtimeErrorStateReporter?.reportCurrent()
reconnections = 0
......@@ -127,6 +140,7 @@ export function createWebSocket(
}
function handleDisconnect() {
setReactTimingSender?.(undefined)
newWebSocket.onerror = null
newWebSocket.onclose = null
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(
const environment = span.attributes?.['next.rsc.environment']
const renderId = span.attributes?.['next.rsc.render_id']
const componentPath = span.attributes?.['next.rsc.component_path']
const outcome = span.attributes?.['next.rsc.outcome']
const incompleteReason = span.attributes?.['next.rsc.incomplete_reason']
const file = span.attributes?.['next.rsc.source.file']
const line1 = span.attributes?.['next.rsc.source.line']
......@@ -343,7 +344,7 @@ function getSpanTraceItem(
category: getSpanCategory(span),
label,
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,
startTime: span.startTime,
durationMs: span.durationMs,
......
......@@ -998,7 +998,6 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal,
}
)
......@@ -1035,7 +1034,6 @@ async function generateDynamicFlightRenderResult(
temporaryReferences: options?.temporaryReferences,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal,
}
)
......@@ -1542,7 +1540,6 @@ async function generateDynamicFlightRenderResultWithStagesInDev(
onError: onError,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
)
}
......@@ -1971,7 +1968,6 @@ async function finalRuntimeServerPrerender(
onError,
signal: finalServerController.signal,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
)
......@@ -3825,7 +3821,6 @@ async function renderToStream(
onError: serverComponentsErrorHandler,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
)
reactServerResult = new ReactServerResult(serverStream)
......@@ -4026,7 +4021,6 @@ async function renderToStream(
filterStackFrame,
onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
)
)
......@@ -4069,7 +4063,6 @@ async function renderToStream(
filterStackFrame,
onError: serverComponentsErrorHandler,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
)
)
......@@ -5683,7 +5676,6 @@ async function streamStagedRenderInDev({
startTime,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: requestAbortSignal,
}
) as Readable
......@@ -5855,7 +5847,6 @@ async function renderWithWarmCachesForValidationInDev(
startTime,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
signal: validationAbortSignal,
}
) as Readable
......@@ -5987,7 +5978,6 @@ async function prerenderWithWarmCachesForStaticValidationInDev(
startTime,
filterStackFrame,
debugChannel: debugChannel?.serverSide,
localRenderTiming: debugChannel?.localRenderTiming,
}
) as Readable
......
......@@ -5,7 +5,6 @@
import { PassThrough, Writable, type Readable } from 'node:stream'
import type { DebugChannelServer } from './debug-channel-server.web'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
export { createWebDebugChannel } from './debug-channel-server.web'
......@@ -17,7 +16,6 @@ export { createWebDebugChannel } from './debug-channel-server.web'
export type NodeDebugChannelPair = {
serverSide: DebugChannelServer
clientSide: { readable: Readable }
localRenderTiming?: LocalRenderTiming
}
/**
......@@ -26,14 +24,6 @@ export type NodeDebugChannelPair = {
* which expects debugChannel to be a Node.js stream with a .write() method.
*/
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
// server-side write target is a separate, write-only Writable that forwards
// into it rather than the PassThrough itself: React's renderToPipeableStream
......@@ -52,24 +42,15 @@ export function createNodeDebugChannel(): NodeDebugChannelPair {
const writable = new Writable({
write(chunk, encoding, callback) {
localRenderTiming?.readDebugChunk(chunk)
passthrough.write(chunk, encoding, callback)
},
final(callback) {
localRenderTiming?.finishDebug()
passthrough.end(callback)
},
})
writable.once('close', () => {
if (!writable.writableFinished) localRenderTiming?.abort()
})
passthrough.once('close', () => {
if (!passthrough.readableEnded) localRenderTiming?.abort()
})
return {
serverSide: writable,
clientSide: { readable: passthrough },
localRenderTiming,
}
}
......@@ -4,12 +4,10 @@
*/
import type { AnyStream } from './app-render-prerender-utils'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
export type DebugChannelPair = {
serverSide: DebugChannelServer
clientSide: DebugChannelClient
localRenderTiming?: LocalRenderTiming
}
export type DebugChannelServer = any
......@@ -24,44 +22,29 @@ type DebugChannelClient = {
* which expects debugChannel = { writable: WritableStream }.
*/
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
const clientSideReadable = new ReadableStream<Uint8Array>({
start(controller) {
readableController = controller
},
cancel() {
localRenderTiming?.abort()
},
})
return {
serverSide: {
writable: new WritableStream<Uint8Array>({
write(chunk) {
localRenderTiming?.readDebugChunk(chunk)
readableController?.enqueue(chunk)
},
close() {
localRenderTiming?.finishDebug()
readableController?.close()
},
abort(err) {
localRenderTiming?.abort()
readableController?.error(err)
},
}),
},
clientSide: { readable: clientSideReadable },
localRenderTiming,
}
}
......
......@@ -547,7 +547,6 @@ export function renderToNodeFlightStream(
opts: FlightRenderOptions
): AnyStream {
if (!ComponentMod.renderToPipeableStream) {
opts.localRenderTiming?.abort()
throw new Error('renderToPipeableStream is not implemented')
}
......@@ -556,21 +555,9 @@ export function renderToNodeFlightStream(
// 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 request signal, which can outlive it.
const { signal, localRenderTiming, ...renderOptions } = opts ?? {}
localRenderTiming?.start()
const pt = localRenderTiming
? new Transform({
transform(chunk, _encoding, callback) {
localRenderTiming.readFlightChunk(chunk)
callback(null, chunk)
},
flush(callback) {
localRenderTiming.finishFlight()
callback()
},
})
: new PassThrough()
const { signal, ...renderOptions } = opts ?? {}
const pt = new PassThrough()
let pipeable: ReturnType<
NonNullable<FlightComponentMod['renderToPipeableStream']>
>
......@@ -581,7 +568,6 @@ export function renderToNodeFlightStream(
renderOptions
)
} catch (error) {
localRenderTiming?.abort()
pt.destroy()
throw error
}
......@@ -592,7 +578,6 @@ export function renderToNodeFlightStream(
// know; the listener is registered before piping so it runs before React's.
pt.once('close', () => {
if (!pt.writableFinished) {
localRenderTiming?.abort()
pipeable.abort(new ResponseAborted())
}
})
......@@ -600,20 +585,15 @@ export function renderToNodeFlightStream(
try {
pipeable.pipe(pt)
} catch (error) {
localRenderTiming?.abort()
pt.destroy()
throw error
}
if (signal) {
if (signal.aborted) {
localRenderTiming?.abort()
pipeable.abort(signal.reason)
} else {
const onAbort = () => {
localRenderTiming?.abort()
pipeable.abort(signal.reason)
}
const onAbort = () => pipeable.abort(signal.reason)
signal.addEventListener('abort', onAbort, { once: true })
pt.on('close', () => signal.removeEventListener('abort', onAbort))
}
......
......@@ -11,7 +11,6 @@ import type { PostponedState, PrerenderOptions } from 'react-dom/static'
import { resume, renderToReadableStream } from 'react-dom/server'
import { prerender } from 'react-dom/static'
import type { renderToReadableStream as flightRenderToReadableStream } from 'react-server-dom-webpack/server'
import type { LocalRenderTiming } from '../lib/trace/react-render-timing'
import {
renderToInitialFizzStream,
......@@ -81,7 +80,7 @@ export type FlightClientModules = Parameters<FlightRenderToReadableStream>[1]
*/
export type FlightRenderOptions = NonNullable<
Parameters<FlightRenderToReadableStream>[2]
> & { localRenderTiming?: LocalRenderTiming }
>
export type FizzStreamResult = {
stream: AnyStream
......@@ -253,74 +252,7 @@ export function renderToWebFlightStream(
clientModules: FlightClientModules,
opts: FlightRenderOptions
): AnyStream {
const { localRenderTiming, ...renderOptions } = 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 }
)
return ComponentMod.renderToReadableStream(payload, clientModules, opts)
}
export async function streamToString(stream: AnyStream): Promise<string> {
......
......@@ -895,8 +895,16 @@ export default abstract class Server<
'http.target': req.url,
},
},
async (span) =>
this.handleRequestImpl(req, res, parsedUrl).finally(() => {
async (span) => {
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
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
......@@ -955,6 +963,7 @@ export default abstract class Server<
span.updateName(isRSCRequest ? `RSC ${method}` : `${method}`)
}
})
}
)
})
......
......@@ -145,6 +145,7 @@ import { handleErrorStateResponse } from '../mcp/tools/get-errors'
import { handlePageMetadataResponse } from '../mcp/tools/get-page-metadata'
import { setStackFrameResolver } from '../mcp/tools/utils/format-errors'
import { createRuntimeErrorStateHandler } from './runtime-error-state'
import { createBrowserReactTimingReceiver } from '../lib/trace/browser-react-timings'
import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker'
import { getFileLogger } from './browser-logs/file-logger'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
......@@ -1506,6 +1507,10 @@ export async function createHotReloaderTurbopack(
const htmlRequestId = req.url
? new URL(req.url, 'http://n').searchParams.get('id')
: null
const reactTimings = createBrowserReactTimingReceiver(
htmlRequestId,
nextConfig.experimental.requestInsights === true
)
// Clients with a request ID are inferred App Router clients. If Cache
// Components is not enabled, we consider those legacy clients. Pages
......@@ -1565,6 +1570,7 @@ export async function createHotReloaderTurbopack(
: undefined
client.on('close', () => {
reactTimings.dispose()
runtimeErrorStateHandler?.dispose()
// Remove active subscriptions
......@@ -1588,6 +1594,10 @@ export async function createHotReloaderTurbopack(
// Next.js messages
switch (parsedData.event) {
case HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS: {
reactTimings.receive(parsedData, data.toString().length)
break
}
case 'span-end': {
hotReloaderSpan.manualTraceChild(
parsedData.spanName,
......
......@@ -57,6 +57,15 @@ export const enum HMR_MESSAGE_SENT_TO_SERVER {
RUNTIME_ERRORS = 'runtimeErrors',
MCP_PAGE_METADATA_RESPONSE = 'mcp-page-metadata-response',
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 {
......
......@@ -78,8 +78,10 @@ import {
import { RouteKind } from '../route-kind'
import {
HMR_MESSAGE_SENT_TO_BROWSER,
HMR_MESSAGE_SENT_TO_SERVER,
type NextJsHotReloaderInterface,
} from './hot-reloader-types'
import { createBrowserReactTimingReceiver } from '../lib/trace/browser-react-timings'
import type { HmrMessageSentToBrowser } from './hot-reloader-types'
import type { WebpackError } from 'webpack'
import { PAGE_TYPES } from '../../lib/page-types'
......@@ -460,6 +462,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
const htmlRequestId = req.url
? new URL(req.url, 'http://n').searchParams.get('id')
: null
const reactTimings = createBrowserReactTimingReceiver(
htmlRequestId,
this.config.experimental.requestInsights === true
)
if (!this.webpackHotMiddleware) {
throw new InvariantError('Did not start HotReloaderWebpack.')
......@@ -498,6 +504,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
| undefined
switch (payload.event) {
case HMR_MESSAGE_SENT_TO_SERVER.REACT_DEBUG_TIMINGS: {
reactTimings.receive(payload, data.length)
break
}
case 'span-end': {
traceChild = {
name: payload.spanName,
......@@ -676,6 +686,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
}
client.on('close', () => {
reactTimings.dispose()
this.webpackHotMiddleware?.deleteClient(client, htmlRequestId)
if (htmlRequestId) {
......
......@@ -90,6 +90,7 @@ import {
appendRequestInsightUpdateToJournal,
closeRequestInsightsJournal,
configureRequestInsightsJournal,
getArchivedRequestInsightDebugContext,
getRequestInsightsHistory as getJournalHistory,
readRequestInsightsJournal,
} from '../lib/trace/request-insights-journal'
......@@ -289,6 +290,7 @@ export default class DevServer extends Server {
await configureRequestInsightsJournal(this.distDir)
configureRequestInsightsHistoryProvider({
append: appendRequestInsightToJournal,
getDebugRequest: getArchivedRequestInsightDebugContext,
appendUpdate: appendRequestInsightUpdateToJournal,
appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal,
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 {
captureLocalSpanContext,
type captureLocalSpanContext,
createLocalSpanId,
} from './local-span-recorder'
import {
captureLocalSpanRecorder,
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()
import type { SpanStoreRecord } from './span-store'
import type { ReactTimingRecord } from '../../../shared/lib/react-debug-timing'
collector = createReactTimingCollector(
(timings) => {
record(
timings.map((timing) => ({
...context,
name: `ReactServerComponents.${timing.kind}`,
spanId: createLocalSpanId(parent.spanId),
timestamp: timing.startTime + timing.durationMs,
startTime: timing.startTime,
durationMs: timing.durationMs,
status: 'ok',
attributes: {
'next.span_type': `ReactServerComponents.${timing.kind}`,
'next.span_name':
timing.kind === 'component'
? `render ${timing.name}`
: `await ${timing.name}`,
'next.span_category': 'application',
'next.rsc.kind': timing.kind,
'next.rsc.render_id': renderId,
'next.rsc.environment': timing.environment,
...(timing.source
? {
'next.rsc.source.file': timing.source.file,
'next.rsc.source.line': timing.source.line,
'next.rsc.source.column': timing.source.column,
'next.rsc.source.name': timing.source.methodName,
}
: {}),
...(timing.ownerName
? { 'next.rsc.owner': timing.ownerName }
: {}),
...(timing.componentPath
? { 'next.rsc.component_path': timing.componentPath }
: {}),
'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.'
)
export function createReactTimingSpanRecords(
timings: readonly ReactTimingRecord[],
context: ReturnType<typeof captureLocalSpanContext>,
renderId: string
): SpanStoreRecord[] {
return timings.map((timing) => ({
...context,
name: `ReactServerComponents.${timing.kind}`,
spanId: createLocalSpanId(context.parentSpanId),
timestamp: timing.startTime + timing.durationMs,
startTime: timing.startTime,
durationMs: timing.durationMs,
status:
timing.outcome === 'errored'
? 'error'
: timing.outcome === 'completed'
? 'ok'
: undefined,
attributes: {
'next.span_type': `ReactServerComponents.${timing.kind}`,
'next.span_name': `${timing.kind === 'component' ? 'render' : 'await'} ${timing.name}`,
'next.span_category': 'application',
'next.rsc.kind': timing.kind,
'next.rsc.render_id': renderId,
'next.rsc.environment': timing.environment,
'next.rsc.observation': 'decoded',
...(timing.outcome ? { 'next.rsc.outcome': timing.outcome } : {}),
'next.rsc.timing':
timing.kind === 'component' ? 'render-interval' : 'await',
...(timing.source
? {
'next.rsc.source.file': timing.source.file,
'next.rsc.source.line': timing.source.line,
'next.rsc.source.column': timing.source.column,
'next.rsc.source.name': timing.source.methodName,
}
)
} catch {
closed = true
}
},
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
: {}),
...(timing.ownerName ? { 'next.rsc.owner': timing.ownerName } : {}),
...(timing.componentPath
? { 'next.rsc.component_path': timing.componentPath }
: {}),
},
}
}))
}
......@@ -10,7 +10,10 @@ import {
REQUEST_INSIGHT_REQUEST_SPAN_TYPE,
type RequestInsight,
} from '../../../shared/lib/request-insights'
import type { RequestInsightUpdate } from './request-insights'
import type {
RequestInsightDebugContext,
RequestInsightUpdate,
} from './request-insights'
import {
getRequestInsightTags,
matchesRequestInsightFilters,
......@@ -51,6 +54,7 @@ type HistoryFilter = {
type IndexedSummary = {
sequence: number
request: RequestInsightSummary
debugContext?: RequestInsightDebugContext
}
type PendingAppend = {
......@@ -58,6 +62,7 @@ type PendingAppend = {
byteLength: number
key: string
summary: RequestInsightSummary
debugContext?: RequestInsightDebugContext
}
type AppendBatch = {
......@@ -77,14 +82,21 @@ class RequestInsightsJournal {
private size = 0
private sequence = 0
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 generation = 0
private truncated = false
constructor(readonly file: string) {}
append(request: RequestInsight): void {
append(
request: RequestInsight,
debugContext?: RequestInsightDebugContext
): void {
let line: string
try {
line = `${JSON.stringify({ version: 1, request } satisfies JournalRecord)}\n`
......@@ -95,7 +107,7 @@ class RequestInsightsJournal {
const byteLength = Buffer.byteLength(line, 'utf8')
const summary = summarizeRequestInsight(request)
const key = getRequestInsightKey(request)
this.pendingRequests.add(key)
this.pendingRequests.set(key, debugContext)
let batch = this.pendingAppends
if (!batch || batch.byteLength + byteLength > JOURNAL_WRITE_BATCH_BYTES) {
batch = { records: [], byteLength: 0 }
......@@ -116,7 +128,7 @@ class RequestInsightsJournal {
}
})
}
batch.records.push({ line, byteLength, key, summary })
batch.records.push({ line, byteLength, key, summary, debugContext })
batch.byteLength += byteLength
}
......@@ -148,11 +160,55 @@ class RequestInsightsJournal {
this.summaries.set(record.key, {
sequence: ++this.sequence,
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(
identity: Pick<RequestInsight, 'requestId' | 'kind'>,
update: RequestInsightUpdate
......@@ -182,7 +238,8 @@ class RequestInsightsJournal {
const fetchCount = request?.fetches.length
this.writes = this.writes
.then(async () => {
const previous = this.summaries.get(key)?.request
const previousEntry = this.summaries.get(key)
const previous = previousEntry?.request
if (!previous && !snapshot) {
return
}
......@@ -276,7 +333,9 @@ class RequestInsightsJournal {
this.summaries.set(key, {
sequence: this.summaries.get(key)?.sequence ?? ++this.sequence,
request: updatedSummary,
debugContext: previousEntry?.debugContext,
})
this.indexDebugRequest(previousEntry?.debugContext)
})
.catch((error) => {
console.warn('Failed to write Request Insights journal', error)
......@@ -287,6 +346,7 @@ class RequestInsightsJournal {
await writeFile(this.file, '')
this.size = 0
this.summaries.clear()
this.debugRequests.clear()
this.generation++
this.truncated = true
}
......@@ -298,6 +358,7 @@ class RequestInsightsJournal {
this.size = 0
this.sequence = 0
this.summaries.clear()
this.debugRequests.clear()
this.sessionId = createSessionId()
this.generation = 0
this.truncated = false
......@@ -415,8 +476,21 @@ type JournalRegistry = {
journals: Map<string, RequestInsightsJournal>
}
export function appendRequestInsightToJournal(request: RequestInsight): void {
getJournalRegistry().configured?.append(request)
export function appendRequestInsightToJournal(
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(
......
......@@ -4,6 +4,8 @@ import {
completeRequestInsight,
configureRequestInsightsHistoryProvider,
getRequestInsightsSnapshot,
getRequestInsightForDebugRequest,
setRequestInsightRootParent,
importRequestInsightSpans,
recordRequestInsightFetch,
registerRequestInsightsExporter,
......@@ -17,15 +19,21 @@ import {
appendRequestInsightUpdateToJournal,
closeRequestInsightsJournal,
configureRequestInsightsJournal,
getArchivedRequestInsightDebugContext,
getRequestInsightsHistory,
initializeRequestInsightsJournal,
readRequestInsightsJournal,
resetRequestInsightsJournalForTest,
StaleRequestInsightsHistoryCursorError,
} from './request-insights-journal'
import { recordSpan } from './span-store'
import { createLocalRenderTiming } from './react-render-timing'
import { registerLocalSpanRecorder } from './local-span-recorder'
import { recordSpan, recordSpans } from './span-store'
import { createReactTimingSpanRecords } from './react-render-timing'
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 { getTracer } from './tracer'
import {
......@@ -723,6 +731,10 @@ describe('request insights', () => {
createRequestId: () => requestId,
})
recordSpan({ name: 'original', requestId })
setRequestInsightRootParent(identity, {
traceId: 'trace',
spanId: 'parent',
})
completeRequestInsight(identity)
}
......@@ -739,6 +751,9 @@ describe('request insights', () => {
requestId: 'discarded',
})
).toHaveLength(1)
expect(
getRequestInsightForDebugRequest('discarded', 'discarded')
).toBeDefined()
const fs = require('fs/promises') as typeof import('fs/promises')
const currentStat = await fs.stat(
......@@ -757,6 +772,9 @@ describe('request insights', () => {
requestId: 'discarded',
})
).toHaveLength(0)
expect(
getRequestInsightForDebugRequest('discarded', 'discarded')
).toBeUndefined()
if (update === 'span') {
recordSpan({ name: 'late work', requestId: 'discarded' })
......@@ -912,13 +930,13 @@ describe('request insights', () => {
await initializeRequestInsightsJournal(distDir)
configureJournalProvider(distDir)
startRequestInsight(identity)
let render!: NonNullable<ReturnType<typeof createLocalRenderTiming>>
let context!: ReturnType<typeof captureLocalSpanContext>
let parentSpanId!: string
runWithRequestInsightsIdentity(identity, () =>
getTracer().trace(AppRenderSpan.renderToReadableStream, (span) => {
parentSpanId = span!.spanContext().spanId
render = createLocalRenderTiming()!
render.start()
context = captureLocalSpanContext(span!.spanContext())
setRequestInsightRootParent(identity, span!.spanContext())
})
)
completeRequestInsight(identity)
......@@ -928,11 +946,22 @@ describe('request insights', () => {
durationMs: completed.durationMs,
}
render.readFlightChunk('0:D"$1"\n0:D"$2"\n0:D"$3"\n')
render.readDebugChunk(
':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'
)
await Promise.resolve()
const component = {
id: 'decoded:0',
kind: 'component' as const,
name: 'Page',
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({
...responseTiming,
spans: expect.arrayContaining([
......@@ -946,10 +975,22 @@ describe('request insights', () => {
recordSpan({ name: 'GET /', ...other })
completeRequestInsight(other)
}
render.readFlightChunk('f:D"$1"\nf:D"$2"\nf:D"$3"\n')
render.readDebugChunk('4:{"name":"' + 'x'.repeat(1024 * 1024))
render.finishFlight()
render.finishDebug()
expect(
getRequestInsightForDebugRequest('react-stream', 'react-stream')
).toMatchObject({ parent: { spanId: parentSpanId } })
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)
expect(stored).toMatchObject(responseTiming)
......@@ -957,19 +998,6 @@ describe('request insights', () => {
(span) => span.name === 'ReactServerComponents.component'
)
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) {
expect(interval).toMatchObject({
parentSpanId,
......@@ -1672,6 +1700,7 @@ describe('request insights', () => {
function configureJournalProvider(distDir: string): void {
configureRequestInsightsHistoryProvider({
append: appendRequestInsightToJournal,
getDebugRequest: getArchivedRequestInsightDebugContext,
appendUpdate: appendRequestInsightUpdateToJournal,
appendArchivedUpdate: appendArchivedRequestInsightUpdateToJournal,
getHistory: (query) => getRequestInsightsHistory(distDir, query),
......
......@@ -41,6 +41,17 @@ const REQUEST_INSIGHTS_HISTORY_PROVIDER_KEY = Symbol.for(
const CLIENT_COMPONENT_LOADING_SPAN_TYPE =
'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 RequestInsightIdentity = Readonly<{
requestId?: string
......@@ -68,7 +79,14 @@ export type RequestInsightsJournalQuery = {
}
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
appendArchivedUpdate?(
identity: Pick<RequestInsight, 'requestId' | 'kind'>,
......@@ -105,6 +123,9 @@ const SAFE_SPAN_ATTRIBUTE_KEYS = new Set([
'next.rsc.owner',
'next.rsc.component_path',
'next.rsc.environment',
'next.rsc.observation',
'next.rsc.outcome',
'next.rsc.observation_partial',
'next.rsc.timing',
'next.rsc.source.file',
'next.rsc.source.line',
......@@ -125,8 +146,14 @@ class InMemoryRequestInsightsStore {
private readonly requestOrder: string[] = []
private readonly completedRequestOrder: string[] = []
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) {
return
}
......@@ -134,6 +161,28 @@ class InMemoryRequestInsightsStore {
requestId: identity.requestId,
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) {
this.activeRequests.add(insightKey)
}
......@@ -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 {
this.listeners.add(listener)
return () => {
......@@ -334,6 +427,9 @@ class InMemoryRequestInsightsStore {
this.requestTimings.clear()
this.requestOrder.length = 0
this.completedRequestOrder.length = 0
this.debugRequestKeys.clear()
this.debugRequestParents.clear()
this.debugRequests.clear()
}
private updateTiming(
......@@ -462,7 +558,25 @@ class InMemoryRequestInsightsStore {
this.activeRequests.delete(insightKey)
insight.completedAt = completedAt
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)
if (requestIndex !== -1) {
......@@ -475,6 +589,14 @@ class InMemoryRequestInsightsStore {
) {
const completedInsightKey = this.completedRequestOrder.shift()
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.requestTimings.delete(completedInsightKey)
const completedIndex = this.requestOrder.indexOf(completedInsightKey)
......@@ -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_REQUEST_INSIGHTS) {
getRequestInsightsHistoryProvider()?.append(insight)
getRequestInsightsHistoryProvider()?.append(insight, debugContext)
}
} else {
return
......@@ -639,12 +764,32 @@ export function completeRequestInsight(identity: RequestInsightIdentity): void {
getRequestInsightsStore().completeRequest(identity)
}
export function startRequestInsight(identity: RequestInsightIdentity): void {
export function startRequestInsight(identity: DebugRequestIdentity): void {
if (isRequestInsightsEnabled()) {
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 {
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) {
export async function copy_ncced(task) {
// 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
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) {
......
'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