Unverified Commit 00e9cf9d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fixes to next.js tracing (#63673)

### What?

* fix the span name generated for webpack build-module
* fix the time base for spans reported by manualTraceChild


Closes PACK-2835
parent 7d82b682
......@@ -151,11 +151,14 @@ export class ProfilingPlugin {
(compilation: any) => {
compilation.hooks.buildModule.tap(pluginName, (module: any) => {
const moduleType = (() => {
if (!module.userRequest) {
const r = module.userRequest
if (!r || r.endsWith('!')) {
return ''
} else {
const resource = r.split('!').pop()
const match = /^[^?]+\.([^?]+)$/.exec(resource)
return match ? match[1] : ''
}
return module.userRequest.split('.').pop()
})()
const issuerModule = compilation?.moduleGraph?.getIssuer(module)
......
......@@ -81,7 +81,7 @@ import type { WebpackError } from 'webpack'
import { PAGE_TYPES } from '../../lib/page-types'
import { FAST_REFRESH_RUNTIME_RELOAD } from './messages'
const MILLISECONDS_IN_NANOSECOND = 1_000_000
const MILLISECONDS_IN_NANOSECOND = BigInt(1_000_000)
const isTestMode = !!(
process.env.NEXT_TEST_MODE ||
process.env.__NEXT_TEST_MODE ||
......@@ -429,11 +429,11 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
name: payload.spanName,
startTime:
BigInt(Math.floor(payload.startTime)) *
BigInt(MILLISECONDS_IN_NANOSECOND),
MILLISECONDS_IN_NANOSECOND,
attrs: payload.attributes,
endTime:
BigInt(Math.floor(payload.endTime)) *
BigInt(MILLISECONDS_IN_NANOSECOND),
MILLISECONDS_IN_NANOSECOND,
}
break
}
......@@ -441,10 +441,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
traceChild = {
name: payload.event,
startTime:
BigInt(payload.startTime) *
BigInt(MILLISECONDS_IN_NANOSECOND),
endTime:
BigInt(payload.endTime) * BigInt(MILLISECONDS_IN_NANOSECOND),
BigInt(payload.startTime) * MILLISECONDS_IN_NANOSECOND,
endTime: BigInt(payload.endTime) * MILLISECONDS_IN_NANOSECOND,
attrs: {
updatedModules: payload.updatedModules.map((m: string) =>
m
......@@ -541,8 +539,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
if (traceChild) {
this.hotReloaderSpan.manualTraceChild(
traceChild.name,
traceChild.startTime || process.hrtime.bigint(),
traceChild.endTime || process.hrtime.bigint(),
traceChild.startTime,
traceChild.endTime,
{ ...traceChild.attrs, clientId: payload.id }
)
}
......
......@@ -210,10 +210,10 @@ export function renderStyledStringToErrorAnsi(string: StyledString): string {
}
}
const MILLISECONDS_IN_NANOSECOND = 1_000_000
const MILLISECONDS_IN_NANOSECOND = BigInt(1_000_000)
export function msToNs(ms: number): bigint {
return BigInt(Math.floor(ms)) * BigInt(MILLISECONDS_IN_NANOSECOND)
return BigInt(Math.floor(ms)) * MILLISECONDS_IN_NANOSECOND
}
export type ChangeSubscriptions = Map<
......
......@@ -2,6 +2,7 @@ import { reporter } from './report'
import type { SpanId, TraceEvent, TraceState } from './types'
const NUM_OF_MICROSEC_IN_NANOSEC = BigInt('1000')
const NUM_OF_MILLISEC_IN_NANOSEC = BigInt('1000000')
let count = 0
const getId = () => {
count++
......@@ -102,13 +103,21 @@ export class Span {
manualTraceChild(
name: string,
// Start time in nanoseconds since epoch.
startTime: bigint,
startTime?: bigint,
// Stop time in nanoseconds since epoch.
stopTime: bigint,
stopTime?: bigint,
attrs?: Attributes
) {
const span = new Span({ name, parentId: this.id, attrs, startTime })
span.stop(stopTime)
// We need to convert the time info to the same base as hrtime since that is used usually.
const correction =
process.hrtime.bigint() - BigInt(Date.now()) * NUM_OF_MILLISEC_IN_NANOSEC
const span = new Span({
name,
parentId: this.id,
attrs,
startTime: startTime ? startTime + correction : process.hrtime.bigint(),
})
span.stop(stopTime ? stopTime + correction : process.hrtime.bigint())
}
getId() {
......
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