next
Version:
The React Framework
7 lines • 3.71 kB
JavaScript
;exports.__esModule=true;exports.ProfilingPlugin=exports.spans=void 0;var _tracer=require("../../tracer");var _webpack=require("next/dist/compiled/webpack/webpack");var _api=require("@opentelemetry/api");const pluginName='ProfilingPlugin';const spans=new WeakMap();exports.spans=spans;function getNormalModuleLoaderHook(compilation){if(_webpack.isWebpack5){// @ts-ignore TODO: Remove ignore when webpack 5 is stable
return _webpack.webpack.NormalModule.getCompilationHooks(compilation).loader;}return compilation.hooks.normalModuleLoader;}function tracingIsEnabled(){const tracerProvider=_api.trace.getTracerProvider();if(tracerProvider instanceof _api.ProxyTracerProvider){const proxyDelegate=tracerProvider.getDelegate();return!(proxyDelegate instanceof _api.NoopTracerProvider);}return false;}class ProfilingPlugin{constructor(){this.compiler=void 0;}apply(compiler){// Only enable plugin when instrumentation is loaded
if(!tracingIsEnabled()){return;}this.traceTopLevelHooks(compiler);this.traceCompilationHooks(compiler);this.compiler=compiler;}traceHookPair(spanName,startHook,stopHook,attrs,onSetSpan){let span;startHook.tap(pluginName,()=>{span=(0,_tracer.stackPush)(this.compiler,spanName,attrs);onSetSpan==null?void 0: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;}(0,_tracer.stackPop)(this.compiler,span,spanName);});}traceLoopedHook(spanName,startHook,stopHook){let span;startHook.tap(pluginName,()=>{if(!span){span=(0,_tracer.stackPush)(this.compiler,spanName);}});stopHook.tap(pluginName,()=>{(0,_tracer.stackPop)(this.compiler,span,spanName);});}traceTopLevelHooks(compiler){this.traceHookPair('webpack-compile',compiler.hooks.compile,compiler.hooks.done,()=>{return{attributes:{name:compiler.name}};},span=>spans.set(compiler,span));this.traceHookPair('webpack-prepare-env',compiler.hooks.environment,compiler.hooks.afterEnvironment);if(compiler.options.mode==='development'){this.traceHookPair('webpack-invalidated',compiler.hooks.invalid,compiler.hooks.done);}}traceCompilationHooks(compiler){if(_webpack.isWebpack5){this.traceHookPair('webpack-compilation',compiler.hooks.beforeCompile,compiler.hooks.afterCompile);}compiler.hooks.compilation.tap(pluginName,compilation=>{compilation.hooks.buildModule.tap(pluginName,module=>{const compilerSpan=spans.get(compiler);if(!compilerSpan){return;}_tracer.tracer.withSpan(compilerSpan,()=>{const span=_tracer.tracer.startSpan('build-module');span.setAttribute('name',module.userRequest);spans.set(module,span);});});getNormalModuleLoaderHook(compilation).tap(pluginName,(loaderContext,module)=>{const parentSpan=spans.get(module);loaderContext.currentTraceSpan=parentSpan;});compilation.hooks.succeedModule.tap(pluginName,module=>{spans.get(module).end();});this.traceHookPair('webpack-compilation-chunk-graph',compilation.hooks.beforeChunks,compilation.hooks.afterChunks);this.traceHookPair('webpack-compilation-optimize',compilation.hooks.optimize,compilation.hooks.reviveModules);this.traceLoopedHook('webpack-compilation-optimize-modules',compilation.hooks.optimizeModules,compilation.hooks.afterOptimizeModules);this.traceLoopedHook('webpack-compilation-optimize-chunks',compilation.hooks.optimizeChunks,compilation.hooks.afterOptimizeChunks);this.traceHookPair('webpack-compilation-optimize-tree',compilation.hooks.optimizeTree,compilation.hooks.afterOptimizeTree);this.traceHookPair('webpack-compilation-hash',compilation.hooks.beforeHash,compilation.hooks.afterHash);});}}exports.ProfilingPlugin=ProfilingPlugin;
//# sourceMappingURL=profiling-plugin.js.map