Unverified Commit e27b7e99 authored by Dale Bustad's avatar Dale Bustad Committed by GitHub

Telemetry-compatible tracing (#22713)

A number of changes here.  I recommend viewing the diff with the <a href="?w=1">whitespace flag enabled</a>.

- OpenTelemetry is replaced with a custom and lightweight tracing solution.
- Three trace targets are currently supported: console, Zipkin, and NextJS.
- Tracing is now governed by environment variables rather than `--require instrument.js`.
  + `TRACE_TARGET`: one of `CONSOLE`, `ZIPKIN`, or `TELEMETRY`; defaults to `TELEMETRY` if unset or invalid.
  + `TRACE_ID`: an 8-byte hex-encoded value used as the Zipkin trace ID; if not provided, this value will be randomly generated and passed down to subprocesses.

Other sundry:

- I'm missing something, probably a setup step, with the Zipkin target.  Traces are captured successfully, but you have to manually enter the Trace ID in order to view the trace - it doesn't show up in queries.
- I'm generally unhappy with [this commit](https://github.com/vercel/next.js/pull/22713/commits/235cedcb3ead76b630b4c8aa695f904489da2831).  It is... untidy to provide a telemetry object via `setGlobal`, but I don't have a ready alternative.  Is `distDir` strictly required when creating a new Telemetry object?  I didn't dig too deep here.

As noted, there are a lot of changes, so it'd be great if a reviewer could:

- [ ] pull down the branch and try to break it
- [ ] check the Zipkin traces and identify possible regressions in the functionality

Closes #22570
Fixes #22574
parent fa02b197
// Disable automatic instrumentation
process.env.OTEL_NO_PATCH_MODULES = '*'
const { NodeTracerProvider } = require('@opentelemetry/node')
const { SimpleSpanProcessor } = require('@opentelemetry/tracing')
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin')
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 SimpleSpanProcessor(
new ZipkinExporter({
serviceName: 'next-js',
})
)
)
tracerProvider.register()
...@@ -34,8 +34,6 @@ ...@@ -34,8 +34,6 @@
"publish-stable": "lerna version --force-publish", "publish-stable": "lerna version --force-publish",
"lint-staged": "lint-staged", "lint-staged": "lint-staged",
"next": "node --trace-deprecation packages/next/dist/bin/next", "next": "node --trace-deprecation packages/next/dist/bin/next",
"trace": "node --trace-deprecation -r ./bench/instrument.js packages/next/dist/bin/next",
"trace-debug": "node --inspect --trace-deprecation -r ./bench/instrument.js packages/next/dist/bin/next",
"debug": "node --inspect packages/next/dist/bin/next" "debug": "node --inspect packages/next/dist/bin/next"
}, },
"pre-commit": "lint-staged", "pre-commit": "lint-staged",
...@@ -45,11 +43,6 @@ ...@@ -45,11 +43,6 @@
"@babel/preset-react": "7.12.10", "@babel/preset-react": "7.12.10",
"@fullhuman/postcss-purgecss": "1.3.0", "@fullhuman/postcss-purgecss": "1.3.0",
"@mdx-js/loader": "0.18.0", "@mdx-js/loader": "0.18.0",
"@opentelemetry/exporter-zipkin": "0.14.0",
"@opentelemetry/node": "0.14.0",
"@opentelemetry/plugin-http": "0.14.0",
"@opentelemetry/plugin-https": "0.14.0",
"@opentelemetry/tracing": "0.14.0",
"@testing-library/react": "11.2.5", "@testing-library/react": "11.2.5",
"@types/cheerio": "0.22.16", "@types/cheerio": "0.22.16",
"@types/fs-extra": "8.1.0", "@types/fs-extra": "8.1.0",
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
import * as log from '../build/output/log' import * as log from '../build/output/log'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { NON_STANDARD_NODE_ENV } from '../lib/constants' import { NON_STANDARD_NODE_ENV } from '../lib/constants'
import opentelemetryApi from '@opentelemetry/api'
;['react', 'react-dom'].forEach((dependency) => { ;['react', 'react-dom'].forEach((dependency) => {
try { try {
// When 'npm link' is used it checks the clone location. Not the project. // When 'npm link' is used it checks the clone location. Not the project.
...@@ -107,18 +106,7 @@ if (typeof React.Suspense === 'undefined') { ...@@ -107,18 +106,7 @@ if (typeof React.Suspense === 'undefined') {
process.on('SIGTERM', () => process.exit(0)) process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0)) process.on('SIGINT', () => process.exit(0))
commands[command]() commands[command]().then((exec) => exec(forwardedArgs))
.then((exec) => exec(forwardedArgs))
.then(async () => {
if (command === 'build') {
// @ts-ignore getDelegate exists
const tp = opentelemetryApi.trace.getTracerProvider().getDelegate()
if (tp.shutdown) {
await tp.shutdown()
}
process.exit(0)
}
})
if (command === 'dev') { if (command === 'dev') {
const { CONFIG_FILE } = require('../next-server/lib/constants') const { CONFIG_FILE } = require('../next-server/lib/constants')
......
This diff is collapsed.
...@@ -29,9 +29,8 @@ import { removePathTrailingSlash } from '../client/normalize-trailing-slash' ...@@ -29,9 +29,8 @@ 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 * as Log from './output/log' import * as Log from './output/log'
import opentelemetryApi from '@opentelemetry/api'
import { tracer, traceAsyncFn } from './tracer'
import { loadComponents } from '../next-server/server/load-components' import { loadComponents } from '../next-server/server/load-components'
import { trace } from '../telemetry/trace'
const fileGzipStats: { [k: string]: Promise<number> } = {} const fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => { const fsStatGzip = (file: string) => {
...@@ -719,7 +718,7 @@ export async function isPageStatic( ...@@ -719,7 +718,7 @@ export async function isPageStatic(
runtimeEnvConfig: any, runtimeEnvConfig: any,
locales?: string[], locales?: string[],
defaultLocale?: string, defaultLocale?: string,
spanContext?: any parentId?: any
): Promise<{ ): Promise<{
isStatic?: boolean isStatic?: boolean
isAmpOnly?: boolean isAmpOnly?: boolean
...@@ -731,28 +730,15 @@ export async function isPageStatic( ...@@ -731,28 +730,15 @@ export async function isPageStatic(
prerenderFallback?: boolean | 'blocking' prerenderFallback?: boolean | 'blocking'
isNextImageImported?: boolean isNextImageImported?: boolean
}> { }> {
return opentelemetryApi.context.with( const isPageStaticSpan = trace('is-page-static-utils', parentId)
opentelemetryApi.propagation.extract( return isPageStaticSpan.traceAsyncFn(async () => {
opentelemetryApi.context.active(),
spanContext
),
() => {
return traceAsyncFn(
tracer.startSpan('is-page-static-utils'),
async () => {
try { try {
require('../next-server/lib/runtime-config').setConfig( require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
runtimeEnvConfig
)
const components = await loadComponents(distDir, page, serverless) const components = await loadComponents(distDir, page, serverless)
const mod = components.ComponentMod const mod = components.ComponentMod
const Comp = mod.default || mod const Comp = mod.default || mod
if ( if (!Comp || !isValidElementType(Comp) || typeof Comp === 'string') {
!Comp ||
!isValidElementType(Comp) ||
typeof Comp === 'string'
) {
throw new Error('INVALID_DEFAULT_EXPORT') throw new Error('INVALID_DEFAULT_EXPORT')
} }
...@@ -838,8 +824,7 @@ export async function isPageStatic( ...@@ -838,8 +824,7 @@ export async function isPageStatic(
const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED const isNextImageImported = (global as any).__NEXT_IMAGE_IMPORTED
const config = mod.config || {} const config = mod.config || {}
return { return {
isStatic: isStatic: !hasStaticProps && !hasGetInitialProps && !hasServerProps,
!hasStaticProps && !hasGetInitialProps && !hasServerProps,
isHybridAmp: config.amp === 'hybrid', isHybridAmp: config.amp === 'hybrid',
isAmpOnly: config.amp === true, isAmpOnly: config.amp === true,
prerenderRoutes, prerenderRoutes,
...@@ -853,10 +838,7 @@ export async function isPageStatic( ...@@ -853,10 +838,7 @@ export async function isPageStatic(
if (err.code === 'MODULE_NOT_FOUND') return {} if (err.code === 'MODULE_NOT_FOUND') return {}
throw err throw err
} }
} })
)
}
)
} }
export async function hasCustomGetInitialProps( export async function hasCustomGetInitialProps(
......
import { createHash } from 'crypto' import { createHash } from 'crypto'
import { tracer, traceAsyncFn, traceFn } from '../../../../tracer' import { trace } from '../../../../../telemetry/trace'
import transform from './transform' import transform from './transform'
import cacache from 'next/dist/compiled/cacache' import cacache from 'next/dist/compiled/cacache'
async function read(cacheDirectory, etag) { async function read(cacheDirectory, etag) {
const cachedResult = await traceAsyncFn( const cachedResult = await trace('read-cache-file').traceAsyncFn(() =>
tracer.startSpan('read-cache-file'), cacache.get(cacheDirectory, etag)
async () => await cacache.get(cacheDirectory, etag)
) )
return JSON.parse(cachedResult.data) return JSON.parse(cachedResult.data)
} }
function write(cacheDirectory, etag, data) { function write(cacheDirectory, etag, data) {
return traceAsyncFn(tracer.startSpan('write-cache-file'), () => return trace('write-cache-file').traceAsyncFn(() =>
cacache.put(cacheDirectory, etag, JSON.stringify(data)) cacache.put(cacheDirectory, etag, JSON.stringify(data))
) )
} }
const etag = function (source, identifier, options) { const etag = function (source, identifier, options) {
return traceFn(tracer.startSpan('etag'), () => { return trace('etag').traceFn(() => {
const hash = createHash('md4') const hash = createHash('md4')
const contents = JSON.stringify({ source, options, identifier }) const contents = JSON.stringify({ source, options, identifier })
...@@ -31,8 +29,9 @@ const etag = function (source, identifier, options) { ...@@ -31,8 +29,9 @@ const etag = function (source, identifier, options) {
} }
export default async function handleCache(params) { export default async function handleCache(params) {
const span = tracer.startSpan('handle-cache') const span = trace('handle-cache')
return traceAsyncFn(span, async () => {
return span.traceAsyncFn(async () => {
const { source, options = {}, cacheIdentifier, cacheDirectory } = params const { source, options = {}, cacheIdentifier, cacheDirectory } = params
const file = etag(source, cacheIdentifier) const file = etag(source, cacheIdentifier)
...@@ -47,12 +46,9 @@ export default async function handleCache(params) { ...@@ -47,12 +46,9 @@ export default async function handleCache(params) {
// Otherwise just transform the file // Otherwise just transform the file
// return it to the user asap and write it in cache // return it to the user asap and write it in cache
const result = await traceAsyncFn( const result = await trace('transform').traceAsyncFn(async () => {
tracer.startSpan('transform'),
async () => {
return transform(source, options) return transform(source, options)
} })
)
await write(cacheDirectory, file, result) await write(cacheDirectory, file, result)
......
// import babel from 'next/dist/compiled/babel/core' // import babel from 'next/dist/compiled/babel/core'
import loaderUtils from 'next/dist/compiled/loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import { tracer, traceAsyncFn, traceFn } from '../../../../tracer' import { trace } from '../../../../../telemetry/trace'
import cache from './cache' import cache from './cache'
import transform from './transform' import transform from './transform'
...@@ -22,19 +22,19 @@ export default function makeLoader(callback) { ...@@ -22,19 +22,19 @@ export default function makeLoader(callback) {
} }
async function loader(source, inputSourceMap, overrides) { async function loader(source, inputSourceMap, overrides) {
// Provided by profiling-plugin.ts // this.currentTraceSpan is provided by profiling-plugin.ts
return tracer.withSpan(this.currentTraceSpan, () => { const loaderSpan = trace('babel-loader', this.currentTraceSpan?.id)
const span = tracer.startSpan('babel-loader')
return traceAsyncFn(span, async () => { return loaderSpan.traceAsyncFn(async () => {
const filename = this.resourcePath const filename = this.resourcePath
span.setAttribute('filename', filename) loaderSpan.setAttribute('filename', filename)
let loaderOptions = loaderUtils.getOptions(this) || {} let loaderOptions = loaderUtils.getOptions(this) || {}
let customOptions let customOptions
if (overrides && overrides.customOptions) { if (overrides && overrides.customOptions) {
const result = await traceAsyncFn( const customoptionsSpan = trace('loader-overrides-customoptions')
tracer.startSpan('loader-overrides-customoptions'), const result = await customoptionsSpan.traceAsyncFn(
async () => async () =>
await overrides.customOptions.call(this, loaderOptions, { await overrides.customOptions.call(this, loaderOptions, {
source, source,
...@@ -95,18 +95,16 @@ async function loader(source, inputSourceMap, overrides) { ...@@ -95,18 +95,16 @@ async function loader(source, inputSourceMap, overrides) {
delete programmaticOptions.cacheDirectory delete programmaticOptions.cacheDirectory
delete programmaticOptions.cacheIdentifier delete programmaticOptions.cacheIdentifier
const config = traceFn( const partialConfigSpan = trace('babel-load-partial-config-async')
tracer.startSpan('babel-load-partial-config-async'), const config = partialConfigSpan.traceFn(() => {
() => {
return babel.loadPartialConfig(programmaticOptions) return babel.loadPartialConfig(programmaticOptions)
} })
)
if (config) { if (config) {
let options = config.options let options = config.options
if (overrides && overrides.config) { if (overrides && overrides.config) {
options = await traceAsyncFn( const overridesSpan = trace('loader-overrides-config')
tracer.startSpan('loader-overrides-config'), options = await overridesSpan.traceAsyncFn(
async () => async () =>
await overrides.config.call(this, config, { await overrides.config.call(this, config, {
source, source,
...@@ -138,17 +136,12 @@ async function loader(source, inputSourceMap, overrides) { ...@@ -138,17 +136,12 @@ async function loader(source, inputSourceMap, overrides) {
cacheCompression: false, cacheCompression: false,
}) })
} else { } else {
result = await traceAsyncFn( const transformSpan = trace('transform')
tracer.startSpan('transform', { transformSpan.setAttribute('filename', filename)
attributes: { transformSpan.setAttribute('cache', 'DISABLED')
filename, result = await transformSpan.traceAsyncFn(async () => {
cache: 'DISABLED',
},
}),
async () => {
return transform(source, options) return transform(source, options)
} })
)
} }
// TODO: Babel should really provide the full list of config files that // TODO: Babel should really provide the full list of config files that
...@@ -167,5 +160,4 @@ async function loader(source, inputSourceMap, overrides) { ...@@ -167,5 +160,4 @@ async function loader(source, inputSourceMap, overrides) {
// 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 loaderUtils from 'next/dist/compiled/loader-utils' import loaderUtils from 'next/dist/compiled/loader-utils'
import { tracer, traceFn } from '../../tracer'
export type ClientPagesLoaderOptions = { export type ClientPagesLoaderOptions = {
absolutePagePath: string absolutePagePath: string
...@@ -8,14 +7,16 @@ export type ClientPagesLoaderOptions = { ...@@ -8,14 +7,16 @@ export type ClientPagesLoaderOptions = {
// this parameter: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters // this parameter: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters
function nextClientPagesLoader(this: any) { function nextClientPagesLoader(this: any) {
return tracer.withSpan(this.currentTraceSpan, () => { const pagesLoaderSpan = this.currentTraceSpan.traceChild(
const span = tracer.startSpan('next-client-pages-loader') 'next-client-pages-loader'
return traceFn(span, () => { )
return pagesLoaderSpan.traceFn(() => {
const { absolutePagePath, page } = loaderUtils.getOptions( const { absolutePagePath, page } = loaderUtils.getOptions(
this this
) as ClientPagesLoaderOptions ) as ClientPagesLoaderOptions
span.setAttribute('absolutePagePath', absolutePagePath) pagesLoaderSpan.setAttribute('absolutePagePath', absolutePagePath)
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath) const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath)
const stringifiedPage = JSON.stringify(page) const stringifiedPage = JSON.stringify(page)
...@@ -29,7 +30,6 @@ function nextClientPagesLoader(this: any) { ...@@ -29,7 +30,6 @@ function nextClientPagesLoader(this: any) {
]); ]);
` `
}) })
})
} }
export default nextClientPagesLoader export default nextClientPagesLoader
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
ROUTES_MANIFEST, ROUTES_MANIFEST,
REACT_LOADABLE_MANIFEST, REACT_LOADABLE_MANIFEST,
} from '../../../../next-server/lib/constants' } from '../../../../next-server/lib/constants'
import { tracer, traceFn } from '../../../tracer' import { trace } from '../../../../telemetry/trace'
export type ServerlessLoaderQuery = { export type ServerlessLoaderQuery = {
page: string page: string
...@@ -34,8 +34,8 @@ export type ServerlessLoaderQuery = { ...@@ -34,8 +34,8 @@ export type ServerlessLoaderQuery = {
} }
const nextServerlessLoader: webpack.loader.Loader = function () { const nextServerlessLoader: webpack.loader.Loader = function () {
const span = tracer.startSpan('next-serverless-loader') const loaderSpan = trace('next-serverless-loader')
return traceFn(span, () => { return loaderSpan.traceFn(() => {
const { const {
distDir, distDir,
absolutePagePath, absolutePagePath,
......
...@@ -17,7 +17,6 @@ import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-e ...@@ -17,7 +17,6 @@ import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-e
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin' import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
import { Rewrite } from '../../../lib/load-custom-routes' import { Rewrite } from '../../../lib/load-custom-routes'
import { getSortedRoutes } from '../../../next-server/lib/router/utils' import { getSortedRoutes } from '../../../next-server/lib/router/utils'
import { tracer, traceFn } from '../../tracer'
import { spans } from './profiling-plugin' import { spans } from './profiling-plugin'
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> } type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
...@@ -31,9 +30,12 @@ function generateClientManifest( ...@@ -31,9 +30,12 @@ function generateClientManifest(
assetMap: BuildManifest, assetMap: BuildManifest,
rewrites: Rewrite[] rewrites: Rewrite[]
): string { ): string {
return tracer.withSpan(spans.get(compiler), () => { const compilerSpan = spans.get(compiler)
const span = tracer.startSpan('NextJsBuildManifest-generateClientManifest') const genClientManifestSpan = compilerSpan?.traceChild(
return traceFn(span, () => { 'NextJsBuildManifest-generateClientManifest'
)
return genClientManifestSpan?.traceFn(() => {
const clientManifest: ClientBuildManifest = { const clientManifest: ClientBuildManifest = {
// TODO: update manifest type to include rewrites // TODO: update manifest type to include rewrites
__rewrites: rewrites as any, __rewrites: rewrites as any,
...@@ -62,7 +64,6 @@ function generateClientManifest( ...@@ -62,7 +64,6 @@ function generateClientManifest(
return devalue(clientManifest) return devalue(clientManifest)
}) })
})
} }
function isJsFile(file: string): boolean { function isJsFile(file: string): boolean {
...@@ -103,9 +104,11 @@ export default class BuildManifestPlugin { ...@@ -103,9 +104,11 @@ export default class BuildManifestPlugin {
} }
createAssets(compiler: any, compilation: any, assets: any) { createAssets(compiler: any, compilation: any, assets: any) {
return tracer.withSpan(spans.get(compiler), () => { const compilerSpan = spans.get(compiler)
const span = tracer.startSpan('NextJsBuildManifest-createassets') const createAssetsSpan = compilerSpan?.traceChild(
return traceFn(span, () => { 'NextJsBuildManifest-createassets'
)
return createAssetsSpan?.traceFn(() => {
const namedChunks: Map<string, webpack.compilation.Chunk> = const namedChunks: Map<string, webpack.compilation.Chunk> =
compilation.namedChunks compilation.namedChunks
const assetMap: DeepMutable<BuildManifest> = { const assetMap: DeepMutable<BuildManifest> = {
...@@ -152,8 +155,7 @@ export default class BuildManifestPlugin { ...@@ -152,8 +155,7 @@ export default class BuildManifestPlugin {
) )
for (const entrypoint of compilation.entrypoints.values()) { for (const entrypoint of compilation.entrypoints.values()) {
const isAmpRuntime = const isAmpRuntime = entrypoint.name === CLIENT_STATIC_FILES_RUNTIME_AMP
entrypoint.name === CLIENT_STATIC_FILES_RUNTIME_AMP
if (isAmpRuntime) { if (isAmpRuntime) {
for (const file of entrypoint.getFiles()) { for (const file of entrypoint.getFiles()) {
...@@ -222,7 +224,6 @@ export default class BuildManifestPlugin { ...@@ -222,7 +224,6 @@ export default class BuildManifestPlugin {
return assets return assets
}) })
})
} }
apply(compiler: webpack.Compiler) { apply(compiler: webpack.Compiler) {
......
...@@ -4,7 +4,7 @@ import path from 'path' ...@@ -4,7 +4,7 @@ import path from 'path'
import bfj from 'next/dist/compiled/bfj' import bfj from 'next/dist/compiled/bfj'
import { spans } from './profiling-plugin' import { spans } from './profiling-plugin'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import { tracer, traceAsyncFn } from '../../tracer' import { trace } from '../../../telemetry/trace'
const STATS_VERSION = 0 const STATS_VERSION = 0
...@@ -75,10 +75,10 @@ export default class BuildStatsPlugin { ...@@ -75,10 +75,10 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync( compiler.hooks.done.tapAsync(
'NextJsBuildStats', 'NextJsBuildStats',
async (stats, callback) => { async (stats, callback) => {
tracer.withSpan(spans.get(compiler), async () => { const compilerSpan = spans.get(compiler)
try { try {
const writeStatsSpan = tracer.startSpan('NextJsBuildStats') const writeStatsSpan = trace('NextJsBuildStats', compilerSpan?.id)
await traceAsyncFn(writeStatsSpan, () => { await writeStatsSpan.traceAsyncFn(() => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const statsJson = reduceSize( const statsJson = reduceSize(
stats.toJson({ stats.toJson({
...@@ -114,7 +114,6 @@ export default class BuildStatsPlugin { ...@@ -114,7 +114,6 @@ export default class BuildStatsPlugin {
} catch (err) { } catch (err) {
callback(err) callback(err)
} }
})
} }
) )
} }
......
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
isWebpack5, isWebpack5,
sources, sources,
} from 'next/dist/compiled/webpack/webpack' } from 'next/dist/compiled/webpack/webpack'
import { tracer, traceAsyncFn } from '../../tracer' import { trace } from '../../../telemetry/trace'
import { spans } from './profiling-plugin' import { spans } from './profiling-plugin'
// https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20 // https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20
...@@ -70,25 +70,23 @@ export class CssMinimizerPlugin { ...@@ -70,25 +70,23 @@ 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 compilerSpan = spans.get(compiler)
const span = tracer.startSpan('css-minimizer-plugin', { const cssMinimizerSpan = trace(
attributes: { 'css-minimizer-plugin',
webpackVersion: 5, compilerSpan?.id
}, )
}) cssMinimizerSpan.setAttribute('webpackVersion', 5)
return traceAsyncFn(span, async () => { return cssMinimizerSpan.traceAsyncFn(async () => {
const files = Object.keys(assets) const files = Object.keys(assets)
await Promise.all( await Promise.all(
files files
.filter((file) => CSS_REGEX.test(file)) .filter((file) => CSS_REGEX.test(file))
.map(async (file) => { .map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', { const assetSpan = trace('minify-css', cssMinimizerSpan.id)
attributes: { assetSpan.setAttribute('file', file)
file,
}, return assetSpan.traceAsyncFn(async () => {
})
return traceAsyncFn(span, async () => {
const asset = assets[file] const asset = assets[file]
const etag = cache.getLazyHashedEtag(asset) const etag = cache.getLazyHashedEtag(asset)
...@@ -111,7 +109,6 @@ export class CssMinimizerPlugin { ...@@ -111,7 +109,6 @@ export class CssMinimizerPlugin {
}) })
) )
}) })
})
} }
) )
return return
...@@ -119,16 +116,16 @@ export class CssMinimizerPlugin { ...@@ -119,16 +116,16 @@ export class CssMinimizerPlugin {
compilation.hooks.optimizeChunkAssets.tapPromise( compilation.hooks.optimizeChunkAssets.tapPromise(
'CssMinimizerPlugin', 'CssMinimizerPlugin',
(chunks: webpack.compilation.Chunk[]) => { (chunks: webpack.compilation.Chunk[]) => {
return tracer.withSpan(spans.get(compiler), () => { const compilerSpan = spans.get(compiler)
const span = tracer.startSpan('css-minimizer-plugin', { const cssMinimizerSpan = trace(
attributes: { 'css-minimizer-plugin',
webpackVersion: 4, compilerSpan?.id
compilationName: compilation.name, )
}, cssMinimizerSpan.setAttribute('webpackVersion', 4)
}) cssMinimizerSpan.setAttribute('compilationName', compilation.name)
return traceAsyncFn(span, async () => { return cssMinimizerSpan.traceAsyncFn(async () => {
const res = await Promise.all( return await Promise.all(
chunks chunks
.reduce( .reduce(
(acc, chunk) => acc.concat(chunk.files || []), (acc, chunk) => acc.concat(chunk.files || []),
...@@ -136,12 +133,10 @@ export class CssMinimizerPlugin { ...@@ -136,12 +133,10 @@ export class CssMinimizerPlugin {
) )
.filter((entry) => CSS_REGEX.test(entry)) .filter((entry) => CSS_REGEX.test(entry))
.map(async (file) => { .map(async (file) => {
const assetSpan = tracer.startSpan('minify-css', { const assetSpan = trace('minify-css', cssMinimizerSpan.id)
attributes: { assetSpan.setAttribute('file', file)
file,
}, return assetSpan.traceAsyncFn(async () => {
})
return traceAsyncFn(assetSpan, async () => {
const asset = compilation.assets[file] const asset = compilation.assets[file]
// Makes trace attributes the same as webpack 5 // Makes trace attributes the same as webpack 5
// When using webpack 4 the result is not cached // When using webpack 4 the result is not cached
...@@ -153,8 +148,6 @@ export class CssMinimizerPlugin { ...@@ -153,8 +148,6 @@ export class CssMinimizerPlugin {
}) })
}) })
) )
return res
})
}) })
} }
) )
......
import { tracer, stackPush, stackPop } from '../../tracer'
import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack' import { webpack, isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { import { trace, stackPush, stackPop, Span } from '../../../telemetry/trace'
Span,
trace,
ProxyTracerProvider,
NoopTracerProvider,
} from '@opentelemetry/api'
const pluginName = 'ProfilingPlugin' const pluginName = 'ProfilingPlugin'
export const spans = new WeakMap() export const spans = new WeakMap<any, Span>()
function getNormalModuleLoaderHook(compilation: any) { function getNormalModuleLoaderHook(compilation: any) {
if (isWebpack5) { if (isWebpack5) {
...@@ -19,23 +13,10 @@ function getNormalModuleLoaderHook(compilation: any) { ...@@ -19,23 +13,10 @@ function getNormalModuleLoaderHook(compilation: any) {
return compilation.hooks.normalModuleLoader return compilation.hooks.normalModuleLoader
} }
function tracingIsEnabled() {
const tracerProvider: any = trace.getTracerProvider()
if (tracerProvider instanceof ProxyTracerProvider) {
const proxyDelegate: any = tracerProvider.getDelegate()
return !(proxyDelegate instanceof NoopTracerProvider)
}
return false
}
export class ProfilingPlugin { export class ProfilingPlugin {
compiler: any compiler: any
apply(compiler: any) { apply(compiler: any) {
// Only enable plugin when instrumentation is loaded
if (!tracingIsEnabled()) {
return
}
this.traceTopLevelHooks(compiler) this.traceTopLevelHooks(compiler)
this.traceCompilationHooks(compiler) this.traceCompilationHooks(compiler)
this.compiler = compiler this.compiler = compiler
...@@ -46,7 +27,7 @@ export class ProfilingPlugin { ...@@ -46,7 +27,7 @@ export class ProfilingPlugin {
startHook: any, startHook: any,
stopHook: any, stopHook: any,
attrs?: any, attrs?: any,
onSetSpan?: (span: Span | undefined) => void onSetSpan?: (span: Span) => void
) { ) {
let span: Span | undefined let span: Span | undefined
startHook.tap(pluginName, () => { startHook.tap(pluginName, () => {
...@@ -60,7 +41,7 @@ export class ProfilingPlugin { ...@@ -60,7 +41,7 @@ export class ProfilingPlugin {
if (!span) { if (!span) {
return return
} }
stackPop(this.compiler, span, spanName) stackPop(this.compiler, span)
}) })
} }
...@@ -72,7 +53,7 @@ export class ProfilingPlugin { ...@@ -72,7 +53,7 @@ export class ProfilingPlugin {
} }
}) })
stopHook.tap(pluginName, () => { stopHook.tap(pluginName, () => {
stackPop(this.compiler, span, spanName) stackPop(this.compiler, span)
}) })
} }
...@@ -82,7 +63,7 @@ export class ProfilingPlugin { ...@@ -82,7 +63,7 @@ export class ProfilingPlugin {
compiler.hooks.compile, compiler.hooks.compile,
compiler.hooks.done, compiler.hooks.done,
() => { () => {
return { attributes: { name: compiler.name } } return { name: compiler.name }
}, },
(span) => spans.set(compiler, span) (span) => spans.set(compiler, span)
) )
...@@ -115,12 +96,11 @@ export class ProfilingPlugin { ...@@ -115,12 +96,11 @@ export class ProfilingPlugin {
if (!compilerSpan) { if (!compilerSpan) {
return return
} }
tracer.withSpan(compilerSpan, () => {
const span = tracer.startSpan('build-module') const span = trace('build-module', compilerSpan.id)
span.setAttribute('name', module.userRequest) span.setAttribute('name', module.userRequest)
spans.set(module, span) spans.set(module, span)
}) })
})
getNormalModuleLoaderHook(compilation).tap( getNormalModuleLoaderHook(compilation).tap(
pluginName, pluginName,
...@@ -131,7 +111,7 @@ export class ProfilingPlugin { ...@@ -131,7 +111,7 @@ export class ProfilingPlugin {
) )
compilation.hooks.succeedModule.tap(pluginName, (module: any) => { compilation.hooks.succeedModule.tap(pluginName, (module: any) => {
spans.get(module).end() spans.get(module)?.stop()
}) })
this.traceHookPair( this.traceHookPair(
......
...@@ -10,7 +10,6 @@ import pLimit from 'p-limit' ...@@ -10,7 +10,6 @@ import pLimit from 'p-limit'
import jestWorker from 'jest-worker' import jestWorker from 'jest-worker'
import crypto from 'crypto' import crypto from 'crypto'
import cacache from 'next/dist/compiled/cacache' import cacache from 'next/dist/compiled/cacache'
import { tracer, traceAsyncFn } from '../../../../tracer'
import { spans } from '../../profiling-plugin' import { spans } from '../../profiling-plugin'
function getEcmaVersion(environment) { function getEcmaVersion(environment) {
...@@ -123,16 +122,11 @@ class TerserPlugin { ...@@ -123,16 +122,11 @@ class TerserPlugin {
{ SourceMapSource, RawSource } { SourceMapSource, RawSource }
) { ) {
const compilerSpan = spans.get(compiler) const compilerSpan = spans.get(compiler)
const terserSpan = compilerSpan.traceChild('terser-webpack-plugin-optimize')
terserSpan.setAttribute('webpackVersion', isWebpack5 ? 5 : 4)
terserSpan.setAttribute('compilationName', compilation.name)
return tracer.withSpan(compilerSpan, () => { return terserSpan.traceAsyncFn(async () => {
const span = tracer.startSpan('terser-webpack-plugin-optimize', {
attributes: {
webpackVersion: isWebpack5 ? 5 : 4,
compilationName: compilation.name,
},
})
return traceAsyncFn(span, async () => {
let numberOfAssetsForMinify = 0 let numberOfAssetsForMinify = 0
const assetsList = isWebpack5 const assetsList = isWebpack5
? Object.keys(assets) ? Object.keys(assets)
...@@ -223,14 +217,14 @@ class TerserPlugin { ...@@ -223,14 +217,14 @@ class TerserPlugin {
const { name, inputSource, info, eTag } = asset const { name, inputSource, info, eTag } = asset
let { output } = asset let { output } = asset
const assetSpan = tracer.startSpan('minify-js', { const minifySpan = terserSpan.traceChild('minify-fs')
attributes: { minifySpan.setAttribute('name', name)
name, minifySpan.setAttribute(
cache: typeof output === 'undefined' ? 'MISS' : 'HIT', 'cache',
}, typeof output === 'undefined' ? 'MISS' : 'HIT'
}) )
return traceAsyncFn(assetSpan, async () => { return minifySpan.traceAsyncFn(async () => {
if (!output) { if (!output) {
const { const {
source: sourceFromInputSource, source: sourceFromInputSource,
...@@ -310,7 +304,6 @@ class TerserPlugin { ...@@ -310,7 +304,6 @@ class TerserPlugin {
await initializedWorker.end() await initializedWorker.end()
} }
}) })
})
} }
/** /**
......
...@@ -46,8 +46,7 @@ import { PrerenderManifest } from '../build' ...@@ -46,8 +46,7 @@ import { PrerenderManifest } from '../build'
import exportPage from './worker' import exportPage from './worker'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { getPagePath } from '../next-server/server/require' import { getPagePath } from '../next-server/server/require'
import { tracer, traceFn, traceAsyncFn } from '../build/tracer' import { trace } from '../telemetry/trace'
import opentelemetryApi from '@opentelemetry/api'
const exists = promisify(existsOrig) const exists = promisify(existsOrig)
...@@ -137,20 +136,21 @@ export default async function exportApp( ...@@ -137,20 +136,21 @@ export default async function exportApp(
options: ExportOptions, options: ExportOptions,
configuration?: NextConfig configuration?: NextConfig
): Promise<void> { ): Promise<void> {
const nextExportSpan = tracer.startSpan('next-export') const nextExportSpan = trace('next-export')
return traceAsyncFn(nextExportSpan, async () => {
return nextExportSpan.traceAsyncFn(async () => {
dir = resolve(dir) dir = resolve(dir)
// attempt to load global env values so they are available in next.config.js // attempt to load global env values so they are available in next.config.js
traceFn(tracer.startSpan('load-dotenv'), () => nextExportSpan
loadEnvConfig(dir, false, Log) .traceChild('load-dotenv')
) .traceFn(() => loadEnvConfig(dir, false, Log))
const nextConfig = const nextConfig =
configuration || configuration ||
(await traceAsyncFn(tracer.startSpan('load-next-config'), () => (await nextExportSpan
loadConfig(PHASE_EXPORT, dir) .traceChild('load-next-config')
)) .traceAsyncFn(() => loadConfig(PHASE_EXPORT, dir)))
const threads = options.threads || Math.max(cpus().length - 1, 1) const threads = options.threads || Math.max(cpus().length - 1, 1)
const distDir = join(dir, nextConfig.distDir) const distDir = join(dir, nextConfig.distDir)
...@@ -276,7 +276,9 @@ export default async function exportApp( ...@@ -276,7 +276,9 @@ export default async function exportApp(
if (!options.silent) { if (!options.silent) {
Log.info('Copying "static" directory') Log.info('Copying "static" directory')
} }
await traceAsyncFn(tracer.startSpan('copy-static-directory'), () => await nextExportSpan
.traceChild('copy-static-directory')
.traceAsyncFn(() =>
recursiveCopy(join(dir, 'static'), join(outDir, 'static')) recursiveCopy(join(dir, 'static'), join(outDir, 'static'))
) )
} }
...@@ -289,7 +291,9 @@ export default async function exportApp( ...@@ -289,7 +291,9 @@ export default async function exportApp(
if (!options.silent) { if (!options.silent) {
Log.info('Copying "static build" directory') Log.info('Copying "static build" directory')
} }
await traceAsyncFn(tracer.startSpan('copy-next-static-directory'), () => await nextExportSpan
.traceChild('copy-next-static-directory')
.traceAsyncFn(() =>
recursiveCopy( recursiveCopy(
join(distDir, CLIENT_STATIC_FILES_PATH), join(distDir, CLIENT_STATIC_FILES_PATH),
join(outDir, '_next', CLIENT_STATIC_FILES_PATH) join(outDir, '_next', CLIENT_STATIC_FILES_PATH)
...@@ -321,9 +325,9 @@ export default async function exportApp( ...@@ -321,9 +325,9 @@ export default async function exportApp(
} }
if (!options.buildExport) { if (!options.buildExport) {
const { isNextImageImported } = await traceAsyncFn( const { isNextImageImported } = await nextExportSpan
tracer.startSpan('is-next-image-imported'), .traceChild('is-next-image-imported')
() => .traceAsyncFn(() =>
promises promises
.readFile(join(distDir, EXPORT_MARKER), 'utf8') .readFile(join(distDir, EXPORT_MARKER), 'utf8')
.then((text) => JSON.parse(text)) .then((text) => JSON.parse(text))
...@@ -376,9 +380,9 @@ export default async function exportApp( ...@@ -376,9 +380,9 @@ export default async function exportApp(
if (!options.silent && !options.buildExport) { if (!options.silent && !options.buildExport) {
Log.info(`Launching ${threads} workers`) Log.info(`Launching ${threads} workers`)
} }
const exportPathMap = await traceAsyncFn( const exportPathMap = await nextExportSpan
tracer.startSpan('run-export-path-map'), .traceChild('run-export-path-map')
() => .traceAsyncFn(() =>
nextConfig.exportPathMap(defaultPathMap, { nextConfig.exportPathMap(defaultPathMap, {
dev: false, dev: false,
dir, dir,
...@@ -483,7 +487,9 @@ export default async function exportApp( ...@@ -483,7 +487,9 @@ export default async function exportApp(
if (!options.silent) { if (!options.silent) {
Log.info('Copying "public" directory') Log.info('Copying "public" directory')
} }
await traceAsyncFn(tracer.startSpan('copy-public-directory'), () => await nextExportSpan
.traceChild('copy-public-directory')
.traceAsyncFn(() =>
recursiveCopy(publicDir, outDir, { recursiveCopy(publicDir, outDir, {
filter(path) { filter(path) {
// Exclude paths used by pages // Exclude paths used by pages
...@@ -508,17 +514,10 @@ export default async function exportApp( ...@@ -508,17 +514,10 @@ export default async function exportApp(
await Promise.all( await Promise.all(
filteredPaths.map(async (path) => { filteredPaths.map(async (path) => {
const pageExportSpan = tracer.startSpan('export-page', { const pageExportSpan = nextExportSpan.traceChild('export-page')
attributes: { path }, pageExportSpan.setAttribute('path', path)
})
return traceAsyncFn(pageExportSpan, async () => {
const spanContext = {}
opentelemetryApi.propagation.inject(
opentelemetryApi.context.active(),
spanContext
)
return pageExportSpan.traceAsyncFn(async () => {
const result = await worker.default({ const result = await worker.default({
path, path,
pathMap: exportPathMap[path], pathMap: exportPathMap[path],
...@@ -533,7 +532,7 @@ export default async function exportApp( ...@@ -533,7 +532,7 @@ export default async function exportApp(
optimizeFonts: nextConfig.experimental.optimizeFonts, optimizeFonts: nextConfig.experimental.optimizeFonts,
optimizeImages: nextConfig.experimental.optimizeImages, optimizeImages: nextConfig.experimental.optimizeImages,
optimizeCss: nextConfig.experimental.optimizeCss, optimizeCss: nextConfig.experimental.optimizeCss,
spanContext, parentSpanId: pageExportSpan.id,
}) })
for (const validation of result.ampValidations || []) { for (const validation of result.ampValidations || []) {
......
...@@ -16,8 +16,7 @@ import { GetStaticProps } from '../types' ...@@ -16,8 +16,7 @@ import { GetStaticProps } from '../types'
import { requireFontManifest } from '../next-server/server/require' import { requireFontManifest } from '../next-server/server/require'
import { FontManifest } from '../next-server/server/font-utils' import { FontManifest } from '../next-server/server/font-utils'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
import { tracer, traceAsyncFn } from '../build/tracer' import { trace } from '../telemetry/trace'
import opentelemetryApi from '@opentelemetry/api'
const envConfig = require('../next-server/lib/runtime-config') const envConfig = require('../next-server/lib/runtime-config')
...@@ -52,7 +51,7 @@ interface ExportPageInput { ...@@ -52,7 +51,7 @@ interface ExportPageInput {
optimizeFonts: boolean optimizeFonts: boolean
optimizeImages: boolean optimizeImages: boolean
optimizeCss: any optimizeCss: any
spanContext: any parentSpanId: any
} }
interface ExportPageResults { interface ExportPageResults {
...@@ -85,7 +84,7 @@ type ComponentModule = ComponentType<{}> & { ...@@ -85,7 +84,7 @@ type ComponentModule = ComponentType<{}> & {
} }
export default async function exportPage({ export default async function exportPage({
spanContext, parentSpanId,
path, path,
pathMap, pathMap,
distDir, distDir,
...@@ -100,13 +99,9 @@ export default async function exportPage({ ...@@ -100,13 +99,9 @@ export default async function exportPage({
optimizeImages, optimizeImages,
optimizeCss, optimizeCss,
}: ExportPageInput): Promise<ExportPageResults> { }: ExportPageInput): Promise<ExportPageResults> {
return opentelemetryApi.context.with( const exportPageSpan = trace('export-page-worker', parentSpanId)
opentelemetryApi.propagation.extract(
opentelemetryApi.context.active(), return exportPageSpan.traceAsyncFn(async () => {
spanContext
),
() => {
return traceAsyncFn(tracer.startSpan('export-page-worker'), async () => {
let results: ExportPageResults = { let results: ExportPageResults = {
ampValidations: [], ampValidations: [],
} }
...@@ -127,10 +122,7 @@ export default async function exportPage({ ...@@ -127,10 +122,7 @@ export default async function exportPage({
delete query.__nextSsgPath delete query.__nextSsgPath
if (renderOpts.locale) { if (renderOpts.locale) {
const localePathResult = normalizeLocalePath( const localePathResult = normalizeLocalePath(path, renderOpts.locales)
path,
renderOpts.locales
)
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
updatedPath = localePathResult.pathname updatedPath = localePathResult.pathname
...@@ -155,8 +147,7 @@ export default async function exportPage({ ...@@ -155,8 +147,7 @@ export default async function exportPage({
// Check if the page is a specified dynamic route // Check if the page is a specified dynamic route
if (isDynamic && page !== path) { if (isDynamic && page !== path) {
params = params = getRouteMatcher(getRouteRegex(page))(updatedPath) || undefined
getRouteMatcher(getRouteRegex(page))(updatedPath) || undefined
if (params) { if (params) {
// we have to pass these separately for serverless // we have to pass these separately for serverless
if (!serverless) { if (!serverless) {
...@@ -461,6 +452,4 @@ export default async function exportPage({ ...@@ -461,6 +452,4 @@ export default async function exportPage({
return { ...results, error: true } return { ...results, error: true }
} }
}) })
}
)
} }
...@@ -34,6 +34,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path' ...@@ -34,6 +34,7 @@ import { normalizePagePath } from '../next-server/server/normalize-page-path'
import Router, { Params, route } from '../next-server/server/router' import Router, { Params, route } from '../next-server/server/router'
import { eventCliSession } from '../telemetry/events' import { eventCliSession } from '../telemetry/events'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
import { setGlobal } from '../telemetry/trace'
import HotReloader from './hot-reloader' import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file' import { findPageFile } from './lib/find-page-file'
import { getNodeOptionsWithoutInspect } from './lib/utils' import { getNodeOptionsWithoutInspect } from './lib/utils'
...@@ -297,6 +298,8 @@ export default class DevServer extends Server { ...@@ -297,6 +298,8 @@ export default class DevServer extends Server {
isCustomServer: this.isCustomServer, isCustomServer: this.isCustomServer,
}) })
) )
// This is required by the tracing subsystem.
setGlobal('telemetry', telemetry)
} }
protected async close(): Promise<void> { protected async close(): Promise<void> {
......
import api, { Span } from '@opentelemetry/api' import { trace, Span } from './trace'
export const tracer = api.trace.getTracer('next', process.env.__NEXT_VERSION) const stacks = new WeakMap<any, Array<Span>>()
const stoppedSpansSets = new WeakMap<any, Set<Span>>()
const compilerStacks = new WeakMap() export function stackPush(keyObj: any, spanName: string, attrs?: any): Span {
const compilerStoppedSpans = new WeakMap() let stack = stacks.get(keyObj)
export function stackPush(compiler: any, spanName: string, attrs?: any): any {
let stack = compilerStacks.get(compiler)
let span let span
if (!stack) { if (!stack) {
compilerStacks.set(compiler, (stack = [])) stack = []
span = tracer.startSpan(spanName, attrs ? attrs() : undefined) stacks.set(keyObj, stack)
span = trace(spanName, undefined, attrs ? attrs() : undefined)
} else { } else {
const parent = stack[stack.length - 1] const parent = stack[stack.length - 1]
if (parent) { if (parent) {
tracer.withSpan(parent, () => { span = trace(spanName, parent.id, attrs ? attrs() : undefined)
span = tracer.startSpan(spanName, attrs ? attrs() : undefined)
})
} else { } else {
span = tracer.startSpan(spanName, attrs ? attrs() : undefined) span = trace(spanName, undefined, attrs ? attrs() : undefined)
} }
} }
...@@ -27,23 +24,23 @@ export function stackPush(compiler: any, spanName: string, attrs?: any): any { ...@@ -27,23 +24,23 @@ export function stackPush(compiler: any, spanName: string, attrs?: any): any {
return span return span
} }
export function stackPop(compiler: any, span: any, associatedName?: string) { export function stackPop(keyObj: any, span: any): void {
let stack = compilerStacks.get(compiler) let stack = stacks.get(keyObj)
if (!stack) { if (!stack) {
console.warn( console.info(
'Attempted to pop from non-existent stack. Compiler reference must be bad.' 'Attempted to pop from non-existent stack. Key reference must be bad.'
) )
return return
} }
let stoppedSpans: Set<Span> = compilerStoppedSpans.get(compiler) let stoppedSpans = stoppedSpansSets.get(keyObj)
if (!stoppedSpans) { if (!stoppedSpans) {
stoppedSpans = new Set() stoppedSpans = new Set()
compilerStoppedSpans.set(compiler, stoppedSpans) stoppedSpansSets.set(keyObj, stoppedSpans)
} }
if (stoppedSpans.has(span)) { if (stoppedSpans.has(span)) {
console.warn( console.info(
`Attempted to terminate tracing span that was already stopped for ${associatedName}` `Attempted to terminate tracing span that was already stopped for ${span.name}`
) )
return return
} }
...@@ -51,50 +48,24 @@ export function stackPop(compiler: any, span: any, associatedName?: string) { ...@@ -51,50 +48,24 @@ export function stackPop(compiler: any, span: any, associatedName?: string) {
while (true) { while (true) {
let poppedSpan = stack.pop() let poppedSpan = stack.pop()
if (poppedSpan === span) { if (poppedSpan && poppedSpan === span) {
stoppedSpans.add(poppedSpan) stoppedSpans.add(poppedSpan)
span.end() span.stop()
stoppedSpans.add(span) stoppedSpans.add(span)
break break
} else if (poppedSpan === undefined || stack.indexOf(span) === -1) { } else if (poppedSpan === undefined || stack.indexOf(span) === -1) {
// We've either reached the top of the stack or the stack doesn't contain // We've either reached the top of the stack or the stack doesn't contain
// the span for another reason. // the span for another reason.
console.warn(`Tracing span was not found in stack for: ${associatedName}`) console.info(`Tracing span was not found in stack for: ${span.name}`)
stoppedSpans.add(span) stoppedSpans.add(span)
span.end() span.stop()
break break
} else if (stack.indexOf(span) !== -1) { } else if (stack.indexOf(span) !== -1) {
console.warn( console.info(
`Attempted to pop span that was not at top of stack for: ${associatedName}` `Attempted to pop span that was not at top of stack for: ${span.name}`
) )
stoppedSpans.add(poppedSpan) stoppedSpans.add(poppedSpan)
poppedSpan.end() poppedSpan.stop()
}
} }
}
export function traceFn<T extends (...args: unknown[]) => ReturnType<T>>(
span: Span,
fn: T
): ReturnType<T> {
return tracer.withSpan(span, () => {
try {
return fn()
} finally {
span.end()
}
})
}
export function traceAsyncFn<T extends (...args: unknown[]) => ReturnType<T>>(
span: Span,
fn: T
): Promise<ReturnType<T>> {
return tracer.withSpan(span, async () => {
try {
return await fn()
} finally {
span.end()
} }
})
} }
import { trace, Span, SpanStatus } from './trace'
import { SpanId, setGlobal } from './shared'
import { stackPush, stackPop } from './autoparent'
export { trace, SpanId, Span, SpanStatus, stackPush, stackPop, setGlobal }
import { TARGET, SpanId } from '../shared'
import reportToConsole from './to-console'
import reportToZipkin from './to-zipkin'
import reportToTelemetry from './to-telemetry'
type Reporter = (
spanName: string,
duration: number,
timestamp: number,
id: SpanId,
parentId?: SpanId,
attrs?: Object
) => void
const target =
process.env.TRACE_TARGET && process.env.TRACE_TARGET in TARGET
? TARGET[process.env.TRACE_TARGET as TARGET]
: TARGET.TELEMETRY
if (process.env.TRACE_TARGET && !target) {
console.info(
'For TRACE_TARGET, please specify one of: CONSOLE, ZIPKIN, TELEMETRY'
)
}
export let report: Reporter
if (target === TARGET.CONSOLE) {
report = reportToConsole
} else if (target === TARGET.ZIPKIN) {
report = reportToZipkin
} else {
report = reportToTelemetry
}
const idToName = new Map<string, string>()
const reportToConsole = (
spanName: string,
duration: number,
_timestamp: number,
id: string,
parentId?: string,
attrs?: Object
) => {
idToName.set(id, spanName)
const parentStr =
parentId && idToName.has(parentId)
? `, parent: ${idToName.get(parentId)}`
: ''
const attrsStr = attrs
? `, ${Object.entries(attrs)
.map(([key, val]) => `${key}: ${val}`)
.join(', ')}`
: ''
console.log(`[trace] ${spanName} took ${duration} μs${parentStr}${attrsStr}`)
}
export default reportToConsole
import { traceGlobals } from '../shared'
const TRACE_EVENT_WHITELIST = new Map(
Object.entries({
'webpack-invalidated': 'WEBPACK_INVALIDATED',
})
)
const reportToTelemetry = (spanName: string, duration: number) => {
const eventName = TRACE_EVENT_WHITELIST.get(spanName)
if (!eventName) {
return
}
const telemetry = traceGlobals.get('telemetry')
if (!telemetry) {
return
}
telemetry.record({
eventName,
payload: {
durationInMicroseconds: duration,
},
})
}
export default reportToTelemetry
import { randomBytes } from 'crypto'
import fetch from 'node-fetch'
let traceId = process.env.TRACE_ID
if (!traceId) {
traceId = process.env.TRACE_ID = randomBytes(8).toString('hex')
}
const localEndpoint = {
serviceName: 'zipkin-query',
ipv4: '127.0.0.1',
port: 9411,
}
const zipkinUrl = `http://${localEndpoint.ipv4}:${localEndpoint.port}/api/v2/spans`
const reportToLocalHost = (
name: string,
duration: number,
timestamp: number,
id: string,
parentId?: string,
attrs?: Object
) => {
const body = [
{
traceId,
parentId,
name,
id,
timestamp,
duration,
localEndpoint,
tags: attrs,
},
]
// We intentionally do not block on I/O here.
fetch(zipkinUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}).catch(() => {})
}
export default reportToLocalHost
export enum TARGET {
CONSOLE = 'CONSOLE',
ZIPKIN = 'ZIPKIN',
TELEMETRY = 'TELEMETRY',
}
export type SpanId = string
export const traceGlobals: Map<any, any> = new Map()
export const setGlobal = (key: any, val: any) => {
traceGlobals.set(key, val)
}
import { randomBytes } from 'crypto'
import { SpanId } from './shared'
import { report } from './report'
const NUM_OF_MICROSEC_IN_SEC = BigInt('1000')
const getId = () => randomBytes(8).toString('hex')
export enum SpanStatus {
Started,
Stopped,
}
export class Span {
name: string
id: SpanId
parentId?: SpanId
duration: number | null
attrs: { [key: string]: any }
status: SpanStatus
_start: bigint
constructor(name: string, parentId?: SpanId, attrs?: Object) {
this.name = name
this.parentId = parentId
this.duration = null
this.attrs = attrs ? { ...attrs } : {}
this.status = SpanStatus.Started
this.id = getId()
this._start = process.hrtime.bigint()
}
// Durations are reported as microseconds. This gives 1000x the precision
// of something like Date.now(), which reports in milliseconds.
// Additionally, ~285 years can be safely represented as microseconds as
// a float64 in both JSON and JavaScript.
stop() {
const end: bigint = process.hrtime.bigint()
const duration = (end - this._start) / NUM_OF_MICROSEC_IN_SEC
this.status = SpanStatus.Stopped
if (duration > Number.MAX_SAFE_INTEGER) {
throw new Error(`Duration is too long to express as float64: ${duration}`)
}
const timestamp = this._start / NUM_OF_MICROSEC_IN_SEC
report(
this.name,
Number(duration),
Number(timestamp),
this.id,
this.parentId,
this.attrs
)
}
traceChild(name: string, attrs?: Object) {
return new Span(name, this.id, attrs)
}
setAttribute(key: string, value: any) {
this.attrs[key] = value
}
traceFn(fn: any) {
try {
return fn()
} finally {
this.stop()
}
}
async traceAsyncFn(fn: any) {
try {
return await fn()
} finally {
this.stop()
}
}
}
export const trace = (name: string, parentId?: SpanId, attrs?: Object) => {
return new Span(name, parentId, attrs)
}
...@@ -2488,103 +2488,18 @@ ...@@ -2488,103 +2488,18 @@
"@octokit/openapi-types" "^4.0.2" "@octokit/openapi-types" "^4.0.2"
"@types/node" ">= 8" "@types/node" ">= 8"
"@opentelemetry/api@0.14.0", "@opentelemetry/api@^0.14.0": "@opentelemetry/api@0.14.0":
version "0.14.0" version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-0.14.0.tgz#4e17d8d2f1da72b19374efa7b6526aa001267cae" resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-0.14.0.tgz#4e17d8d2f1da72b19374efa7b6526aa001267cae"
integrity sha512-L7RMuZr5LzMmZiQSQDy9O1jo0q+DaLy6XpYJfIGfYSfoJA5qzYwUP3sP1uMIQ549DvxAgM3ng85EaPTM/hUHwQ== integrity sha512-L7RMuZr5LzMmZiQSQDy9O1jo0q+DaLy6XpYJfIGfYSfoJA5qzYwUP3sP1uMIQ549DvxAgM3ng85EaPTM/hUHwQ==
dependencies: dependencies:
"@opentelemetry/context-base" "^0.14.0" "@opentelemetry/context-base" "^0.14.0"
"@opentelemetry/context-async-hooks@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/context-async-hooks/-/context-async-hooks-0.14.0.tgz#34b77e87060bea2489b16f55168c35a580c5de50"
integrity sha512-BgAPWbmelvL/xhI0B17aidyw24E1D69PlKjDZD1L5AI050Glg1+pa6W0jMSoZltnM27Bc+Pc9n22fqxa4kvLRg==
dependencies:
"@opentelemetry/context-base" "^0.14.0"
"@opentelemetry/context-base@^0.14.0": "@opentelemetry/context-base@^0.14.0":
version "0.14.0" version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.14.0.tgz#c67fc20a4d891447ca1a855d7d70fa79a3533001" resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.14.0.tgz#c67fc20a4d891447ca1a855d7d70fa79a3533001"
integrity sha512-sDOAZcYwynHFTbLo6n8kIbLiVF3a3BLkrmehJUyEbT9F+Smbi47kLGS2gG2g0fjBLR/Lr1InPD7kXL7FaTqEkw== integrity sha512-sDOAZcYwynHFTbLo6n8kIbLiVF3a3BLkrmehJUyEbT9F+Smbi47kLGS2gG2g0fjBLR/Lr1InPD7kXL7FaTqEkw==
"@opentelemetry/core@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/core/-/core-0.14.0.tgz#35870e58e3d084dfc4dea99c973f3cf7479b3c6a"
integrity sha512-HJ4VM0cV6c5qjdW7C7koB2IT4ADunCOehxnKFRslQkbDqAEA1w42AZ9679siYALpWYxNqcJyqF2jxCNtfNHa6Q==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/context-base" "^0.14.0"
semver "^7.1.3"
"@opentelemetry/exporter-zipkin@0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/exporter-zipkin/-/exporter-zipkin-0.14.0.tgz#35281d96bd101c0c1a48837868ba8249a0332f7f"
integrity sha512-cg/86iRK64NSGEcWOTzTfqMByyAu8udi3HgcYP2r0WF8JseabfhrlnBp/4/ba4HuoaGe8uXVRmvDT0uAguhO8w==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/resources" "^0.14.0"
"@opentelemetry/tracing" "^0.14.0"
"@opentelemetry/node@0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/node/-/node-0.14.0.tgz#20af865de191a6b14443d619bb08d49003e64b19"
integrity sha512-79egldZKRIqXArjLHD8SYTJUfY5PrcFZll4+ykfbz23vy/CXEVev+1S8bS8bkE08DUNzskDuiFYlLdWTY8fdrQ==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/context-async-hooks" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/tracing" "^0.14.0"
require-in-the-middle "^5.0.0"
semver "^7.1.3"
"@opentelemetry/plugin-http@0.14.0", "@opentelemetry/plugin-http@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/plugin-http/-/plugin-http-0.14.0.tgz#5c5ac23bd3a5cbd8891fce857d52a86bb0dbf51c"
integrity sha512-j6VHSotl+geN+znYgGgn/GohTAirAtHum8Zpc52VZ/WQCrNfHcmEf4NYaGCuz5tW2EgmbH+66iDM/lgNadQuQg==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/semantic-conventions" "^0.14.0"
semver "^7.1.3"
shimmer "^1.2.1"
"@opentelemetry/plugin-https@0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/plugin-https/-/plugin-https-0.14.0.tgz#60a0db56339de497b35aaae0de91b10ecff9b0ea"
integrity sha512-FyT3iaEO9CdGu1EeIe+JmAwfoYwjG9GtHiyWSovFEKO/0OLQLrXA2AQDgltOeelu/snPGB2MF92iPqbpfVdhHw==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/plugin-http" "^0.14.0"
"@opentelemetry/semantic-conventions" "^0.14.0"
semver "^7.1.3"
shimmer "^1.2.1"
"@opentelemetry/resources@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/resources/-/resources-0.14.0.tgz#e89378931b4e02d4b6fb526d237d9594db2bb68a"
integrity sha512-7XVML4HxvoH6kWY+x0mhMc5m0a2YBvPCTSX7yAqyp9XIGvFpdjzAE2ggJ40DZrL1sPv9f0QYAbnIKFDVLBTfGA==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/semantic-conventions@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/semantic-conventions/-/semantic-conventions-0.14.0.tgz#7b48f4300709b34cef0beed8099b79e4a5907886"
integrity sha512-iDGRLQwo+ka1ljlLo4KyuUmzsJwtPw+PyCjetQwn3m/pTXjdyWLGOTARBrpQGpkQp7k87RaCCg5AqZaKFU2G6g==
"@opentelemetry/tracing@0.14.0", "@opentelemetry/tracing@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/tracing/-/tracing-0.14.0.tgz#f1e528b90def213a454ca410ca2d460acfacbe3f"
integrity sha512-sw9lXJQUQO0xaCvHWFUzIhgh2jGFgXubRQ6g1O84Q/ILU93ZMMVt+d97mihcrtBrV89Sy39HF8tAwekjpzv+cA==
dependencies:
"@opentelemetry/api" "^0.14.0"
"@opentelemetry/context-base" "^0.14.0"
"@opentelemetry/core" "^0.14.0"
"@opentelemetry/resources" "^0.14.0"
"@opentelemetry/semantic-conventions" "^0.14.0"
"@polka/url@^1.0.0-next.9": "@polka/url@^1.0.0-next.9":
version "1.0.0-next.11" version "1.0.0-next.11"
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.11.tgz#aeb16f50649a91af79dbe36574b66d0f9e4d9f71" resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.11.tgz#aeb16f50649a91af79dbe36574b66d0f9e4d9f71"
...@@ -11071,10 +10986,6 @@ modify-values@^1.0.0: ...@@ -11071,10 +10986,6 @@ modify-values@^1.0.0:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022" resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
module-details-from-path@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/module-details-from-path/-/module-details-from-path-1.0.3.tgz#114c949673e2a8a35e9d35788527aa37b679da2b"
moment@^2.24.0: moment@^2.24.0:
version "2.24.0" version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
...@@ -13990,15 +13901,6 @@ require-from-string@^1.1.0: ...@@ -13990,15 +13901,6 @@ require-from-string@^1.1.0:
version "1.2.1" version "1.2.1"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-1.2.1.tgz#529c9ccef27380adfec9a2f965b649bbee636418" resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-1.2.1.tgz#529c9ccef27380adfec9a2f965b649bbee636418"
require-in-the-middle@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/require-in-the-middle/-/require-in-the-middle-5.1.0.tgz#b768f800377b47526d026bbf5a7f727f16eb412f"
integrity sha512-M2rLKVupQfJ5lf9OvqFGIT+9iVLnTmjgbOmpil12hiSQNn5zJTKGPoIisETNjfK+09vP3rpm1zJajmErpr2sEQ==
dependencies:
debug "^4.1.1"
module-details-from-path "^1.0.3"
resolve "^1.12.0"
require-like@^0.1.2: require-like@^0.1.2:
version "0.1.2" version "0.1.2"
resolved "https://registry.yarnpkg.com/require-like/-/require-like-0.1.2.tgz#ad6f30c13becd797010c468afa775c0c0a6b47fa" resolved "https://registry.yarnpkg.com/require-like/-/require-like-0.1.2.tgz#ad6f30c13becd797010c468afa775c0c0a6b47fa"
...@@ -14614,11 +14516,6 @@ shellwords@^0.1.1: ...@@ -14614,11 +14516,6 @@ shellwords@^0.1.1:
version "0.1.1" version "0.1.1"
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
shimmer@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/shimmer/-/shimmer-1.2.1.tgz#610859f7de327b587efebf501fb43117f9aff337"
integrity sha512-sQTKC1Re/rM6XyFM6fIAGHRPVGvyXfgzIDvzoq608vM+jeyVD0Tu1E6Np0Kc2zAIFWIj963V2800iF/9LPieQw==
side-channel@^1.0.2, side-channel@^1.0.3: side-channel@^1.0.2, side-channel@^1.0.3:
version "1.0.4" version "1.0.4"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf" resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"
......
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