Skip to content

Commit

Permalink
Telemetry: ensure the ClientComponentLoad metric is only reported whe…
Browse files Browse the repository at this point in the history
…n available (vercel#62459)
  • Loading branch information
dvoytenko authored Feb 23, 2024
1 parent dcf8f04 commit 00f300a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
24 changes: 14 additions & 10 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -634,16 +634,20 @@ async function renderToHTMLOrFlightImpl(
req.on('end', () => {
if ('performance' in globalThis) {
const metrics = getClientComponentLoaderMetrics({ reset: true })
getTracer()
.startSpan(NextNodeServerSpan.clientComponentLoading, {
startTime: metrics.clientComponentLoadStart,
attributes: {
'next.clientComponentLoadCount': metrics.clientComponentLoadCount,
},
})
.end(
metrics.clientComponentLoadStart + metrics.clientComponentLoadTimes
)
if (metrics) {
getTracer()
.startSpan(NextNodeServerSpan.clientComponentLoading, {
startTime: metrics.clientComponentLoadStart,
attributes: {
'next.clientComponentLoadCount':
metrics.clientComponentLoadCount,
},
})
.end(
metrics.clientComponentLoadStart +
metrics.clientComponentLoadTimes
)
}
}
})
}
Expand Down
13 changes: 8 additions & 5 deletions packages/next/src/server/client-component-renderer-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ export function wrapClientComponentLoader(ComponentMod: any) {
export function getClientComponentLoaderMetrics(
options: { reset?: boolean } = {}
) {
const metrics = {
clientComponentLoadStart,
clientComponentLoadTimes,
clientComponentLoadCount,
}
const metrics =
clientComponentLoadStart === 0
? undefined
: {
clientComponentLoadStart,
clientComponentLoadTimes,
clientComponentLoadCount,
}

if (options.reset) {
clientComponentLoadStart = 0
Expand Down
20 changes: 11 additions & 9 deletions packages/next/src/server/pipe-readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ function createWriterFromResponse(
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
const metrics = getClientComponentLoaderMetrics()
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{
start: metrics.clientComponentLoadStart,
end:
metrics.clientComponentLoadStart +
metrics.clientComponentLoadTimes,
}
)
if (metrics) {
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{
start: metrics.clientComponentLoadStart,
end:
metrics.clientComponentLoadStart +
metrics.clientComponentLoadTimes,
}
)
}
}

res.flushHeaders()
Expand Down

0 comments on commit 00f300a

Please sign in to comment.