Unverified Commit 6feb803f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add otel span for client component loading (#62296)

This adds a new span to allow tracking the sum of all client component
loading times for a specific request along with the count of items
loaded.

Closes NEXT-2540

---------
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent f4ac92d3
...@@ -51,7 +51,7 @@ import { ...@@ -51,7 +51,7 @@ import {
getRedirectStatusCodeFromError, getRedirectStatusCodeFromError,
} from '../../client/components/redirect' } from '../../client/components/redirect'
import { addImplicitTags } from '../lib/patch-fetch' import { addImplicitTags } from '../lib/patch-fetch'
import { AppRenderSpan } from '../lib/trace/constants' import { AppRenderSpan, NextNodeServerSpan } from '../lib/trace/constants'
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
import { FlightRenderResult } from './flight-render-result' import { FlightRenderResult } from './flight-render-result'
import { import {
...@@ -615,14 +615,71 @@ async function renderToHTMLOrFlightImpl( ...@@ -615,14 +615,71 @@ async function renderToHTMLOrFlightImpl(
enableTainting, enableTainting,
} = renderOpts } = renderOpts
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0
// We need to expose the bundled `require` API globally for // We need to expose the bundled `require` API globally for
// react-server-dom-webpack. This is a hack until we find a better way. // react-server-dom-webpack. This is a hack until we find a better way.
if (ComponentMod.__next_app__) { if (ComponentMod.__next_app__) {
// @ts-ignore // @ts-ignore
globalThis.__next_require__ = ComponentMod.__next_app__.require globalThis.__next_require__ = (...args: any[]) => {
if (clientComponentLoadStart === 0) {
clientComponentLoadStart = Date.now()
}
const startTime = Date.now()
try {
clientComponentLoadCount += 1
return ComponentMod.__next_app__.require(...args)
} finally {
clientComponentLoadTimes += Date.now() - startTime
}
}
// @ts-ignore // @ts-ignore
globalThis.__next_chunk_load__ = ComponentMod.__next_app__.loadChunk globalThis.__next_chunk_load__ = (...args: any[]) => {
const startTime = Date.now()
try {
clientComponentLoadCount += 1
return ComponentMod.__next_app__.loadChunk(...args)
} finally {
clientComponentLoadTimes += Date.now() - startTime
}
}
}
if (typeof req.on === 'function') {
req.on('end', () => {
const type = NextNodeServerSpan.clientComponentLoading
const startTime = clientComponentLoadStart
const endTime = clientComponentLoadStart + clientComponentLoadTimes
getTracer()
.startSpan(type, {
startTime,
attributes: {
'next.clientComponentLoadCount': clientComponentLoadCount,
},
})
.end(endTime)
if (
typeof performance !== 'undefined' &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
const { timeOrigin } = performance
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(
type.split('.').pop() || ''
).replace(/[A-Z]/g, (match: string) => '-' + match.toLowerCase())}`,
{
start: startTime - timeOrigin,
end: endTime - timeOrigin,
}
)
}
})
} }
const metadata: AppPageRenderResultMetadata = {} const metadata: AppPageRenderResultMetadata = {}
......
...@@ -39,6 +39,7 @@ enum NextNodeServerSpan { ...@@ -39,6 +39,7 @@ enum NextNodeServerSpan {
compression = 'NextNodeServer.compression', compression = 'NextNodeServer.compression',
getBuildId = 'NextNodeServer.getBuildId', getBuildId = 'NextNodeServer.getBuildId',
createComponentTree = 'NextNodeServer.createComponentTree', createComponentTree = 'NextNodeServer.createComponentTree',
clientComponentLoading = 'NextNodeServer.clientComponentLoading',
getLayoutOrPageModule = 'NextNodeServer.getLayoutOrPageModule', getLayoutOrPageModule = 'NextNodeServer.getLayoutOrPageModule',
generateStaticRoutes = 'NextNodeServer.generateStaticRoutes', generateStaticRoutes = 'NextNodeServer.generateStaticRoutes',
generateFsStaticRoutes = 'NextNodeServer.generateFsStaticRoutes', generateFsStaticRoutes = 'NextNodeServer.generateFsStaticRoutes',
...@@ -134,6 +135,7 @@ export const NextVanillaSpanAllowlist = [ ...@@ -134,6 +135,7 @@ export const NextVanillaSpanAllowlist = [
NextNodeServerSpan.findPageComponents, NextNodeServerSpan.findPageComponents,
NextNodeServerSpan.getLayoutOrPageModule, NextNodeServerSpan.getLayoutOrPageModule,
NextNodeServerSpan.startResponse, NextNodeServerSpan.startResponse,
NextNodeServerSpan.clientComponentLoading,
] ]
// These Spans are allowed to be always logged // These Spans are allowed to be always logged
...@@ -141,6 +143,7 @@ export const NextVanillaSpanAllowlist = [ ...@@ -141,6 +143,7 @@ export const NextVanillaSpanAllowlist = [
export const LogSpanAllowList = [ export const LogSpanAllowList = [
NextNodeServerSpan.findPageComponents, NextNodeServerSpan.findPageComponents,
NextNodeServerSpan.createComponentTree, NextNodeServerSpan.createComponentTree,
NextNodeServerSpan.clientComponentLoading,
] ]
export { export {
......
...@@ -144,6 +144,7 @@ type NextAttributeNames = ...@@ -144,6 +144,7 @@ type NextAttributeNames =
| 'next.segment' | 'next.segment'
| 'next.span_name' | 'next.span_name'
| 'next.span_type' | 'next.span_type'
| 'next.clientComponentLoadCount'
type OTELAttributeNames = `http.${string}` | `net.${string}` type OTELAttributeNames = `http.${string}` | `net.${string}`
type AttributeNames = NextAttributeNames | OTELAttributeNames type AttributeNames = NextAttributeNames | OTELAttributeNames
......
...@@ -162,6 +162,16 @@ createNextDescribe( ...@@ -162,6 +162,16 @@ createNextDescribe(
kind: 0, kind: 0,
status: { code: 0 }, status: { code: 0 },
}, },
{
attributes: {
'next.clientComponentLoadCount': 4,
},
kind: 0,
name: 'NextNodeServer.clientComponentLoading',
status: {
code: 0,
},
},
{ {
name: 'start response', name: 'start response',
attributes: { attributes: {
...@@ -771,6 +781,9 @@ createNextDescribe( ...@@ -771,6 +781,9 @@ createNextDescribe(
{ {
name: 'generateMetadata /app/[param]/rsc-fetch/page', name: 'generateMetadata /app/[param]/rsc-fetch/page',
}, },
{
name: 'NextNodeServer.clientComponentLoading',
},
{ {
name: 'start response', name: 'start response',
}, },
......
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