Unverified Commit 36602e7f authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Debug tracing: add updated modules and page to HMR span (#53698)

This adds tags for `updatedModules` (an array of relative paths to changed modules that caused the hmr update) and `page` (the path to the updated page) to the `client-hmr-latency` event.

Test Plan: Ran `next dev` in a test app, changed a source file, and observed that the `client-hmr-latency` span in `.next/trace` has correct `page` and `updatedModules` tags.
parent baef523f
...@@ -188,17 +188,17 @@ function handleErrors(errors: any) { ...@@ -188,17 +188,17 @@ function handleErrors(errors: any) {
let startLatency: any = undefined let startLatency: any = undefined
function onBeforeFastRefresh(hasUpdates: any) { function onBeforeFastRefresh(updatedModules: string[]) {
if (hasUpdates) { if (updatedModules.length > 0) {
// Only trigger a pending state if we have updates to apply // Only trigger a pending state if we have updates to apply
// (cf. onFastRefresh) // (cf. onFastRefresh)
onBeforeRefresh() onBeforeRefresh()
} }
} }
function onFastRefresh(hasUpdates: any) { function onFastRefresh(updatedModules: string[]) {
onBuildOk() onBuildOk()
if (hasUpdates) { if (updatedModules.length > 0) {
// Only complete a pending state if we applied updates // Only complete a pending state if we applied updates
// (cf. onBeforeFastRefresh) // (cf. onBeforeFastRefresh)
onRefresh() onRefresh()
...@@ -214,6 +214,8 @@ function onFastRefresh(hasUpdates: any) { ...@@ -214,6 +214,8 @@ function onFastRefresh(hasUpdates: any) {
id: window.__nextDevClientId, id: window.__nextDevClientId,
startTime: startLatency, startTime: startLatency,
endTime: endLatency, endTime: endLatency,
page: window.location.pathname,
updatedModules,
}) })
) )
if (self.__NEXT_HMR_LATENCY_CB) { if (self.__NEXT_HMR_LATENCY_CB) {
...@@ -329,7 +331,10 @@ function afterApplyUpdates(fn: () => void) { ...@@ -329,7 +331,10 @@ function afterApplyUpdates(fn: () => void) {
} }
// Attempt to update code on the fly, fall back to a hard reload. // Attempt to update code on the fly, fall back to a hard reload.
function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) { function tryApplyUpdates(
onBeforeHotUpdate: ((updatedModules: string[]) => unknown) | undefined,
onHotUpdateSuccess: (updatedModules: string[]) => unknown
) {
// @ts-expect-error TODO: module.hot exists but type needs to be added. Can't use `as any` here as webpack parses for `module.hot` calls. // @ts-expect-error TODO: module.hot exists but type needs to be added. Can't use `as any` here as webpack parses for `module.hot` calls.
if (!module.hot) { if (!module.hot) {
// HotModuleReplacementPlugin is not in Webpack configuration. // HotModuleReplacementPlugin is not in Webpack configuration.
...@@ -343,7 +348,7 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) { ...@@ -343,7 +348,7 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) {
return return
} }
function handleApplyUpdates(err: any, updatedModules: any) { function handleApplyUpdates(err: any, updatedModules: string[] | null) {
if (err || hadRuntimeError || !updatedModules) { if (err || hadRuntimeError || !updatedModules) {
if (err) { if (err) {
console.warn( console.warn(
...@@ -363,18 +368,17 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) { ...@@ -363,18 +368,17 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) {
return return
} }
const hasUpdates = Boolean(updatedModules.length)
if (typeof onHotUpdateSuccess === 'function') { if (typeof onHotUpdateSuccess === 'function') {
// Maybe we want to do something. // Maybe we want to do something.
onHotUpdateSuccess(hasUpdates) onHotUpdateSuccess(updatedModules)
} }
if (isUpdateAvailable()) { if (isUpdateAvailable()) {
// While we were updating, there was a new update! Do it again. // While we were updating, there was a new update! Do it again.
// However, this time, don't trigger a pending refresh state. // However, this time, don't trigger a pending refresh state.
tryApplyUpdates( tryApplyUpdates(
hasUpdates ? undefined : onBeforeHotUpdate, updatedModules.length > 0 ? undefined : onBeforeHotUpdate,
hasUpdates ? onBuildOk : onHotUpdateSuccess updatedModules.length > 0 ? onBuildOk : onHotUpdateSuccess
) )
} else { } else {
onBuildOk() onBuildOk()
...@@ -399,8 +403,7 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) { ...@@ -399,8 +403,7 @@ function tryApplyUpdates(onBeforeHotUpdate: any, onHotUpdateSuccess: any) {
} }
if (typeof onBeforeHotUpdate === 'function') { if (typeof onBeforeHotUpdate === 'function') {
const hasUpdates = Boolean(updatedModules.length) onBeforeHotUpdate(updatedModules)
onBeforeHotUpdate(hasUpdates)
} }
// @ts-expect-error TODO: module.hot exists but type needs to be added. Can't use `as any` here as webpack parses for `module.hot` calls. // @ts-expect-error TODO: module.hot exists but type needs to be added. Can't use `as any` here as webpack parses for `module.hot` calls.
return module.hot.apply() return module.hot.apply()
......
...@@ -364,7 +364,7 @@ export default class HotReloader { ...@@ -364,7 +364,7 @@ export default class HotReloader {
name: string name: string
startTime?: bigint startTime?: bigint
endTime?: bigint endTime?: bigint
attrs?: Record<string, number | string> attrs?: Record<string, number | string | undefined | string[]>
} }
| undefined | undefined
...@@ -374,6 +374,10 @@ export default class HotReloader { ...@@ -374,6 +374,10 @@ export default class HotReloader {
name: payload.event, name: payload.event,
startTime: BigInt(payload.startTime) * BigInt(1000 * 1000), startTime: BigInt(payload.startTime) * BigInt(1000 * 1000),
endTime: BigInt(payload.endTime) * BigInt(1000 * 1000), endTime: BigInt(payload.endTime) * BigInt(1000 * 1000),
attrs: {
updatedModules: payload.updatedModules,
page: payload.page,
},
} }
break break
} }
......
...@@ -19,11 +19,13 @@ export class Span { ...@@ -19,11 +19,13 @@ export class Span {
name: string name: string
id: SpanId id: SpanId
parentId?: SpanId parentId?: SpanId
// Duration of the span in *microseconds*.
duration: number | null duration: number | null
attrs: { [key: string]: any } attrs: { [key: string]: any }
status: SpanStatus status: SpanStatus
now: number now: number
// Number of nanoseconds since epoch.
_start: bigint _start: bigint
constructor({ constructor({
...@@ -81,7 +83,9 @@ export class Span { ...@@ -81,7 +83,9 @@ export class Span {
manualTraceChild( manualTraceChild(
name: string, name: string,
// Start time in nanoseconds since epoch.
startTime: bigint, startTime: bigint,
// Stop time in nanoseconds since epoch.
stopTime: bigint, stopTime: bigint,
attrs?: Object attrs?: Object
) { ) {
......
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