forked from vercel/next.js
/
profiling-plugin.ts
164 lines (147 loc) · 4.66 KB
/
profiling-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { trace, Span } from '../../../telemetry/trace'
const pluginName = 'ProfilingPlugin'
export const spans = new WeakMap<any, Span>()
function getNormalModuleLoaderHook(compilation: any) {
if (isWebpack5) {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
return webpack.NormalModule.getCompilationHooks(compilation).loader
}
return compilation.hooks.normalModuleLoader
}
export class ProfilingPlugin {
compiler: any
runWebpackSpan: Span | undefined
constructor({ runWebpackSpan }: { runWebpackSpan: Span | undefined }) {
this.runWebpackSpan = runWebpackSpan
}
apply(compiler: any) {
this.traceTopLevelHooks(compiler)
this.traceCompilationHooks(compiler)
this.compiler = compiler
}
traceHookPair(
spanName: string,
startHook: any,
stopHook: any,
{
parentSpan,
attrs,
onSetSpan,
}: {
parentSpan?: () => Span | undefined
attrs?: any
onSetSpan?: (span: Span) => void
} = {}
) {
let span: Span | undefined
startHook.tap(pluginName, () => {
span = trace(spanName, parentSpan?.()?.id, attrs ? attrs() : attrs)
onSetSpan?.(span)
})
stopHook.tap(pluginName, () => {
// `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
}
span.stop()
})
}
traceTopLevelHooks(compiler: any) {
this.traceHookPair(
'webpack-compilation',
isWebpack5 ? compiler.hooks.beforeCompile : compiler.hooks.compile,
isWebpack5 ? compiler.hooks.afterCompile : compiler.hooks.done,
{
parentSpan: () => this.runWebpackSpan,
attrs: () => ({ name: compiler.name }),
onSetSpan: (span) => spans.set(compiler, span),
}
)
if (compiler.options.mode === 'development') {
this.traceHookPair(
'webpack-invalidated',
compiler.hooks.invalid,
compiler.hooks.done,
{ attrs: () => ({ name: compiler.name }) }
)
}
}
traceCompilationHooks(compiler: any) {
this.traceHookPair(
'webpack-emit',
compiler.hooks.emit,
compiler.hooks.afterEmit,
{ parentSpan: () => this.runWebpackSpan }
)
compiler.hooks.compilation.tap(pluginName, (compilation: any) => {
compilation.hooks.buildModule.tap(pluginName, (module: any) => {
const compilerSpan = spans.get(compiler)
if (!compilerSpan) {
return
}
const moduleType = (() => {
if (!module.userRequest) {
return ''
}
return module.userRequest.split('.').pop()
})()
const issuerModule = compilation?.moduleGraph?.getIssuer(module)
const span = trace(
`build-module${moduleType ? `-${moduleType}` : ''}`,
issuerModule ? spans.get(issuerModule)?.id : compilerSpan.id
)
span.setAttribute('name', module.userRequest)
spans.set(module, span)
})
getNormalModuleLoaderHook(compilation).tap(
pluginName,
(loaderContext: any, module: any) => {
const moduleSpan = spans.get(module)
loaderContext.currentTraceSpan = moduleSpan
}
)
compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
spans.get(module)?.stop()
})
this.traceHookPair(
'webpack-compilation-chunk-graph',
compilation.hooks.beforeChunks,
compilation.hooks.afterChunks,
{ parentSpan: () => this.runWebpackSpan }
)
this.traceHookPair(
'webpack-compilation-optimize',
compilation.hooks.optimize,
compilation.hooks.reviveModules,
{ parentSpan: () => this.runWebpackSpan }
)
this.traceHookPair(
'webpack-compilation-optimize-modules',
compilation.hooks.optimizeModules,
compilation.hooks.afterOptimizeModules,
{ parentSpan: () => this.runWebpackSpan }
)
this.traceHookPair(
'webpack-compilation-optimize-chunks',
compilation.hooks.optimizeChunks,
compilation.hooks.afterOptimizeChunks,
{ parentSpan: () => this.runWebpackSpan }
)
this.traceHookPair(
'webpack-compilation-optimize-tree',
compilation.hooks.optimizeTree,
compilation.hooks.afterOptimizeTree,
{ parentSpan: () => this.runWebpackSpan }
)
this.traceHookPair(
'webpack-compilation-hash',
compilation.hooks.beforeHash,
compilation.hooks.afterHash,
{ parentSpan: () => this.runWebpackSpan }
)
})
}
}