-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add otel span for client component loading #62296
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ import { | |
getRedirectStatusCodeFromError, | ||
} from '../../client/components/redirect' | ||
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 { FlightRenderResult } from './flight-render-result' | ||
import { | ||
|
@@ -615,14 +615,71 @@ async function renderToHTMLOrFlightImpl( | |
enableTainting, | ||
} = 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 | ||
// react-server-dom-webpack. This is a hack until we find a better way. | ||
if (ComponentMod.__next_app__) { | ||
// @ts-ignore | ||
globalThis.__next_require__ = ComponentMod.__next_app__.require | ||
globalThis.__next_require__ = (...args: any[]) => { | ||
if (clientComponentLoadStart === 0) { | ||
clientComponentLoadStart = Date.now() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there can be gaps in the timeline? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just followed prior art of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Let's follow then what they do. |
||
} | ||
|
||
const startTime = Date.now() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One assumption that we are making here is that all of these require times are sequential. Is that true? Otherwise we will get bigger values than that it actually takes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The span time time should be the duration of the sum of individual load times even if spread out over time |
||
try { | ||
clientComponentLoadCount += 1 | ||
return ComponentMod.__next_app__.require(...args) | ||
} finally { | ||
clientComponentLoadTimes += Date.now() - startTime | ||
} | ||
} | ||
|
||
// @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 = {} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if this recursive? Or only top-level? If recursive - we need to expect 100s to 1000s of calls here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea should be ok as we're only tracking the total sum and not each individual call