Unverified Commit 60422e65 authored by mknichel's avatar mknichel Committed by GitHub

[.next/trace] Serialize trace info across workers to preserve .next/trace with…

[.next/trace] Serialize trace info across workers to preserve .next/trace with webpackBuildWorker (#57761)

This PR sets up the webpack build workers (webpackBuildWorker: true) to serialize debug trace information across the worker boundary so that it can appear in the final .next/trace file at the end of the build.

Currently, when webpackBuildWorker is turned on, all traces that appear under the webpack compilation are lost. After this PR, they will appear in the trace file just like when the workers are not enabled.
parent cd821c8e
......@@ -21,7 +21,13 @@ import {
} from '../build-context'
import { createEntrypoints } from '../entries'
import loadConfig from '../../server/config'
import { trace } from '../../trace'
import {
getTraceEvents,
initializeTraceState,
trace,
type TraceEvent,
type TraceState,
} from '../../trace'
import { WEBPACK_LAYERS } from '../../lib/constants'
import { TraceEntryPointsPlugin } from '../webpack/plugins/next-trace-entrypoints-plugin'
import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin'
......@@ -331,10 +337,18 @@ export async function webpackBuildImpl(
export async function workerMain(workerData: {
compilerName: keyof typeof COMPILER_INDEXES
buildContext: typeof NextBuildContext
}) {
traceState: TraceState
}): Promise<
Awaited<ReturnType<typeof webpackBuildImpl>> & {
debugTraceEvents: TraceEvent[]
}
> {
// setup new build context from the serialized data passed from the parent
Object.assign(NextBuildContext, workerData.buildContext)
// Initialize tracer state from the parent
initializeTraceState(workerData.traceState)
// Resume plugin state
resumePluginState(NextBuildContext.pluginState)
......@@ -343,7 +357,9 @@ export async function workerMain(workerData: {
PHASE_PRODUCTION_BUILD,
NextBuildContext.dir!
)
NextBuildContext.nextBuildSpan = trace('next-build')
NextBuildContext.nextBuildSpan = trace(
`worker-main-${workerData.compilerName}`
)
const result = await webpackBuildImpl(workerData.compilerName)
const { entriesTrace, chunksTrace } = result.buildTraceContext ?? {}
......@@ -361,5 +377,6 @@ export async function workerMain(workerData: {
const entryNameFilesMap = chunksTrace.entryNameFilesMap
result.buildTraceContext!.chunksTrace!.entryNameFilesMap = entryNameFilesMap
}
return result
NextBuildContext.nextBuildSpan.stop()
return { ...result, debugTraceEvents: getTraceEvents() }
}
......@@ -6,6 +6,7 @@ import { Worker } from 'next/dist/compiled/jest-worker'
import origDebug from 'next/dist/compiled/debug'
import type { ChildProcess } from 'child_process'
import path from 'path'
import { exportTraceState, recordTraceEvents } from '../../trace'
const debug = origDebug('next:build:webpack-build')
......@@ -83,7 +84,15 @@ async function webpackBuildWithWorker(
const curResult = await worker.workerMain({
buildContext: prunedBuildContext,
compilerName,
traceState: {
...exportTraceState(),
defaultParentSpanId: nextBuildSpan?.id,
shouldSaveTraceEvents: true,
},
})
if (nextBuildSpan && curResult.debugTraceEvents) {
recordTraceEvents(curResult.debugTraceEvents)
}
// destroy worker so it's not sticking around using memory
await worker.end()
......
import { trace, flushAllTraces, Span, SpanStatus } from './trace'
import type { SpanId } from './shared'
import {
trace,
exportTraceState,
flushAllTraces,
getTraceEvents,
initializeTraceState,
recordTraceEvents,
Span,
SpanStatus,
} from './trace'
import { setGlobal } from './shared'
import type { SpanId, TraceEvent, TraceState } from './types'
export { trace, flushAllTraces, Span, setGlobal, SpanStatus }
export type { SpanId }
export {
trace,
exportTraceState,
flushAllTraces,
getTraceEvents,
initializeTraceState,
recordTraceEvents,
Span,
setGlobal,
SpanStatus,
}
export type { SpanId, TraceEvent, TraceState }
import { mkdtemp, readFile } from 'fs/promises'
import { reporter } from '.'
import { setGlobal } from '../shared'
import { join } from 'path'
import { tmpdir } from 'os'
const TRACE_EVENT = {
name: 'test-span',
duration: 321,
timestamp: Date.now(),
id: 127,
startTime: Date.now(),
}
const WEBPACK_INVALIDATED_EVENT = {
name: 'webpack-invalidated',
duration: 100,
timestamp: Date.now(),
id: 112,
startTime: Date.now(),
}
describe('Trace Reporter', () => {
describe('JSON reporter', () => {
it('should write the trace events to JSON file', async () => {
const tmpDir = await mkdtemp(join(tmpdir(), 'json-reporter'))
setGlobal('distDir', tmpDir)
setGlobal('phase', 'anything')
reporter.report(TRACE_EVENT)
await reporter.flushAll()
const traceFilename = join(tmpDir, 'trace')
const traces = JSON.parse(await readFile(traceFilename, 'utf-8'))
expect(traces.length).toEqual(1)
expect(traces[0].name).toEqual('test-span')
expect(traces[0].id).toEqual(127)
expect(traces[0].duration).toEqual(321)
expect(traces[0].traceId).toBeDefined()
})
})
describe('Telemetry reporter', () => {
it('should record telemetry event', async () => {
const recordMock = jest.fn()
const telemetryMock = {
record: recordMock,
}
setGlobal('telemetry', telemetryMock)
// This should be ignored.
reporter.report(TRACE_EVENT)
expect(recordMock).toBeCalledTimes(0)
reporter.report(WEBPACK_INVALIDATED_EVENT)
expect(recordMock).toBeCalledTimes(1)
expect(recordMock).toHaveBeenCalledWith({
eventName: 'WEBPACK_INVALIDATED',
payload: {
durationInMicroseconds: 100,
},
})
})
})
})
import type { SpanId } from '../shared'
import type { TraceEvent } from '../types'
import reportToTelemetry from './to-telemetry'
import reportToJson from './to-json'
type Reporter = {
flushAll: () => Promise<void> | void
report: (
spanName: string,
duration: number,
timestamp: number,
id: SpanId,
parentId?: SpanId,
attrs?: Object,
startTime?: number
) => void
}
import type { Reporter } from './types'
class MultiReporter implements Reporter {
private reporters: Reporter[] = []
......@@ -26,26 +14,8 @@ class MultiReporter implements Reporter {
await Promise.all(this.reporters.map((reporter) => reporter.flushAll()))
}
report(
spanName: string,
duration: number,
timestamp: number,
id: SpanId,
parentId?: SpanId,
attrs?: Object,
startTime?: number
) {
this.reporters.forEach((reporter) =>
reporter.report(
spanName,
duration,
timestamp,
id,
parentId,
attrs,
startTime
)
)
report(event: TraceEvent) {
this.reporters.forEach((reporter) => reporter.report(event))
}
}
......
......@@ -3,6 +3,7 @@ import { traceGlobals } from '../shared'
import fs from 'fs'
import path from 'path'
import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants'
import type { TraceEvent } from '../types'
const localEndpoint = {
serviceName: 'nextjs',
......@@ -10,16 +11,8 @@ const localEndpoint = {
port: 9411,
}
type Event = {
traceId: string
parentId?: number
name: string
id: number
timestamp: number
duration: number
type Event = TraceEvent & {
localEndpoint?: typeof localEndpoint
tags?: Object
startTime?: number
}
// Batch events as zipkin allows for multiple events to be sent in one go
......@@ -116,15 +109,7 @@ class RotatingWriteStream {
}
}
const reportToLocalHost = (
name: string,
duration: number,
timestamp: number,
id: number,
parentId?: number,
attrs?: Object,
startTime?: number
) => {
const reportToLocalHost = (event: TraceEvent) => {
const distDir = traceGlobals.get('distDir')
const phase = traceGlobals.get('phase')
if (!distDir || !phase) {
......@@ -136,7 +121,7 @@ const reportToLocalHost = (
}
if (!batch) {
batch = batcher(async (events) => {
batch = batcher(async (events: Event[]) => {
if (!writeStream) {
await fs.promises.mkdir(distDir, { recursive: true })
const file = path.join(distDir, 'trace')
......@@ -156,14 +141,8 @@ const reportToLocalHost = (
}
batch.report({
...event,
traceId,
parentId,
name,
id,
timestamp,
duration,
tags: attrs,
startTime,
})
}
......
import type { Telemetry } from '../../telemetry/storage'
import { traceGlobals } from '../shared'
import type { TraceEvent } from '../types'
const TRACE_EVENT_ACCESSLIST = new Map(
Object.entries({
......@@ -7,8 +8,8 @@ const TRACE_EVENT_ACCESSLIST = new Map(
})
)
const reportToTelemetry = (spanName: string, duration: number) => {
const eventName = TRACE_EVENT_ACCESSLIST.get(spanName)
const reportToTelemetry = ({ name, duration }: TraceEvent) => {
const eventName = TRACE_EVENT_ACCESSLIST.get(name)
if (!eventName) {
return
}
......
import type { TraceEvent } from '../types'
export type Reporter = {
flushAll: () => Promise<void> | void
report: (event: TraceEvent) => void
}
export type SpanId = number
let _traceGlobals: Map<any, any> = (global as any)._traceGlobals
if (!_traceGlobals) {
......
import { mkdtemp, readFile } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
import { setGlobal } from './shared'
import {
clearTraceEvents,
exportTraceState,
flushAllTraces,
getTraceEvents,
initializeTraceState,
recordTraceEvents,
trace,
} from './trace'
describe('Trace', () => {
beforeEach(() => {
initializeTraceState({
lastId: 0,
shouldSaveTraceEvents: true,
})
clearTraceEvents()
})
describe('Tracer', () => {
it('traces a block of code', async () => {
const tmpDir = await mkdtemp(join(tmpdir(), 'json-reporter'))
setGlobal('distDir', tmpDir)
setGlobal('phase', 'anything')
const root = trace('root-span', undefined, {
'some-tag': 'some-value',
})
root.traceChild('child-span').traceFn(() => null)
await root.traceChild('async-child-span').traceAsyncFn(async () => {
const delayedPromise = new Promise((resolve) => {
setTimeout(resolve, 100)
})
await delayedPromise
})
root.stop()
const traceEvents = getTraceEvents()
expect(traceEvents.length).toEqual(3)
expect(traceEvents[0].name).toEqual('child-span')
expect(traceEvents[1].name).toEqual('async-child-span')
expect(traceEvents[2].name).toEqual('root-span')
// Check that the serialized .next/trace file looks correct.
await flushAllTraces()
const traceFilename = join(tmpDir, 'trace')
const serializedTraces = JSON.parse(
await readFile(traceFilename, 'utf-8')
)
expect(serializedTraces).toMatchObject([
{
id: 2,
name: 'child-span',
parentId: 1,
startTime: expect.any(Number),
timestamp: expect.any(Number),
duration: expect.any(Number),
tags: {},
},
{
id: 3,
name: 'async-child-span',
parentId: 1,
startTime: expect.any(Number),
timestamp: expect.any(Number),
duration: expect.any(Number),
tags: {},
},
{
id: 1,
name: 'root-span',
startTime: expect.any(Number),
timestamp: expect.any(Number),
duration: expect.any(Number),
tags: {
'some-tag': 'some-value',
},
},
])
})
})
describe('Worker', () => {
it('exports and initializes trace state', () => {
const root = trace('root-span')
expect(root.id).toEqual(1)
const traceState = exportTraceState()
expect(traceState.lastId).toEqual(1)
initializeTraceState({
lastId: 101,
})
const span = trace('another-span')
expect(span.id).toEqual(102)
})
it('trace data is serializable to a worker', async () => {
const root = trace('root-span')
root.traceChild('child-span').traceFn(() => null)
root.stop()
const traceEvents = getTraceEvents()
expect(traceEvents.length).toEqual(2)
// This is a proxy check to make sure the object would be serializable
// to a worker. It will fail if the data contains some unserializable
// objects like BigInt.
const clone = JSON.parse(JSON.stringify(traceEvents))
expect(clone).toEqual(traceEvents)
})
it('correctly reports trace data from multiple workers', () => {
// This test simulates workers creating traces and propagating them
// back to the main process for recording. It doesn't use
// actual workers since they are more difficult to set up in tests.
initializeTraceState({
lastId: 5,
defaultParentSpanId: 1,
shouldSaveTraceEvents: true,
})
const worker1Span = trace('worker1')
worker1Span.traceChild('webpack-compilation1').traceFn(() => null)
worker1Span.stop()
const worker1Traces = getTraceEvents()
expect(worker1Traces.length).toEqual(2)
// Repeat for a second worker.
clearTraceEvents()
initializeTraceState({
lastId: 10,
defaultParentSpanId: 1,
shouldSaveTraceEvents: true,
})
const worker2Span = trace('worker2')
worker2Span.traceChild('webpack-compilation2').traceFn(() => null)
worker2Span.stop()
const worker2Traces = getTraceEvents()
expect(worker2Traces.length).toEqual(2)
// Now simulate the traces in the main process and record the traces
// from each worker.
clearTraceEvents()
initializeTraceState({
lastId: 0,
shouldSaveTraceEvents: true,
})
const root = trace('next-build')
root.traceChild('some-child-span').traceFn(() => null)
recordTraceEvents(worker1Traces)
expect(exportTraceState().lastId).toEqual(8)
recordTraceEvents(worker2Traces)
expect(exportTraceState().lastId).toEqual(13)
root.traceChild('another-child-span').traceFn(() => null)
root.stop()
// Check that the final output looks correct.
const allTraces = getTraceEvents()
expect(allTraces.length).toEqual(7)
const firstSpan = allTraces[0]
expect(firstSpan.name).toEqual('some-child-span')
expect(firstSpan.id).toEqual(2)
expect(firstSpan.parentId).toEqual(1)
const worker1Child = allTraces[1]
expect(worker1Child.name).toEqual('webpack-compilation1')
expect(worker1Child.id).toEqual(7)
expect(worker1Child.parentId).toEqual(6)
const worker1Root = allTraces[2]
expect(worker1Root.name).toEqual('worker1')
expect(worker1Root.id).toEqual(6)
expect(worker1Root.parentId).toEqual(1)
const worker2Child = allTraces[3]
expect(worker2Child.name).toEqual('webpack-compilation2')
expect(worker2Child.id).toEqual(12)
expect(worker2Child.parentId).toEqual(11)
const worker2Root = allTraces[4]
expect(worker2Root.name).toEqual('worker2')
expect(worker2Root.id).toEqual(11)
expect(worker2Root.parentId).toEqual(1)
const lastChildSpan = allTraces[5]
expect(lastChildSpan.name).toEqual('another-child-span')
expect(lastChildSpan.id).toEqual(14)
expect(lastChildSpan.parentId).toEqual(1)
const rootSpan = allTraces[6]
expect(rootSpan.name).toEqual('next-build')
expect(rootSpan.id).toEqual(1)
expect(rootSpan.parentId).toBeUndefined()
})
})
})
import type { SpanId } from './shared'
import { reporter } from './report'
import type { SpanId, TraceEvent, TraceState } from './types'
const NUM_OF_MICROSEC_IN_NANOSEC = BigInt('1000')
let count = 0
......@@ -7,6 +7,9 @@ const getId = () => {
count++
return count
}
let defaultParentSpanId: SpanId | undefined
let shouldSaveTraceEvents: boolean | undefined
let savedTraceEvents: TraceEvent[] = []
// eslint typescript has a bug with TS enums
/* eslint-disable no-shadow */
......@@ -40,7 +43,7 @@ export class Span {
attrs?: Object
}) {
this.name = name
this.parentId = parentId
this.parentId = parentId ?? defaultParentSpanId
this.duration = null
this.attrs = attrs ? { ...attrs } : {}
this.status = SpanStatus.Started
......@@ -66,15 +69,19 @@ export class Span {
throw new Error(`Duration is too long to express as float64: ${duration}`)
}
const timestamp = this._start / NUM_OF_MICROSEC_IN_NANOSEC
reporter.report(
this.name,
Number(duration),
Number(timestamp),
this.id,
this.parentId,
this.attrs,
this.now
)
const traceEvent: TraceEvent = {
name: this.name,
duration: Number(duration),
timestamp: Number(timestamp),
id: this.id,
parentId: this.parentId,
tags: this.attrs,
startTime: this.now,
}
reporter.report(traceEvent)
if (shouldSaveTraceEvents) {
savedTraceEvents.push(traceEvent)
}
}
traceChild(name: string, attrs?: Object) {
......@@ -123,3 +130,35 @@ export const trace = (
}
export const flushAllTraces = () => reporter.flushAll()
// This code supports workers by serializing the state of tracers when the
// worker is initialized, and serializing the trace events from the worker back
// to the main process to record when the worker is complete.
export const exportTraceState = (): TraceState => ({
defaultParentSpanId,
lastId: count,
shouldSaveTraceEvents,
})
export const initializeTraceState = (state: TraceState) => {
count = state.lastId
defaultParentSpanId = state.defaultParentSpanId
shouldSaveTraceEvents = state.shouldSaveTraceEvents
}
export function getTraceEvents(): TraceEvent[] {
return savedTraceEvents
}
export function recordTraceEvents(events: TraceEvent[]) {
for (const traceEvent of events) {
reporter.report(traceEvent)
if (traceEvent.id > count) {
count = traceEvent.id + 1
}
}
if (shouldSaveTraceEvents) {
savedTraceEvents.push(...events)
}
}
export const clearTraceEvents = () => (savedTraceEvents = [])
export type SpanId = number
export interface TraceState {
lastId: number
defaultParentSpanId?: SpanId
shouldSaveTraceEvents?: boolean
}
export type TraceEvent = {
traceId?: string
parentId?: SpanId
name: string
id: SpanId
timestamp: number
duration: number
tags?: Object
startTime?: number
}
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