Unverified Commit 90ad2cbc authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Update profiling approach to cover webpack runs (#20900)

parent 8bdff57b
// Disable automatic instrumentation
process.env.OTEL_NO_PATCH_MODULES = '*'
const { NodeTracerProvider } = require('@opentelemetry/node')
const { BatchSpanProcessor } = require('@opentelemetry/tracing')
const { SimpleSpanProcessor } = require('@opentelemetry/tracing')
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
const tracerProvider = new NodeTracerProvider()
const tracerProvider = new NodeTracerProvider({
// All automatic instrumentation plugins have to be disabled as it affects worker_thread/child_process bootup performance
plugins: {
mongodb: { enabled: false, path: '@opentelemetry/plugin-mongodb' },
grpc: { enabled: false, path: '@opentelemetry/plugin-grpc' },
'@grpc/grpc-js': { enabled: false, path: '@opentelemetry/plugin-grpc-js' },
http: { enabled: false, path: '@opentelemetry/plugin-http' },
https: { enabled: false, path: '@opentelemetry/plugin-https' },
mysql: { enabled: false, path: '@opentelemetry/plugin-mysql' },
pg: { enabled: false, path: '@opentelemetry/plugin-pg' },
redis: { enabled: false, path: '@opentelemetry/plugin-redis' },
ioredis: { enabled: false, path: '@opentelemetry/plugin-ioredis' },
'pg-pool': { enabled: false, path: '@opentelemetry/plugin-pg-pool' },
express: { enabled: false, path: '@opentelemetry/plugin-express' },
'@hapi/hapi': {
enabled: false,
path: '@opentelemetry/hapi-instrumentation',
},
koa: { enabled: false, path: '@opentelemetry/koa-instrumentation' },
dns: { enabled: false, path: '@opentelemetry/plugin-dns' },
},
})
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(
new SimpleSpanProcessor(
new ZipkinExporter({
serviceName: 'next-js',
})
......
This diff is collapsed.
......@@ -28,6 +28,8 @@ import { BuildManifest } from '../next-server/server/get-page-files'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { UnwrapPromise } from '../lib/coalesced-function'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
import opentelemetryApi from '@opentelemetry/api'
import { tracer, traceAsyncFn } from './tracer'
const fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => {
......@@ -713,7 +715,8 @@ export async function isPageStatic(
serverBundle: string,
runtimeEnvConfig: any,
locales?: string[],
defaultLocale?: string
defaultLocale?: string,
spanContext?: any
): Promise<{
isStatic?: boolean
isAmpOnly?: boolean
......@@ -725,111 +728,131 @@ export async function isPageStatic(
prerenderFallback?: boolean | 'blocking'
isNextImageImported?: boolean
}> {
try {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
const mod = await require(serverBundle)
const Comp = await (mod.default || mod)
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
throw new Error('INVALID_DEFAULT_EXPORT')
}
const hasGetInitialProps = !!(Comp as any).getInitialProps
const hasStaticProps = !!(await mod.getStaticProps)
const hasStaticPaths = !!(await mod.getStaticPaths)
const hasServerProps = !!(await mod.getServerSideProps)
const hasLegacyServerProps = !!(await mod.unstable_getServerProps)
const hasLegacyStaticProps = !!(await mod.unstable_getStaticProps)
const hasLegacyStaticPaths = !!(await mod.unstable_getStaticPaths)
const hasLegacyStaticParams = !!(await mod.unstable_getStaticParams)
if (hasLegacyStaticParams) {
throw new Error(
`unstable_getStaticParams was replaced with getStaticPaths. Please update your code.`
)
}
if (hasLegacyStaticPaths) {
throw new Error(
`unstable_getStaticPaths was replaced with getStaticPaths. Please update your code.`
)
}
if (hasLegacyStaticProps) {
throw new Error(
`unstable_getStaticProps was replaced with getStaticProps. Please update your code.`
)
}
if (hasLegacyServerProps) {
throw new Error(
`unstable_getServerProps was replaced with getServerSideProps. Please update your code.`
)
}
// A page cannot be prerendered _and_ define a data requirement. That's
// contradictory!
if (hasGetInitialProps && hasStaticProps) {
throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT)
}
if (hasGetInitialProps && hasServerProps) {
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT)
}
if (hasStaticProps && hasServerProps) {
throw new Error(SERVER_PROPS_SSG_CONFLICT)
}
const pageIsDynamic = isDynamicRoute(page)
// A page cannot have static parameters if it is not a dynamic page.
if (hasStaticProps && hasStaticPaths && !pageIsDynamic) {
throw new Error(
`getStaticPaths can only be used with dynamic pages, not '${page}'.` +
`\nLearn more: https://nextjs.org/docs/routing/dynamic-routes`
)
}
if (hasStaticProps && pageIsDynamic && !hasStaticPaths) {
throw new Error(
`getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` +
`\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value`
return opentelemetryApi.context.with(
opentelemetryApi.propagation.extract(
opentelemetryApi.context.active(),
spanContext
),
() => {
return traceAsyncFn(
tracer.startSpan('is-page-static-utils'),
async () => {
try {
require('../next-server/lib/runtime-config').setConfig(
runtimeEnvConfig
)
const mod = await require(serverBundle)
const Comp = await (mod.default || mod)
if (
!Comp ||
!isValidElementType(Comp) ||
typeof Comp === 'string'
) {
throw new Error('INVALID_DEFAULT_EXPORT')
}
const hasGetInitialProps = !!(Comp as any).getInitialProps
const hasStaticProps = !!(await mod.getStaticProps)
const hasStaticPaths = !!(await mod.getStaticPaths)
const hasServerProps = !!(await mod.getServerSideProps)
const hasLegacyServerProps = !!(await mod.unstable_getServerProps)
const hasLegacyStaticProps = !!(await mod.unstable_getStaticProps)
const hasLegacyStaticPaths = !!(await mod.unstable_getStaticPaths)
const hasLegacyStaticParams = !!(await mod.unstable_getStaticParams)
if (hasLegacyStaticParams) {
throw new Error(
`unstable_getStaticParams was replaced with getStaticPaths. Please update your code.`
)
}
if (hasLegacyStaticPaths) {
throw new Error(
`unstable_getStaticPaths was replaced with getStaticPaths. Please update your code.`
)
}
if (hasLegacyStaticProps) {
throw new Error(
`unstable_getStaticProps was replaced with getStaticProps. Please update your code.`
)
}
if (hasLegacyServerProps) {
throw new Error(
`unstable_getServerProps was replaced with getServerSideProps. Please update your code.`
)
}
// A page cannot be prerendered _and_ define a data requirement. That's
// contradictory!
if (hasGetInitialProps && hasStaticProps) {
throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT)
}
if (hasGetInitialProps && hasServerProps) {
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT)
}
if (hasStaticProps && hasServerProps) {
throw new Error(SERVER_PROPS_SSG_CONFLICT)
}
const pageIsDynamic = isDynamicRoute(page)
// A page cannot have static parameters if it is not a dynamic page.
if (hasStaticProps && hasStaticPaths && !pageIsDynamic) {
throw new Error(
`getStaticPaths can only be used with dynamic pages, not '${page}'.` +
`\nLearn more: https://nextjs.org/docs/routing/dynamic-routes`
)
}
if (hasStaticProps && pageIsDynamic && !hasStaticPaths) {
throw new Error(
`getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` +
`\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value`
)
}
let prerenderRoutes: Array<string> | undefined
let encodedPrerenderRoutes: Array<string> | undefined
let prerenderFallback: boolean | 'blocking' | undefined
if (hasStaticProps && hasStaticPaths) {
;({
paths: prerenderRoutes,
fallback: prerenderFallback,
encodedPaths: encodedPrerenderRoutes,
} = await buildStaticPaths(
page,
mod.getStaticPaths,
locales,
defaultLocale
))
}
const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED
const config = mod.config || {}
return {
isStatic:
!hasStaticProps && !hasGetInitialProps && !hasServerProps,
isHybridAmp: config.amp === 'hybrid',
isAmpOnly: config.amp === true,
prerenderRoutes,
prerenderFallback,
encodedPrerenderRoutes,
hasStaticProps,
hasServerProps,
isNextImageImported,
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {}
throw err
}
}
)
}
let prerenderRoutes: Array<string> | undefined
let encodedPrerenderRoutes: Array<string> | undefined
let prerenderFallback: boolean | 'blocking' | undefined
if (hasStaticProps && hasStaticPaths) {
;({
paths: prerenderRoutes,
fallback: prerenderFallback,
encodedPaths: encodedPrerenderRoutes,
} = await buildStaticPaths(
page,
mod.getStaticPaths,
locales,
defaultLocale
))
}
const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED
const config = mod.config || {}
return {
isStatic: !hasStaticProps && !hasGetInitialProps && !hasServerProps,
isHybridAmp: config.amp === 'hybrid',
isAmpOnly: config.amp === true,
prerenderRoutes,
prerenderFallback,
encodedPrerenderRoutes,
hasStaticProps,
hasServerProps,
isNextImageImported,
}
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {}
throw err
}
)
}
export async function hasCustomGetInitialProps(
......
......@@ -193,7 +193,6 @@ export default async function getBaseWebpackConfig(
dev = false,
isServer = false,
pagesDir,
tracer,
target = 'server',
reactProductionProfiling = false,
entrypoints,
......@@ -205,7 +204,6 @@ export default async function getBaseWebpackConfig(
isServer?: boolean
pagesDir: string
target?: string
tracer?: any
reactProductionProfiling?: boolean
entrypoints: WebpackEntrypoints
rewrites: Rewrite[]
......@@ -1078,10 +1076,7 @@ export default async function getBaseWebpackConfig(
buildId,
rewrites,
}),
tracer &&
new ProfilingPlugin({
tracer,
}),
new ProfilingPlugin(),
config.experimental.optimizeFonts &&
!dev &&
isServer &&
......
......@@ -22,147 +22,150 @@ export default function makeLoader(callback) {
}
async function loader(source, inputSourceMap, overrides) {
const span = tracer.startSpan('babel-loader')
return traceAsyncFn(span, async () => {
const filename = this.resourcePath
span.setAttribute('filename', filename)
let loaderOptions = loaderUtils.getOptions(this) || {}
let customOptions
if (overrides && overrides.customOptions) {
const result = await traceAsyncFn(
tracer.startSpan('loader-overrides-customoptions'),
async () =>
await overrides.customOptions.call(this, loaderOptions, {
source,
map: inputSourceMap,
})
)
customOptions = result.custom
loaderOptions = result.loader
}
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that
// users may safely use either one alongside our default use of
// 'this.sourceMap' below without getting error about conflicting aliases.
if (
Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMap') &&
!Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMaps')
) {
loaderOptions = Object.assign({}, loaderOptions, {
sourceMaps: loaderOptions.sourceMap,
})
delete loaderOptions.sourceMap
}
const programmaticOptions = Object.assign({}, loaderOptions, {
filename,
inputSourceMap: inputSourceMap || undefined,
// Set the default sourcemap behavior based on Webpack's mapping flag,
// but allow users to override if they want.
sourceMaps:
loaderOptions.sourceMaps === undefined
? this.sourceMap
: loaderOptions.sourceMaps,
// Ensure that Webpack will get a full absolute path in the sourcemap
// so that it can properly map the module back to its internal cached
// modules.
sourceFileName: filename,
caller: {
name: 'babel-loader',
// Provide plugins with insight into webpack target.
// https://github.com/babel/babel-loader/issues/787
target: this.target,
// Webpack >= 2 supports ESM and dynamic import.
supportsStaticESM: true,
supportsDynamicImport: true,
// Webpack 5 supports TLA behind a flag. We enable it by default
// for Babel, and then webpack will throw an error if the experimental
// flag isn't enabled.
supportsTopLevelAwait: true,
...loaderOptions.caller,
},
})
// Remove loader related options
delete programmaticOptions.cacheDirectory
delete programmaticOptions.cacheIdentifier
const config = traceFn(
tracer.startSpan('babel-load-partial-config-async'),
() => {
return babel.loadPartialConfig(programmaticOptions)
}
)
if (config) {
let options = config.options
if (overrides && overrides.config) {
options = await traceAsyncFn(
tracer.startSpan('loader-overrides-config'),
// Provided by profiling-plugin.ts
return tracer.withSpan(this.currentTraceSpan, () => {
const span = tracer.startSpan('babel-loader')
return traceAsyncFn(span, async () => {
const filename = this.resourcePath
span.setAttribute('filename', filename)
let loaderOptions = loaderUtils.getOptions(this) || {}
let customOptions
if (overrides && overrides.customOptions) {
const result = await traceAsyncFn(
tracer.startSpan('loader-overrides-customoptions'),
async () =>
await overrides.config.call(this, config, {
await overrides.customOptions.call(this, loaderOptions, {
source,
map: inputSourceMap,
customOptions,
})
)
customOptions = result.custom
loaderOptions = result.loader
}
if (options.sourceMaps === 'inline') {
// Babel has this weird behavior where if you set "inline", we
// inline the sourcemap, and set 'result.map = null'. This results
// in bad behavior from Babel since the maps get put into the code,
// which Webpack does not expect, and because the map we return to
// Webpack is null, which is also bad. To avoid that, we override the
// behavior here so "inline" just behaves like 'true'.
options.sourceMaps = true
}
const { cacheDirectory, cacheIdentifier } = loaderOptions
let result
if (cacheDirectory) {
result = await cache({
source,
options,
cacheDirectory,
cacheIdentifier,
cacheCompression: false,
// Standardize on 'sourceMaps' as the key passed through to Webpack, so that
// users may safely use either one alongside our default use of
// 'this.sourceMap' below without getting error about conflicting aliases.
if (
Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMap') &&
!Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMaps')
) {
loaderOptions = Object.assign({}, loaderOptions, {
sourceMaps: loaderOptions.sourceMap,
})
} else {
result = await traceAsyncFn(
tracer.startSpan('transform', {
attributes: {
filename,
cache: 'DISABLED',
},
}),
async () => {
return transform(source, options)
}
)
delete loaderOptions.sourceMap
}
// TODO: Babel should really provide the full list of config files that
// were used so that this can also handle files loaded with 'extends'.
if (typeof config.babelrc === 'string') {
this.addDependency(config.babelrc)
}
if (result) {
const { code, map } = result
const programmaticOptions = Object.assign({}, loaderOptions, {
filename,
inputSourceMap: inputSourceMap || undefined,
// Set the default sourcemap behavior based on Webpack's mapping flag,
// but allow users to override if they want.
sourceMaps:
loaderOptions.sourceMaps === undefined
? this.sourceMap
: loaderOptions.sourceMaps,
// Ensure that Webpack will get a full absolute path in the sourcemap
// so that it can properly map the module back to its internal cached
// modules.
sourceFileName: filename,
caller: {
name: 'babel-loader',
// Provide plugins with insight into webpack target.
// https://github.com/babel/babel-loader/issues/787
target: this.target,
// Webpack >= 2 supports ESM and dynamic import.
supportsStaticESM: true,
supportsDynamicImport: true,
// Webpack 5 supports TLA behind a flag. We enable it by default
// for Babel, and then webpack will throw an error if the experimental
// flag isn't enabled.
supportsTopLevelAwait: true,
...loaderOptions.caller,
},
})
// Remove loader related options
delete programmaticOptions.cacheDirectory
delete programmaticOptions.cacheIdentifier
const config = traceFn(
tracer.startSpan('babel-load-partial-config-async'),
() => {
return babel.loadPartialConfig(programmaticOptions)
}
)
return [code, map]
if (config) {
let options = config.options
if (overrides && overrides.config) {
options = await traceAsyncFn(
tracer.startSpan('loader-overrides-config'),
async () =>
await overrides.config.call(this, config, {
source,
map: inputSourceMap,
customOptions,
})
)
}
if (options.sourceMaps === 'inline') {
// Babel has this weird behavior where if you set "inline", we
// inline the sourcemap, and set 'result.map = null'. This results
// in bad behavior from Babel since the maps get put into the code,
// which Webpack does not expect, and because the map we return to
// Webpack is null, which is also bad. To avoid that, we override the
// behavior here so "inline" just behaves like 'true'.
options.sourceMaps = true
}
const { cacheDirectory, cacheIdentifier } = loaderOptions
let result
if (cacheDirectory) {
result = await cache({
source,
options,
cacheDirectory,
cacheIdentifier,
cacheCompression: false,
})
} else {
result = await traceAsyncFn(
tracer.startSpan('transform', {
attributes: {
filename,
cache: 'DISABLED',
},
}),
async () => {
return transform(source, options)
}
)
}
// TODO: Babel should really provide the full list of config files that
// were used so that this can also handle files loaded with 'extends'.
if (typeof config.babelrc === 'string') {
this.addDependency(config.babelrc)
}
if (result) {
const { code, map } = result
return [code, map]
}
}
}
// If the file was ignored, pass through the original content.
return [source, inputSourceMap]
// If the file was ignored, pass through the original content.
return [source, inputSourceMap]
})
})
}
import { loader } from 'webpack'
import loaderUtils from 'loader-utils'
import { tracer, traceFn } from '../../tracer'
......@@ -7,19 +6,21 @@ export type ClientPagesLoaderOptions = {
page: string
}
const nextClientPagesLoader: loader.Loader = function () {
const span = tracer.startSpan('next-client-pages-loader')
return traceFn(span, () => {
const { absolutePagePath, page } = loaderUtils.getOptions(
this
) as ClientPagesLoaderOptions
// this parameter: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters
function nextClientPagesLoader(this: any) {
return tracer.withSpan(this.currentTraceSpan, () => {
const span = tracer.startSpan('next-client-pages-loader')
return traceFn(span, () => {
const { absolutePagePath, page } = loaderUtils.getOptions(
this
) as ClientPagesLoaderOptions
span.setAttribute('absolutePagePath', absolutePagePath)
span.setAttribute('absolutePagePath', absolutePagePath)
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPage = JSON.stringify(page)
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPage = JSON.stringify(page)
return `
return `
(window.__NEXT_P = window.__NEXT_P || []).push([
${stringifiedPage},
function () {
......@@ -27,6 +28,7 @@ const nextClientPagesLoader: loader.Loader = function () {
}
]);
`
})
})
}
......
......@@ -4,6 +4,7 @@ import postcss, { Parser } from 'postcss'
import webpack from 'webpack'
import sources from 'webpack-sources'
import { tracer, traceAsyncFn } from '../../tracer'
import { spans } from './profiling-plugin'
// @ts-ignore: TODO: remove ignore when webpack 5 is stable
const { RawSource, SourceMapSource } = webpack.sources || sources
......@@ -71,87 +72,91 @@ export class CssMinimizerPlugin {
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
async (assets: any) => {
return tracer.withSpan(spans.get(compiler), () => {
const span = tracer.startSpan('css-minimizer-plugin', {
attributes: {
webpackVersion: 5,
},
})
return traceAsyncFn(span, async () => {
const files = Object.keys(assets)
await Promise.all(
files
.filter((file) => CSS_REGEX.test(file))
.map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', {
attributes: {
file,
},
})
return traceAsyncFn(span, async () => {
const asset = assets[file]
const etag = cache.getLazyHashedEtag(asset)
const cachedResult = await cache.getPromise(file, etag)
assetSpan.setAttribute(
'cache',
cachedResult ? 'HIT' : 'MISS'
)
if (cachedResult) {
assets[file] = cachedResult
return
}
const result = await this.optimizeAsset(file, asset)
await cache.storePromise(file, etag, result)
assets[file] = result
})
})
)
})
})
}
)
return
}
compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => {
return tracer.withSpan(spans.get(compiler), () => {
const span = tracer.startSpan('css-minimizer-plugin', {
attributes: {
webpackVersion: 5,
webpackVersion: 4,
compilationName: compilation.name,
},
})
return traceAsyncFn(span, async () => {
const files = Object.keys(assets)
await Promise.all(
files
.filter((file) => CSS_REGEX.test(file))
const res = await Promise.all(
chunks
.reduce(
(acc, chunk) => acc.concat(chunk.files || []),
[] as string[]
)
.filter((entry) => CSS_REGEX.test(entry))
.map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', {
attributes: {
file,
},
})
return traceAsyncFn(span, async () => {
const asset = assets[file]
const etag = cache.getLazyHashedEtag(asset)
const cachedResult = await cache.getPromise(file, etag)
assetSpan.setAttribute(
'cache',
cachedResult ? 'HIT' : 'MISS'
return traceAsyncFn(assetSpan, async () => {
const asset = compilation.assets[file]
// Makes trace attributes the same as webpack 5
// When using webpack 4 the result is not cached
assetSpan.setAttribute('cache', 'MISS')
compilation.assets[file] = await this.optimizeAsset(
file,
asset
)
if (cachedResult) {
assets[file] = cachedResult
return
}
const result = await this.optimizeAsset(file, asset)
await cache.storePromise(file, etag, result)
assets[file] = result
})
})
)
return res
})
}
)
return
}
compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => {
const span = tracer.startSpan('css-minimizer-plugin', {
attributes: {
webpackVersion: 4,
compilationName: compilation.name,
},
})
return traceAsyncFn(span, async () => {
const res = await Promise.all(
chunks
.reduce(
(acc, chunk) => acc.concat(chunk.files || []),
[] as string[]
)
.filter((entry) => CSS_REGEX.test(entry))
.map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', {
attributes: {
file,
},
})
return traceAsyncFn(assetSpan, async () => {
const asset = compilation.assets[file]
// Makes trace attributes the same as webpack 5
// When using webpack 4 the result is not cached
assetSpan.setAttribute('cache', 'MISS')
compilation.assets[file] = await this.optimizeAsset(
file,
asset
)
})
})
)
return res
})
}
)
......
// Copy of https://github.com/webpack/webpack/blob/master/lib/debug/ProfilingPlugin.js
// License:
/*
Copyright JS Foundation and other contributors
import { tracer } from '../../tracer'
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Includes fix https://github.com/webpack/webpack/pull/9566
// Includes support for custom tracer that can capture across multiple builds
const pluginName = 'ProfilingPlugin'
export class ProfilingPlugin {
tracer: any
/**
* @param {ProfilingPluginOptions=} opts options object
*/
constructor(opts: { tracer: any }) {
this.tracer = opts.tracer
}
export const spans = new WeakMap()
export class ProfilingPlugin {
apply(compiler: any) {
const tracer = this.tracer
// Compiler Hooks
Object.keys(compiler.hooks).forEach((hookName) => {
compiler.hooks[hookName].intercept(
makeInterceptorFor('Compiler', tracer)(hookName)
)
})
// Only enabled when instrumentation is loaded
if (!tracer.getCurrentSpan()) {
return
}
Object.keys(compiler.resolverFactory.hooks).forEach((hookName) => {
compiler.resolverFactory.hooks[hookName].intercept(
makeInterceptorFor('Resolver', tracer)(hookName)
)
compiler.hooks.compile.tap(pluginName, () => {
const span = tracer.startSpan('webpack-compile', {
attributes: { name: compiler.name },
})
spans.set(compiler, span)
})
compiler.hooks.compilation.tap(
pluginName,
(
compilation: any,
{ normalModuleFactory, contextModuleFactory }: any
) => {
interceptAllHooksFor(compilation, tracer, 'Compilation')
interceptAllHooksFor(
normalModuleFactory,
tracer,
'Normal Module Factory'
)
interceptAllHooksFor(
contextModuleFactory,
tracer,
'Context Module Factory'
)
interceptAllParserHooks(normalModuleFactory, tracer)
interceptTemplateInstancesFrom(compilation, tracer)
}
)
}
}
const interceptTemplateInstancesFrom = (compilation: any, tracer: any) => {
const {
mainTemplate,
chunkTemplate,
hotUpdateChunkTemplate,
moduleTemplates,
} = compilation
const { javascript, webassembly } = moduleTemplates
;[
{
instance: mainTemplate,
name: 'MainTemplate',
},
{
instance: chunkTemplate,
name: 'ChunkTemplate',
},
{
instance: hotUpdateChunkTemplate,
name: 'HotUpdateChunkTemplate',
},
{
instance: javascript,
name: 'JavaScriptModuleTemplate',
},
{
instance: webassembly,
name: 'WebAssemblyModuleTemplate',
},
].forEach((templateObject) => {
Object.keys(templateObject.instance.hooks).forEach((hookName) => {
templateObject.instance.hooks[hookName].intercept(
makeInterceptorFor(templateObject.name, tracer)(hookName)
)
compiler.hooks.done.tap(pluginName, () => {
spans.get(compiler).end()
})
})
}
compiler.hooks.compilation.tap(pluginName, (compilation: any) => {
compilation.hooks.buildModule.tap(pluginName, (module: any) => {
tracer.withSpan(spans.get(compiler), () => {
const span = tracer.startSpan('build-module')
span.setAttribute('name', module.userRequest)
spans.set(module, span)
})
})
const interceptAllHooksFor = (instance: any, tracer: any, logLabel: any) => {
if (Reflect.has(instance, 'hooks')) {
Object.keys(instance.hooks).forEach((hookName) => {
instance.hooks[hookName].intercept(
makeInterceptorFor(logLabel, tracer)(hookName)
compilation.hooks.normalModuleLoader.tap(
pluginName,
(loaderContext: any, module: any) => {
const parentSpan = spans.get(module)
loaderContext.currentTraceSpan = parentSpan
}
)
})
}
}
const interceptAllParserHooks = (moduleFactory: any, tracer: any) => {
const moduleTypes = [
'javascript/auto',
'javascript/dynamic',
'javascript/esm',
'json',
'webassembly/experimental',
]
moduleTypes.forEach((moduleType) => {
moduleFactory.hooks.parser
.for(moduleType)
.tap('ProfilingPlugin', (parser: any) => {
interceptAllHooksFor(parser, tracer, 'Parser')
compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
spans.get(module).end()
})
})
}
const makeInterceptorFor = (_instance: any, tracer: any) => (
hookName: any
) => ({
register: ({ name, type, context, fn }: any) => {
const newFn = makeNewProfiledTapFn(hookName, tracer, {
name,
type,
fn,
})
return {
name,
type,
context,
fn: newFn,
}
},
})
// TODO improve typing
/** @typedef {(...args: TODO[]) => void | Promise<TODO>} PluginFunction */
/**
* @param {string} hookName Name of the hook to profile.
* @param {Trace} tracer The trace object.
* @param {object} options Options for the profiled fn.
* @param {string} options.name Plugin name
* @param {string} options.type Plugin type (sync | async | promise)
* @param {PluginFunction} options.fn Plugin function
* @returns {PluginFunction} Chainable hooked function.
*/
const makeNewProfiledTapFn = (
_hookName: any,
tracer: any,
{ name, type, fn }: any
) => {
const defaultCategory = ['blink.user_timing']
switch (type) {
case 'promise':
return (...args: any) => {
const id = ++tracer.counter
tracer.trace.begin({
name,
id,
cat: defaultCategory,
})
const promise = /** @type {Promise<*>} */ fn(...args)
return promise.then((r: any) => {
tracer.trace.end({
name,
id,
cat: defaultCategory,
})
return r
})
}
case 'async':
return (...args: any) => {
const id = ++tracer.counter
tracer.trace.begin({
name,
id,
cat: defaultCategory,
})
const callback = args.pop()
/* eslint-disable */
fn(...args, (...r: any) => {
tracer.trace.end({
name,
id,
cat: defaultCategory,
})
callback(...r)
})
/* eslint-enable */
}
case 'sync':
return (...args: any) => {
const id = ++tracer.counter
// Do not instrument ourself due to the CPU
// profile needing to be the last event in the trace.
if (name === pluginName) {
return fn(...args)
}
tracer.trace.begin({
name,
id,
cat: defaultCategory,
})
let r
try {
r = fn(...args)
} catch (error) {
tracer.trace.end({
name,
id,
cat: defaultCategory,
})
throw error
}
tracer.trace.end({
name,
id,
cat: defaultCategory,
})
return r
}
default:
break
}
}
This diff is collapsed.
This diff is collapsed.
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