Unverified Commit e90825ad authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix compilation relationship (#29174)

* fix compilation relationship

* fix compilation relationship

* Ensure loader span is provided correctly

* Fix webpack 4 childcompiler run

* Use compilation hook instead of make hook
Co-authored-by: 's avatarTim Neutkens <timneutkens@me.com>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 0568d242
...@@ -28,11 +28,12 @@ export type ClientBuildManifest = Record<string, string[]> ...@@ -28,11 +28,12 @@ export type ClientBuildManifest = Record<string, string[]>
// reduced version to send to the client. // reduced version to send to the client.
function generateClientManifest( function generateClientManifest(
compiler: any, compiler: any,
compilation: any,
assetMap: BuildManifest, assetMap: BuildManifest,
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
): string { ): string {
const compilerSpan = spans.get(compiler) const compilationSpan = spans.get(compilation) || spans.get(compiler)
const genClientManifestSpan = compilerSpan?.traceChild( const genClientManifestSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest' 'NextJsBuildManifest-generateClientManifest'
) )
...@@ -115,8 +116,8 @@ export default class BuildManifestPlugin { ...@@ -115,8 +116,8 @@ export default class BuildManifestPlugin {
} }
createAssets(compiler: any, compilation: any, assets: any) { createAssets(compiler: any, compilation: any, assets: any) {
const compilerSpan = spans.get(compiler) const compilationSpan = spans.get(compilation) || spans.get(compiler)
const createAssetsSpan = compilerSpan?.traceChild( const createAssetsSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-createassets' 'NextJsBuildManifest-createassets'
) )
return createAssetsSpan?.traceFn(() => { return createAssetsSpan?.traceFn(() => {
...@@ -230,6 +231,7 @@ export default class BuildManifestPlugin { ...@@ -230,6 +231,7 @@ export default class BuildManifestPlugin {
assets[clientManifestPath] = new sources.RawSource( assets[clientManifestPath] = new sources.RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest( `self.__BUILD_MANIFEST = ${generateClientManifest(
compiler, compiler,
compilation,
assetMap, assetMap,
this.rewrites this.rewrites
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()` )};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
......
...@@ -115,9 +115,10 @@ export default class BuildStatsPlugin { ...@@ -115,9 +115,10 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync( compiler.hooks.done.tapAsync(
'NextJsBuildStats', 'NextJsBuildStats',
async (stats, callback) => { async (stats, callback) => {
const compilerSpan = spans.get(compiler) const compilationSpan =
spans.get(stats.compilation) || spans.get(compiler)
try { try {
const writeStatsSpan = compilerSpan!.traceChild('NextJsBuildStats') const writeStatsSpan = compilationSpan!.traceChild('NextJsBuildStats')
await writeStatsSpan.traceAsyncFn(() => { await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const statsJson = reduceSize( const statsJson = reduceSize(
......
...@@ -69,8 +69,9 @@ export class CssMinimizerPlugin { ...@@ -69,8 +69,9 @@ export class CssMinimizerPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
}, },
async (assets: any) => { async (assets: any) => {
const compilerSpan = spans.get(compiler) const compilationSpan =
const cssMinimizerSpan = compilerSpan!.traceChild( spans.get(compilation) || spans.get(compiler)
const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin' 'css-minimizer-plugin'
) )
cssMinimizerSpan.setAttribute('webpackVersion', 5) cssMinimizerSpan.setAttribute('webpackVersion', 5)
...@@ -114,8 +115,8 @@ export class CssMinimizerPlugin { ...@@ -114,8 +115,8 @@ export class CssMinimizerPlugin {
compilation.hooks.optimizeChunkAssets.tapPromise( compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin', 'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => { (chunks: webpack.compilation.Chunk[]) => {
const compilerSpan = spans.get(compiler) const compilationSpan = spans.get(compilation) || spans.get(compiler)
const cssMinimizerSpan = compilerSpan!.traceChild( const cssMinimizerSpan = compilationSpan!.traceChild(
'css-minimizer-plugin' 'css-minimizer-plugin'
) )
cssMinimizerSpan.setAttribute('webpackVersion', 4) cssMinimizerSpan.setAttribute('webpackVersion', 4)
......
...@@ -301,8 +301,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -301,8 +301,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
if (isWebpack5) { if (isWebpack5) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilerSpan = spans.get(compiler)! const compilationSpan = spans.get(compilation) || spans.get(compiler)!
const traceEntrypointsPluginSpan = compilerSpan.traceChild( const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin' 'next-trace-entrypoint-plugin'
) )
traceEntrypointsPluginSpan.traceFn(() => { traceEntrypointsPluginSpan.traceFn(() => {
...@@ -327,8 +327,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -327,8 +327,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}) })
} else { } else {
compiler.hooks.emit.tap(PLUGIN_NAME, (compilation: any) => { compiler.hooks.emit.tap(PLUGIN_NAME, (compilation: any) => {
const compilerSpan = spans.get(compiler)! const compilationSpan = spans.get(compilation)! || spans.get(compiler)
const traceEntrypointsPluginSpan = compilerSpan.traceChild( const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin' 'next-trace-entrypoint-plugin'
) )
traceEntrypointsPluginSpan.traceFn(() => { traceEntrypointsPluginSpan.traceFn(() => {
...@@ -341,8 +341,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin { ...@@ -341,8 +341,8 @@ export class TraceEntryPointsPlugin implements webpack.Plugin {
}) })
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const compilerSpan = spans.get(compiler)! const compilationSpan = spans.get(compilation)! || spans.get(compiler)
const traceEntrypointsPluginSpan = compilerSpan.traceChild( const traceEntrypointsPluginSpan = compilationSpan.traceChild(
'next-trace-entrypoint-plugin' 'next-trace-entrypoint-plugin'
) )
traceEntrypointsPluginSpan.traceFn(() => traceEntrypointsPluginSpan.traceFn(() =>
......
...@@ -2,7 +2,14 @@ import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack' ...@@ -2,7 +2,14 @@ import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { Span } from '../../../trace' import { Span } from '../../../trace'
const pluginName = 'ProfilingPlugin' const pluginName = 'ProfilingPlugin'
export const spans = new WeakMap<any, Span>() export const spans = new WeakMap<
webpack.compilation.Compilation | webpack.Compiler,
Span
>()
const moduleSpansByCompilation = new WeakMap<
webpack.compilation.Compilation,
WeakMap<webpack.Module, Span>
>()
export const webpackInvalidSpans = new WeakMap<any, Span>() export const webpackInvalidSpans = new WeakMap<any, Span>()
function getNormalModuleLoaderHook(compilation: any) { function getNormalModuleLoaderHook(compilation: any) {
...@@ -39,21 +46,24 @@ export class ProfilingPlugin { ...@@ -39,21 +46,24 @@ export class ProfilingPlugin {
}: { }: {
parentSpan?: () => Span parentSpan?: () => Span
attrs?: any attrs?: any
onStart?: (span: Span) => void onStart?: (span: Span, ...params: any[]) => void
onStop?: () => void onStop?: () => void
} = {} } = {}
) { ) {
let span: Span | undefined let span: Span | undefined
startHook.tap(pluginName, (...params: any[]) => { startHook.tap(
const name = typeof spanName === 'function' ? spanName() : spanName { name: pluginName, stage: -Infinity },
const attributes = attrs ? attrs(...params) : attrs (...params: any[]) => {
span = parentSpan const name = typeof spanName === 'function' ? spanName() : spanName
? parentSpan().traceChild(name, attributes) const attributes = attrs ? attrs(...params) : attrs
: this.runWebpackSpan.traceChild(name, attributes) span = parentSpan
? parentSpan().traceChild(name, attributes)
if (onStart) onStart(span) : this.runWebpackSpan.traceChild(name, attributes)
})
stopHook.tap(pluginName, () => { if (onStart) onStart(span, ...params)
}
)
stopHook.tap({ name: pluginName, stage: Infinity }, () => {
// `stopHook` may be triggered when `startHook` has not in cases // `stopHook` may be triggered when `startHook` has not in cases
// where `stopHook` is used as the terminating event for more // where `stopHook` is used as the terminating event for more
// than one pair of hooks. // than one pair of hooks.
...@@ -69,13 +79,17 @@ export class ProfilingPlugin { ...@@ -69,13 +79,17 @@ export class ProfilingPlugin {
traceTopLevelHooks(compiler: any) { traceTopLevelHooks(compiler: any) {
this.traceHookPair( this.traceHookPair(
'webpack-compilation', 'webpack-compilation',
isWebpack5 ? compiler.hooks.beforeCompile : compiler.hooks.compile, compiler.hooks.compilation,
isWebpack5 ? compiler.hooks.afterCompile : compiler.hooks.done, isWebpack5 ? compiler.hooks.afterCompile : compiler.hooks.done,
{ {
parentSpan: () => parentSpan: () =>
webpackInvalidSpans.get(compiler) || this.runWebpackSpan, webpackInvalidSpans.get(compiler) || this.runWebpackSpan,
attrs: () => ({ name: compiler.name }), attrs: () => ({ name: compiler.name }),
onStart: (span) => spans.set(compiler, span), onStart: (span, compilation) => {
spans.set(compilation, span)
spans.set(compiler, span)
moduleSpansByCompilation.set(compilation, new WeakMap())
},
} }
) )
...@@ -115,8 +129,8 @@ export class ProfilingPlugin { ...@@ -115,8 +129,8 @@ export class ProfilingPlugin {
compiler.hooks.compilation.tap(pluginName, (compilation: any) => { compiler.hooks.compilation.tap(pluginName, (compilation: any) => {
compilation.hooks.buildModule.tap(pluginName, (module: any) => { compilation.hooks.buildModule.tap(pluginName, (module: any) => {
const compilerSpan = spans.get(compiler) const compilationSpan = spans.get(compilation)
if (!compilerSpan) { if (!compilationSpan) {
return return
} }
...@@ -132,28 +146,31 @@ export class ProfilingPlugin { ...@@ -132,28 +146,31 @@ export class ProfilingPlugin {
let span: Span let span: Span
const moduleSpans = moduleSpansByCompilation.get(compilation)
const spanName = `build-module${moduleType ? `-${moduleType}` : ''}` const spanName = `build-module${moduleType ? `-${moduleType}` : ''}`
const issuerSpan: Span | undefined = const issuerSpan: Span | undefined =
issuerModule && spans.get(issuerModule) issuerModule && moduleSpans?.get(issuerModule)
if (issuerSpan) { if (issuerSpan) {
span = issuerSpan.traceChild(spanName) span = issuerSpan.traceChild(spanName)
} else { } else {
span = compilerSpan.traceChild(spanName) span = compilationSpan.traceChild(spanName)
} }
span.setAttribute('name', module.userRequest) span.setAttribute('name', module.userRequest)
spans.set(module, span) moduleSpans!.set(module, span)
}) })
getNormalModuleLoaderHook(compilation).tap( getNormalModuleLoaderHook(compilation).tap(
pluginName, pluginName,
(loaderContext: any, module: any) => { (loaderContext: any, module: any) => {
const moduleSpan = spans.get(module) const moduleSpan = moduleSpansByCompilation
.get(compilation)
?.get(module)
loaderContext.currentTraceSpan = moduleSpan loaderContext.currentTraceSpan = moduleSpan
} }
) )
compilation.hooks.succeedModule.tap(pluginName, (module: any) => { compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
spans.get(module)?.stop() moduleSpansByCompilation?.get(compilation)?.get(module)?.stop()
}) })
if (isWebpack5) { if (isWebpack5) {
...@@ -161,7 +178,7 @@ export class ProfilingPlugin { ...@@ -161,7 +178,7 @@ export class ProfilingPlugin {
'webpack-compilation-seal', 'webpack-compilation-seal',
compilation.hooks.seal, compilation.hooks.seal,
compilation.hooks.afterSeal, compilation.hooks.afterSeal,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation)! }
) )
this.traceHookPair( this.traceHookPair(
...@@ -174,7 +191,7 @@ export class ProfilingPlugin { ...@@ -174,7 +191,7 @@ export class ProfilingPlugin {
request: entry.request, request: entry.request,
} }
}, },
parentSpan: () => spans.get(compiler)!, parentSpan: () => spans.get(compilation)!,
} }
) )
} }
...@@ -183,37 +200,37 @@ export class ProfilingPlugin { ...@@ -183,37 +200,37 @@ export class ProfilingPlugin {
'webpack-compilation-chunk-graph', 'webpack-compilation-chunk-graph',
compilation.hooks.beforeChunks, compilation.hooks.beforeChunks,
compilation.hooks.afterChunks, compilation.hooks.afterChunks,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
this.traceHookPair( this.traceHookPair(
'webpack-compilation-optimize', 'webpack-compilation-optimize',
compilation.hooks.optimize, compilation.hooks.optimize,
compilation.hooks.reviveModules, compilation.hooks.reviveModules,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
this.traceHookPair( this.traceHookPair(
'webpack-compilation-optimize-modules', 'webpack-compilation-optimize-modules',
compilation.hooks.optimizeModules, compilation.hooks.optimizeModules,
compilation.hooks.afterOptimizeModules, compilation.hooks.afterOptimizeModules,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
this.traceHookPair( this.traceHookPair(
'webpack-compilation-optimize-chunks', 'webpack-compilation-optimize-chunks',
compilation.hooks.optimizeChunks, compilation.hooks.optimizeChunks,
compilation.hooks.afterOptimizeChunks, compilation.hooks.afterOptimizeChunks,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
this.traceHookPair( this.traceHookPair(
'webpack-compilation-optimize-tree', 'webpack-compilation-optimize-tree',
compilation.hooks.optimizeTree, compilation.hooks.optimizeTree,
compilation.hooks.afterOptimizeTree, compilation.hooks.afterOptimizeTree,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
this.traceHookPair( this.traceHookPair(
'webpack-compilation-hash', 'webpack-compilation-hash',
compilation.hooks.beforeHash, compilation.hooks.beforeHash,
compilation.hooks.afterHash, compilation.hooks.afterHash,
{ parentSpan: () => spans.get(compiler)! } { parentSpan: () => spans.get(compilation) || spans.get(compiler)! }
) )
}) })
} }
......
...@@ -122,8 +122,10 @@ export class TerserPlugin { ...@@ -122,8 +122,10 @@ export class TerserPlugin {
cache, cache,
{ SourceMapSource, RawSource } { SourceMapSource, RawSource }
) { ) {
const compilerSpan = spans.get(compiler) const compilationSpan = spans.get(compilation) || spans.get(compiler)
const terserSpan = compilerSpan.traceChild('terser-webpack-plugin-optimize') const terserSpan = compilationSpan.traceChild(
'terser-webpack-plugin-optimize'
)
terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4) terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4)
terserSpan.setAttribute('compilationName', compilation.name) terserSpan.setAttribute('compilationName', compilation.name)
......
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