Commit a230be48 authored by JJ Kasper's avatar JJ Kasper Committed by Jiachi Liu

Clean-up fetch metrics tracking (#64746)

This ensures we only track fetch metrics in development mode as that's
the only time we report them currently, this also adds an upper limit on
how many metrics we store per-request as previously this was unbounded,
on top of that this ensures we don't keep tracking fetch metrics after
the request has ended as we only report on request end, and finally this
adds a clean-up to the reference we attach to the request object
containing the fetch metrics once we have used them.

Closes: https://github.com/vercel/next.js/issues/64212

Closes NEXT-3159
parent 73c2d634
...@@ -51,6 +51,8 @@ export interface StaticGenerationStore { ...@@ -51,6 +51,8 @@ export interface StaticGenerationStore {
isDraftMode?: boolean isDraftMode?: boolean
isUnstableNoStore?: boolean isUnstableNoStore?: boolean
requestEndedState?: { ended?: boolean }
} }
export type StaticGenerationAsyncStorage = export type StaticGenerationAsyncStorage =
......
...@@ -585,7 +585,8 @@ async function renderToHTMLOrFlightImpl( ...@@ -585,7 +585,8 @@ async function renderToHTMLOrFlightImpl(
pagePath: string, pagePath: string,
query: NextParsedUrlQuery, query: NextParsedUrlQuery,
renderOpts: RenderOpts, renderOpts: RenderOpts,
baseCtx: AppRenderBaseContext baseCtx: AppRenderBaseContext,
requestEndedState: { ended?: boolean }
) { ) {
const isNotFoundPath = pagePath === '/404' const isNotFoundPath = pagePath === '/404'
...@@ -621,6 +622,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -621,6 +622,7 @@ async function renderToHTMLOrFlightImpl(
if (typeof req.on === 'function') { if (typeof req.on === 'function') {
req.on('end', () => { req.on('end', () => {
requestEndedState.ended = true
if ('performance' in globalThis) { if ('performance' in globalThis) {
const metrics = getClientComponentLoaderMetrics({ reset: true }) const metrics = getClientComponentLoaderMetrics({ reset: true })
if (metrics) { if (metrics) {
...@@ -1438,14 +1440,23 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -1438,14 +1440,23 @@ export const renderToHTMLOrFlight: AppPageRender = (
{ {
urlPathname: pathname, urlPathname: pathname,
renderOpts, renderOpts,
requestEndedState: { ended: false },
}, },
(staticGenerationStore) => (staticGenerationStore) =>
renderToHTMLOrFlightImpl(req, res, pagePath, query, renderOpts, { renderToHTMLOrFlightImpl(
requestStore, req,
staticGenerationStore, res,
componentMod: renderOpts.ComponentMod, pagePath,
query,
renderOpts, renderOpts,
}) {
requestStore,
staticGenerationStore,
componentMod: renderOpts.ComponentMod,
renderOpts,
},
staticGenerationStore.requestEndedState || {}
)
) )
) )
} }
...@@ -9,6 +9,7 @@ import type { FetchMetric } from '../base-http' ...@@ -9,6 +9,7 @@ import type { FetchMetric } from '../base-http'
export type StaticGenerationContext = { export type StaticGenerationContext = {
urlPathname: string urlPathname: string
requestEndedState?: { ended?: boolean }
renderOpts: { renderOpts: {
incrementalCache?: IncrementalCache incrementalCache?: IncrementalCache
isOnDemandRevalidate?: boolean isOnDemandRevalidate?: boolean
...@@ -50,7 +51,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper< ...@@ -50,7 +51,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
> = { > = {
wrap<Result>( wrap<Result>(
storage: AsyncLocalStorage<StaticGenerationStore>, storage: AsyncLocalStorage<StaticGenerationStore>,
{ urlPathname, renderOpts }: StaticGenerationContext, { urlPathname, renderOpts, requestEndedState }: StaticGenerationContext,
callback: (store: StaticGenerationStore) => Result callback: (store: StaticGenerationStore) => Result
): Result { ): Result {
/** /**
...@@ -96,6 +97,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper< ...@@ -96,6 +97,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
isDraftMode: renderOpts.isDraftMode, isDraftMode: renderOpts.isDraftMode,
prerenderState, prerenderState,
requestEndedState,
} }
// TODO: remove this when we resolve accessing the store outside the execution context // TODO: remove this when we resolve accessing the store outside the execution context
......
...@@ -163,7 +163,13 @@ function trackFetchMetric( ...@@ -163,7 +163,13 @@ function trackFetchMetric(
staticGenerationStore: StaticGenerationStore, staticGenerationStore: StaticGenerationStore,
ctx: Omit<FetchMetric, 'end' | 'idx'> ctx: Omit<FetchMetric, 'end' | 'idx'>
) { ) {
if (!staticGenerationStore) return if (
!staticGenerationStore ||
staticGenerationStore.requestEndedState?.ended ||
process.env.NODE_ENV !== 'development'
) {
return
}
staticGenerationStore.fetchMetrics ??= [] staticGenerationStore.fetchMetrics ??= []
const dedupeFields = ['url', 'status', 'method'] as const const dedupeFields = ['url', 'status', 'method'] as const
...@@ -181,6 +187,25 @@ function trackFetchMetric( ...@@ -181,6 +187,25 @@ function trackFetchMetric(
end: Date.now(), end: Date.now(),
idx: staticGenerationStore.nextFetchId || 0, idx: staticGenerationStore.nextFetchId || 0,
}) })
// only store top 10 metrics to avoid storing too many
if (staticGenerationStore.fetchMetrics.length > 10) {
// sort slowest first as these should be highlighted
staticGenerationStore.fetchMetrics.sort((a, b) => {
const aDur = a.end - a.start
const bDur = b.end - b.start
if (aDur < bDur) {
return 1
} else if (aDur > bDur) {
return -1
}
return 0
})
// now grab top 10
staticGenerationStore.fetchMetrics =
staticGenerationStore.fetchMetrics.slice(0, 10)
}
} }
interface PatchableModule { interface PatchableModule {
......
...@@ -1127,6 +1127,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1127,6 +1127,7 @@ export default class NextNodeServer extends BaseServer {
if (this.renderOpts.dev) { if (this.renderOpts.dev) {
const { blue, green, yellow, red, gray, white } = const { blue, green, yellow, red, gray, white } =
require('../lib/picocolors') as typeof import('../lib/picocolors') require('../lib/picocolors') as typeof import('../lib/picocolors')
const _res = res as NodeNextResponse | ServerResponse const _res = res as NodeNextResponse | ServerResponse
const origRes = const origRes =
'originalResponse' in _res ? _res.originalResponse : _res 'originalResponse' in _res ? _res.originalResponse : _res
...@@ -1247,6 +1248,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1247,6 +1248,7 @@ export default class NextNodeServer extends BaseServer {
} }
} }
} }
delete normalizedReq.fetchMetrics
origRes.off('close', reqCallback) origRes.off('close', reqCallback)
} }
origRes.on('close', reqCallback) origRes.on('close', reqCallback)
......
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