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 { NodeTracerProvider } = require('@opentelemetry/node')
const { BatchSpanProcessor } = require('@opentelemetry/tracing') const { SimpleSpanProcessor } = require('@opentelemetry/tracing')
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin') 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( tracerProvider.addSpanProcessor(
new BatchSpanProcessor( new SimpleSpanProcessor(
new ZipkinExporter({ new ZipkinExporter({
serviceName: 'next-js', serviceName: 'next-js',
}) })
......
This diff is collapsed.
...@@ -28,6 +28,8 @@ import { BuildManifest } from '../next-server/server/get-page-files' ...@@ -28,6 +28,8 @@ import { BuildManifest } from '../next-server/server/get-page-files'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash' import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { UnwrapPromise } from '../lib/coalesced-function' import { UnwrapPromise } from '../lib/coalesced-function'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path' 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 fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => { const fsStatGzip = (file: string) => {
...@@ -713,7 +715,8 @@ export async function isPageStatic( ...@@ -713,7 +715,8 @@ export async function isPageStatic(
serverBundle: string, serverBundle: string,
runtimeEnvConfig: any, runtimeEnvConfig: any,
locales?: string[], locales?: string[],
defaultLocale?: string defaultLocale?: string,
spanContext?: any
): Promise<{ ): Promise<{
isStatic?: boolean isStatic?: boolean
isAmpOnly?: boolean isAmpOnly?: boolean
...@@ -725,111 +728,131 @@ export async function isPageStatic( ...@@ -725,111 +728,131 @@ export async function isPageStatic(
prerenderFallback?: boolean | 'blocking' prerenderFallback?: boolean | 'blocking'
isNextImageImported?: boolean isNextImageImported?: boolean
}> { }> {
try { return opentelemetryApi.context.with(
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig) opentelemetryApi.propagation.extract(
const mod = await require(serverBundle) opentelemetryApi.context.active(),
const Comp = await (mod.default || mod) spanContext
),
if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') { () => {
throw new Error('INVALID_DEFAULT_EXPORT') return traceAsyncFn(
} tracer.startSpan('is-page-static-utils'),
async () => {
const hasGetInitialProps = !!(Comp as any).getInitialProps try {
const hasStaticProps = !!(await mod.getStaticProps) require('../next-server/lib/runtime-config').setConfig(
const hasStaticPaths = !!(await mod.getStaticPaths) runtimeEnvConfig
const hasServerProps = !!(await mod.getServerSideProps) )
const hasLegacyServerProps = !!(await mod.unstable_getServerProps) const mod = await require(serverBundle)
const hasLegacyStaticProps = !!(await mod.unstable_getStaticProps) const Comp = await (mod.default || mod)
const hasLegacyStaticPaths = !!(await mod.unstable_getStaticPaths)
const hasLegacyStaticParams = !!(await mod.unstable_getStaticParams) if (
!Comp ||
if (hasLegacyStaticParams) { !isValidElementType(Comp) ||
throw new Error( typeof Comp === 'string'
`unstable_getStaticParams was replaced with getStaticPaths. Please update your code.` ) {
) throw new Error('INVALID_DEFAULT_EXPORT')
} }
if (hasLegacyStaticPaths) { const hasGetInitialProps = !!(Comp as any).getInitialProps
throw new Error( const hasStaticProps = !!(await mod.getStaticProps)
`unstable_getStaticPaths was replaced with getStaticPaths. Please update your code.` const hasStaticPaths = !!(await mod.getStaticPaths)
) const hasServerProps = !!(await mod.getServerSideProps)
} const hasLegacyServerProps = !!(await mod.unstable_getServerProps)
const hasLegacyStaticProps = !!(await mod.unstable_getStaticProps)
if (hasLegacyStaticProps) { const hasLegacyStaticPaths = !!(await mod.unstable_getStaticPaths)
throw new Error( const hasLegacyStaticParams = !!(await mod.unstable_getStaticParams)
`unstable_getStaticProps was replaced with getStaticProps. Please update your code.`
) if (hasLegacyStaticParams) {
} throw new Error(
`unstable_getStaticParams was replaced with getStaticPaths. Please update your code.`
if (hasLegacyServerProps) { )
throw new Error( }
`unstable_getServerProps was replaced with getServerSideProps. Please update your code.`
) if (hasLegacyStaticPaths) {
} throw new Error(
`unstable_getStaticPaths was replaced with getStaticPaths. 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 (hasLegacyStaticProps) {
} throw new Error(
`unstable_getStaticProps was replaced with getStaticProps. Please update your code.`
if (hasGetInitialProps && hasServerProps) { )
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT) }
}
if (hasLegacyServerProps) {
if (hasStaticProps && hasServerProps) { throw new Error(
throw new Error(SERVER_PROPS_SSG_CONFLICT) `unstable_getServerProps was replaced with getServerSideProps. Please update your code.`
} )
}
const pageIsDynamic = isDynamicRoute(page)
// A page cannot have static parameters if it is not a dynamic page. // A page cannot be prerendered _and_ define a data requirement. That's
if (hasStaticProps && hasStaticPaths && !pageIsDynamic) { // contradictory!
throw new Error( if (hasGetInitialProps && hasStaticProps) {
`getStaticPaths can only be used with dynamic pages, not '${page}'.` + throw new Error(SSG_GET_INITIAL_PROPS_CONFLICT)
`\nLearn more: https://nextjs.org/docs/routing/dynamic-routes` }
)
} if (hasGetInitialProps && hasServerProps) {
throw new Error(SERVER_PROPS_GET_INIT_PROPS_CONFLICT)
if (hasStaticProps && pageIsDynamic && !hasStaticPaths) { }
throw new Error(
`getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` + if (hasStaticProps && hasServerProps) {
`\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value` 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( export async function hasCustomGetInitialProps(
......
...@@ -193,7 +193,6 @@ export default async function getBaseWebpackConfig( ...@@ -193,7 +193,6 @@ export default async function getBaseWebpackConfig(
dev = false, dev = false,
isServer = false, isServer = false,
pagesDir, pagesDir,
tracer,
target = 'server', target = 'server',
reactProductionProfiling = false, reactProductionProfiling = false,
entrypoints, entrypoints,
...@@ -205,7 +204,6 @@ export default async function getBaseWebpackConfig( ...@@ -205,7 +204,6 @@ export default async function getBaseWebpackConfig(
isServer?: boolean isServer?: boolean
pagesDir: string pagesDir: string
target?: string target?: string
tracer?: any
reactProductionProfiling?: boolean reactProductionProfiling?: boolean
entrypoints: WebpackEntrypoints entrypoints: WebpackEntrypoints
rewrites: Rewrite[] rewrites: Rewrite[]
...@@ -1078,10 +1076,7 @@ export default async function getBaseWebpackConfig( ...@@ -1078,10 +1076,7 @@ export default async function getBaseWebpackConfig(
buildId, buildId,
rewrites, rewrites,
}), }),
tracer && new ProfilingPlugin(),
new ProfilingPlugin({
tracer,
}),
config.experimental.optimizeFonts && config.experimental.optimizeFonts &&
!dev && !dev &&
isServer && isServer &&
......
...@@ -22,147 +22,150 @@ export default function makeLoader(callback) { ...@@ -22,147 +22,150 @@ export default function makeLoader(callback) {
} }
async function loader(source, inputSourceMap, overrides) { async function loader(source, inputSourceMap, overrides) {
const span = tracer.startSpan('babel-loader') // Provided by profiling-plugin.ts
return traceAsyncFn(span, async () => { return tracer.withSpan(this.currentTraceSpan, () => {
const filename = this.resourcePath const span = tracer.startSpan('babel-loader')
span.setAttribute('filename', filename) return traceAsyncFn(span, async () => {
const filename = this.resourcePath
let loaderOptions = loaderUtils.getOptions(this) || {} span.setAttribute('filename', filename)
let customOptions let loaderOptions = loaderUtils.getOptions(this) || {}
if (overrides && overrides.customOptions) {
const result = await traceAsyncFn( let customOptions
tracer.startSpan('loader-overrides-customoptions'), if (overrides && overrides.customOptions) {
async () => const result = await traceAsyncFn(
await overrides.customOptions.call(this, loaderOptions, { tracer.startSpan('loader-overrides-customoptions'),
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'),
async () => async () =>
await overrides.config.call(this, config, { await overrides.customOptions.call(this, loaderOptions, {
source, source,
map: inputSourceMap, map: inputSourceMap,
customOptions,
}) })
) )
customOptions = result.custom
loaderOptions = result.loader
} }
if (options.sourceMaps === 'inline') { // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
// Babel has this weird behavior where if you set "inline", we // users may safely use either one alongside our default use of
// inline the sourcemap, and set 'result.map = null'. This results // 'this.sourceMap' below without getting error about conflicting aliases.
// in bad behavior from Babel since the maps get put into the code, if (
// which Webpack does not expect, and because the map we return to Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMap') &&
// Webpack is null, which is also bad. To avoid that, we override the !Object.prototype.hasOwnProperty.call(loaderOptions, 'sourceMaps')
// behavior here so "inline" just behaves like 'true'. ) {
options.sourceMaps = true loaderOptions = Object.assign({}, loaderOptions, {
} sourceMaps: loaderOptions.sourceMap,
const { cacheDirectory, cacheIdentifier } = loaderOptions
let result
if (cacheDirectory) {
result = await cache({
source,
options,
cacheDirectory,
cacheIdentifier,
cacheCompression: false,
}) })
} else { delete loaderOptions.sourceMap
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 const programmaticOptions = Object.assign({}, loaderOptions, {
// were used so that this can also handle files loaded with 'extends'. filename,
if (typeof config.babelrc === 'string') { inputSourceMap: inputSourceMap || undefined,
this.addDependency(config.babelrc)
} // Set the default sourcemap behavior based on Webpack's mapping flag,
// but allow users to override if they want.
if (result) { sourceMaps:
const { code, map } = result 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. // If the file was ignored, pass through the original content.
return [source, inputSourceMap] return [source, inputSourceMap]
})
}) })
} }
import { loader } from 'webpack'
import loaderUtils from 'loader-utils' import loaderUtils from 'loader-utils'
import { tracer, traceFn } from '../../tracer' import { tracer, traceFn } from '../../tracer'
...@@ -7,19 +6,21 @@ export type ClientPagesLoaderOptions = { ...@@ -7,19 +6,21 @@ export type ClientPagesLoaderOptions = {
page: string page: string
} }
const nextClientPagesLoader: loader.Loader = function () { // this parameter: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters
const span = tracer.startSpan('next-client-pages-loader') function nextClientPagesLoader(this: any) {
return traceFn(span, () => { return tracer.withSpan(this.currentTraceSpan, () => {
const { absolutePagePath, page } = loaderUtils.getOptions( const span = tracer.startSpan('next-client-pages-loader')
this return traceFn(span, () => {
) as ClientPagesLoaderOptions const { absolutePagePath, page } = loaderUtils.getOptions(
this
) as ClientPagesLoaderOptions
span.setAttribute('absolutePagePath', absolutePagePath) span.setAttribute('absolutePagePath', absolutePagePath)
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath) const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPage = JSON.stringify(page) const stringifiedPage = JSON.stringify(page)
return ` return `
(window.__NEXT_P = window.__NEXT_P || []).push([ (window.__NEXT_P = window.__NEXT_P || []).push([
${stringifiedPage}, ${stringifiedPage},
function () { function () {
...@@ -27,6 +28,7 @@ const nextClientPagesLoader: loader.Loader = function () { ...@@ -27,6 +28,7 @@ const nextClientPagesLoader: loader.Loader = function () {
} }
]); ]);
` `
})
}) })
} }
......
...@@ -4,6 +4,7 @@ import postcss, { Parser } from 'postcss' ...@@ -4,6 +4,7 @@ import postcss, { Parser } from 'postcss'
import webpack from 'webpack' import webpack from 'webpack'
import sources from 'webpack-sources' import sources from 'webpack-sources'
import { tracer, traceAsyncFn } from '../../tracer' import { tracer, traceAsyncFn } from '../../tracer'
import { spans } from './profiling-plugin'
// @ts-ignore: TODO: remove ignore when webpack 5 is stable // @ts-ignore: TODO: remove ignore when webpack 5 is stable
const { RawSource, SourceMapSource } = webpack.sources || sources const { RawSource, SourceMapSource } = webpack.sources || sources
...@@ -71,87 +72,91 @@ export class CssMinimizerPlugin { ...@@ -71,87 +72,91 @@ 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) => {
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', { const span = tracer.startSpan('css-minimizer-plugin', {
attributes: { attributes: {
webpackVersion: 5, webpackVersion: 4,
compilationName: compilation.name,
}, },
}) })
return traceAsyncFn(span, async () => { return traceAsyncFn(span, async () => {
const files = Object.keys(assets) const res = await Promise.all(
await Promise.all( chunks
files .reduce(
.filter((file) => CSS_REGEX.test(file)) (acc, chunk) => acc.concat(chunk.files || []),
[] as string[]
)
.filter((entry) => CSS_REGEX.test(entry))
.map(async (file) => { .map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', { const assetSpan = tracer.startSpan('minify-css', {
attributes: { attributes: {
file, file,
}, },
}) })
return traceAsyncFn(span, async () => { return traceAsyncFn(assetSpan, async () => {
const asset = assets[file] const asset = compilation.assets[file]
// Makes trace attributes the same as webpack 5
const etag = cache.getLazyHashedEtag(asset) // When using webpack 4 the result is not cached
assetSpan.setAttribute('cache', 'MISS')
const cachedResult = await cache.getPromise(file, etag) compilation.assets[file] = await this.optimizeAsset(
file,
assetSpan.setAttribute( asset
'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 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 import { tracer } from '../../tracer'
// License:
/*
Copyright JS Foundation and other contributors
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' const pluginName = 'ProfilingPlugin'
export class ProfilingPlugin { export const spans = new WeakMap()
tracer: any
/**
* @param {ProfilingPluginOptions=} opts options object
*/
constructor(opts: { tracer: any }) {
this.tracer = opts.tracer
}
export class ProfilingPlugin {
apply(compiler: any) { apply(compiler: any) {
const tracer = this.tracer // Only enabled when instrumentation is loaded
if (!tracer.getCurrentSpan()) {
// Compiler Hooks return
Object.keys(compiler.hooks).forEach((hookName) => { }
compiler.hooks[hookName].intercept(
makeInterceptorFor('Compiler', tracer)(hookName)
)
})
Object.keys(compiler.resolverFactory.hooks).forEach((hookName) => { compiler.hooks.compile.tap(pluginName, () => {
compiler.resolverFactory.hooks[hookName].intercept( const span = tracer.startSpan('webpack-compile', {
makeInterceptorFor('Resolver', tracer)(hookName) attributes: { name: compiler.name },
) })
spans.set(compiler, span)
}) })
compiler.hooks.done.tap(pluginName, () => {
compiler.hooks.compilation.tap( spans.get(compiler).end()
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.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) => { compilation.hooks.normalModuleLoader.tap(
if (Reflect.has(instance, 'hooks')) { pluginName,
Object.keys(instance.hooks).forEach((hookName) => { (loaderContext: any, module: any) => {
instance.hooks[hookName].intercept( const parentSpan = spans.get(module)
makeInterceptorFor(logLabel, tracer)(hookName) loaderContext.currentTraceSpan = parentSpan
}
) )
})
}
}
const interceptAllParserHooks = (moduleFactory: any, tracer: any) => {
const moduleTypes = [
'javascript/auto',
'javascript/dynamic',
'javascript/esm',
'json',
'webassembly/experimental',
]
moduleTypes.forEach((moduleType) => { compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
moduleFactory.hooks.parser spans.get(module).end()
.for(moduleType)
.tap('ProfilingPlugin', (parser: any) => {
interceptAllHooksFor(parser, tracer, 'Parser')
}) })
})
}
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