2021-10-07 01:46:46 +02:00
|
|
|
import { NormalModule } from 'next/dist/compiled/webpack/webpack'
|
2021-09-13 15:49:29 +02:00
|
|
|
import { Span } from '../../../trace'
|
2021-09-21 19:17:16 +02:00
|
|
|
import type webpack from 'webpack'
|
2019-08-18 21:45:39 +02:00
|
|
|
|
|
|
|
const pluginName = 'ProfilingPlugin'
|
2021-09-21 19:17:16 +02:00
|
|
|
export const spans = new WeakMap<webpack.Compilation | webpack.Compiler, Span>()
|
2021-09-20 22:19:00 +02:00
|
|
|
const moduleSpansByCompilation = new WeakMap<
|
2021-09-21 19:17:16 +02:00
|
|
|
webpack.Compilation,
|
2021-09-20 22:19:00 +02:00
|
|
|
WeakMap<webpack.Module, Span>
|
|
|
|
>()
|
2021-09-13 15:49:29 +02:00
|
|
|
export const webpackInvalidSpans = new WeakMap<any, Span>()
|
2019-08-18 21:45:39 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
export class ProfilingPlugin {
|
2021-01-29 11:32:26 +01:00
|
|
|
compiler: any
|
2021-08-25 10:47:16 +02:00
|
|
|
runWebpackSpan: Span
|
2021-01-29 11:32:26 +01:00
|
|
|
|
2021-08-25 10:47:16 +02:00
|
|
|
constructor({ runWebpackSpan }: { runWebpackSpan: Span }) {
|
2021-08-17 09:18:47 +02:00
|
|
|
this.runWebpackSpan = runWebpackSpan
|
|
|
|
}
|
2019-08-18 21:45:39 +02:00
|
|
|
apply(compiler: any) {
|
2021-01-29 11:32:26 +01:00
|
|
|
this.traceTopLevelHooks(compiler)
|
|
|
|
this.traceCompilationHooks(compiler)
|
|
|
|
this.compiler = compiler
|
|
|
|
}
|
2019-08-18 21:45:39 +02:00
|
|
|
|
2021-01-29 11:32:26 +01:00
|
|
|
traceHookPair(
|
2021-09-17 09:34:37 +02:00
|
|
|
spanName: string | (() => string),
|
2021-01-29 11:32:26 +01:00
|
|
|
startHook: any,
|
|
|
|
stopHook: any,
|
2021-08-17 09:18:47 +02:00
|
|
|
{
|
|
|
|
parentSpan,
|
|
|
|
attrs,
|
2021-09-13 15:49:29 +02:00
|
|
|
onStart,
|
|
|
|
onStop,
|
2021-08-17 09:18:47 +02:00
|
|
|
}: {
|
2021-08-25 10:47:16 +02:00
|
|
|
parentSpan?: () => Span
|
2021-08-17 09:18:47 +02:00
|
|
|
attrs?: any
|
2021-09-20 22:19:00 +02:00
|
|
|
onStart?: (span: Span, ...params: any[]) => void
|
2021-09-13 15:49:29 +02:00
|
|
|
onStop?: () => void
|
2021-08-17 09:18:47 +02:00
|
|
|
} = {}
|
2021-01-29 11:32:26 +01:00
|
|
|
) {
|
|
|
|
let span: Span | undefined
|
2021-09-20 22:19:00 +02:00
|
|
|
startHook.tap(
|
|
|
|
{ name: pluginName, stage: -Infinity },
|
|
|
|
(...params: any[]) => {
|
|
|
|
const name = typeof spanName === 'function' ? spanName() : spanName
|
|
|
|
const attributes = attrs ? attrs(...params) : attrs
|
|
|
|
span = parentSpan
|
|
|
|
? parentSpan().traceChild(name, attributes)
|
|
|
|
: this.runWebpackSpan.traceChild(name, attributes)
|
|
|
|
|
|
|
|
if (onStart) onStart(span, ...params)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stopHook.tap({ name: pluginName, stage: Infinity }, () => {
|
2021-02-12 11:17:38 +01:00
|
|
|
// `stopHook` may be triggered when `startHook` has not in cases
|
|
|
|
// where `stopHook` is used as the terminating event for more
|
|
|
|
// than one pair of hooks.
|
|
|
|
if (!span) {
|
|
|
|
return
|
|
|
|
}
|
2021-09-13 15:49:29 +02:00
|
|
|
|
|
|
|
if (onStop) onStop()
|
2021-08-17 09:18:47 +02:00
|
|
|
span.stop()
|
2019-08-18 21:45:39 +02:00
|
|
|
})
|
2021-01-29 11:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
traceTopLevelHooks(compiler: any) {
|
|
|
|
this.traceHookPair(
|
2021-08-17 09:18:47 +02:00
|
|
|
'webpack-compilation',
|
2021-09-20 22:19:00 +02:00
|
|
|
compiler.hooks.compilation,
|
2021-10-07 01:46:46 +02:00
|
|
|
compiler.hooks.afterCompile,
|
2021-08-17 09:18:47 +02:00
|
|
|
{
|
2021-09-13 15:49:29 +02:00
|
|
|
parentSpan: () =>
|
|
|
|
webpackInvalidSpans.get(compiler) || this.runWebpackSpan,
|
2021-08-17 09:18:47 +02:00
|
|
|
attrs: () => ({ name: compiler.name }),
|
2021-09-20 22:19:00 +02:00
|
|
|
onStart: (span, compilation) => {
|
|
|
|
spans.set(compilation, span)
|
|
|
|
spans.set(compiler, span)
|
|
|
|
moduleSpansByCompilation.set(compilation, new WeakMap())
|
|
|
|
},
|
2021-08-17 09:18:47 +02:00
|
|
|
}
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
2021-08-17 09:18:47 +02:00
|
|
|
|
2021-02-10 00:21:38 +01:00
|
|
|
if (compiler.options.mode === 'development') {
|
|
|
|
this.traceHookPair(
|
2021-09-17 09:34:37 +02:00
|
|
|
() => `webpack-invalidated-${compiler.name}`,
|
2021-02-10 00:21:38 +01:00
|
|
|
compiler.hooks.invalid,
|
2021-06-25 13:55:46 +02:00
|
|
|
compiler.hooks.done,
|
2021-09-13 15:49:29 +02:00
|
|
|
{
|
|
|
|
onStart: (span) => webpackInvalidSpans.set(compiler, span),
|
|
|
|
onStop: () => webpackInvalidSpans.delete(compiler),
|
|
|
|
attrs: (fileName: any) => ({
|
|
|
|
trigger: fileName || 'manual',
|
|
|
|
}),
|
|
|
|
}
|
2021-02-10 00:21:38 +01:00
|
|
|
)
|
|
|
|
}
|
2021-01-29 11:32:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
traceCompilationHooks(compiler: any) {
|
2021-09-15 20:06:24 +02:00
|
|
|
this.traceHookPair('emit', compiler.hooks.emit, compiler.hooks.afterEmit, {
|
|
|
|
parentSpan: () =>
|
|
|
|
webpackInvalidSpans.get(compiler) || this.runWebpackSpan,
|
|
|
|
})
|
|
|
|
|
2021-10-07 01:46:46 +02:00
|
|
|
this.traceHookPair('make', compiler.hooks.make, compiler.hooks.finishMake, {
|
|
|
|
parentSpan: () =>
|
|
|
|
webpackInvalidSpans.get(compiler) || this.runWebpackSpan,
|
|
|
|
})
|
2021-02-10 00:21:38 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
compiler.hooks.compilation.tap(pluginName, (compilation: any) => {
|
|
|
|
compilation.hooks.buildModule.tap(pluginName, (module: any) => {
|
2021-09-20 22:19:00 +02:00
|
|
|
const compilationSpan = spans.get(compilation)
|
|
|
|
if (!compilationSpan) {
|
2021-01-29 11:32:26 +01:00
|
|
|
return
|
|
|
|
}
|
Telemetry-compatible tracing (#22713)
A number of changes here. I recommend viewing the diff with the <a href="?w=1">whitespace flag enabled</a>.
- OpenTelemetry is replaced with a custom and lightweight tracing solution.
- Three trace targets are currently supported: console, Zipkin, and NextJS.
- Tracing is now governed by environment variables rather than `--require instrument.js`.
+ `TRACE_TARGET`: one of `CONSOLE`, `ZIPKIN`, or `TELEMETRY`; defaults to `TELEMETRY` if unset or invalid.
+ `TRACE_ID`: an 8-byte hex-encoded value used as the Zipkin trace ID; if not provided, this value will be randomly generated and passed down to subprocesses.
Other sundry:
- I'm missing something, probably a setup step, with the Zipkin target. Traces are captured successfully, but you have to manually enter the Trace ID in order to view the trace - it doesn't show up in queries.
- I'm generally unhappy with [this commit](https://github.com/vercel/next.js/pull/22713/commits/235cedcb3ead76b630b4c8aa695f904489da2831). It is... untidy to provide a telemetry object via `setGlobal`, but I don't have a ready alternative. Is `distDir` strictly required when creating a new Telemetry object? I didn't dig too deep here.
As noted, there are a lot of changes, so it'd be great if a reviewer could:
- [ ] pull down the branch and try to break it
- [ ] check the Zipkin traces and identify possible regressions in the functionality
Closes #22570
Fixes #22574
2021-03-10 22:00:20 +01:00
|
|
|
|
2021-08-16 22:05:57 +02:00
|
|
|
const moduleType = (() => {
|
|
|
|
if (!module.userRequest) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return module.userRequest.split('.').pop()
|
|
|
|
})()
|
|
|
|
|
2021-08-18 14:22:53 +02:00
|
|
|
const issuerModule = compilation?.moduleGraph?.getIssuer(module)
|
|
|
|
|
2021-08-25 10:47:16 +02:00
|
|
|
let span: Span
|
|
|
|
|
2021-09-20 22:19:00 +02:00
|
|
|
const moduleSpans = moduleSpansByCompilation.get(compilation)
|
2021-08-25 10:47:16 +02:00
|
|
|
const spanName = `build-module${moduleType ? `-${moduleType}` : ''}`
|
|
|
|
const issuerSpan: Span | undefined =
|
2021-09-20 22:19:00 +02:00
|
|
|
issuerModule && moduleSpans?.get(issuerModule)
|
2021-08-25 10:47:16 +02:00
|
|
|
if (issuerSpan) {
|
|
|
|
span = issuerSpan.traceChild(spanName)
|
|
|
|
} else {
|
2021-09-20 22:19:00 +02:00
|
|
|
span = compilationSpan.traceChild(spanName)
|
2021-08-25 10:47:16 +02:00
|
|
|
}
|
Telemetry-compatible tracing (#22713)
A number of changes here. I recommend viewing the diff with the <a href="?w=1">whitespace flag enabled</a>.
- OpenTelemetry is replaced with a custom and lightweight tracing solution.
- Three trace targets are currently supported: console, Zipkin, and NextJS.
- Tracing is now governed by environment variables rather than `--require instrument.js`.
+ `TRACE_TARGET`: one of `CONSOLE`, `ZIPKIN`, or `TELEMETRY`; defaults to `TELEMETRY` if unset or invalid.
+ `TRACE_ID`: an 8-byte hex-encoded value used as the Zipkin trace ID; if not provided, this value will be randomly generated and passed down to subprocesses.
Other sundry:
- I'm missing something, probably a setup step, with the Zipkin target. Traces are captured successfully, but you have to manually enter the Trace ID in order to view the trace - it doesn't show up in queries.
- I'm generally unhappy with [this commit](https://github.com/vercel/next.js/pull/22713/commits/235cedcb3ead76b630b4c8aa695f904489da2831). It is... untidy to provide a telemetry object via `setGlobal`, but I don't have a ready alternative. Is `distDir` strictly required when creating a new Telemetry object? I didn't dig too deep here.
As noted, there are a lot of changes, so it'd be great if a reviewer could:
- [ ] pull down the branch and try to break it
- [ ] check the Zipkin traces and identify possible regressions in the functionality
Closes #22570
Fixes #22574
2021-03-10 22:00:20 +01:00
|
|
|
span.setAttribute('name', module.userRequest)
|
2021-09-20 22:19:00 +02:00
|
|
|
moduleSpans!.set(module, span)
|
2021-01-10 02:12:13 +01:00
|
|
|
})
|
2019-08-18 21:45:39 +02:00
|
|
|
|
2021-10-09 13:18:58 +02:00
|
|
|
const moduleHooks = NormalModule.getCompilationHooks(compilation)
|
|
|
|
// @ts-ignore TODO: remove ignore when using webpack 5 types
|
|
|
|
moduleHooks.readResource.for(undefined).intercept({
|
|
|
|
register(tapInfo: any) {
|
|
|
|
const fn = tapInfo.fn
|
|
|
|
tapInfo.fn = (loaderContext: any, callback: any) => {
|
|
|
|
const moduleSpan =
|
|
|
|
loaderContext.currentTraceSpan.traceChild(`read-resource`)
|
|
|
|
fn(loaderContext, (err: any, result: any) => {
|
|
|
|
moduleSpan.stop()
|
|
|
|
callback(err, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tapInfo
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
moduleHooks.loader.tap(pluginName, (loaderContext: any, module: any) => {
|
|
|
|
const moduleSpan = moduleSpansByCompilation
|
|
|
|
.get(compilation)
|
|
|
|
?.get(module)
|
|
|
|
loaderContext.currentTraceSpan = moduleSpan
|
|
|
|
})
|
2019-08-18 21:45:39 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
|
2021-09-20 22:19:00 +02:00
|
|
|
moduleSpansByCompilation?.get(compilation)?.get(module)?.stop()
|
2019-08-18 21:45:39 +02:00
|
|
|
})
|
2021-01-29 11:32:26 +01:00
|
|
|
|
2021-10-07 01:46:46 +02:00
|
|
|
this.traceHookPair(
|
|
|
|
'webpack-compilation-seal',
|
|
|
|
compilation.hooks.seal,
|
|
|
|
compilation.hooks.afterSeal,
|
|
|
|
{ parentSpan: () => spans.get(compilation)! }
|
|
|
|
)
|
|
|
|
|
|
|
|
compilation.hooks.addEntry.tap(pluginName, (entry: any) => {
|
|
|
|
const compilationSpan = spans.get(compilation)
|
|
|
|
if (!compilationSpan) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const addEntrySpan = compilationSpan.traceChild('add-entry')
|
|
|
|
addEntrySpan.setAttribute('request', entry.request)
|
|
|
|
spans.set(entry, addEntrySpan)
|
|
|
|
})
|
|
|
|
|
|
|
|
compilation.hooks.succeedEntry.tap(pluginName, (entry: any) => {
|
|
|
|
spans.get(entry)?.stop()
|
|
|
|
})
|
2021-09-15 20:06:24 +02:00
|
|
|
|
2021-01-29 11:32:26 +01:00
|
|
|
this.traceHookPair(
|
|
|
|
'webpack-compilation-chunk-graph',
|
|
|
|
compilation.hooks.beforeChunks,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.afterChunks,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
|
|
|
this.traceHookPair(
|
|
|
|
'webpack-compilation-optimize',
|
|
|
|
compilation.hooks.optimize,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.reviveModules,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
2021-08-17 09:18:47 +02:00
|
|
|
this.traceHookPair(
|
2021-01-29 11:32:26 +01:00
|
|
|
'webpack-compilation-optimize-modules',
|
|
|
|
compilation.hooks.optimizeModules,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.afterOptimizeModules,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
2021-08-17 09:18:47 +02:00
|
|
|
this.traceHookPair(
|
2021-01-29 11:32:26 +01:00
|
|
|
'webpack-compilation-optimize-chunks',
|
|
|
|
compilation.hooks.optimizeChunks,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.afterOptimizeChunks,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
|
|
|
this.traceHookPair(
|
|
|
|
'webpack-compilation-optimize-tree',
|
|
|
|
compilation.hooks.optimizeTree,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.afterOptimizeTree,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
|
|
|
this.traceHookPair(
|
|
|
|
'webpack-compilation-hash',
|
|
|
|
compilation.hooks.beforeHash,
|
2021-08-17 09:18:47 +02:00
|
|
|
compilation.hooks.afterHash,
|
2021-09-20 22:19:00 +02:00
|
|
|
{ parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
|
2021-01-29 11:32:26 +01:00
|
|
|
)
|
2019-08-18 21:45:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|