Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade opentelemetry to v0.17 #22570

Closed
wants to merge 10 commits into from
48 changes: 28 additions & 20 deletions bench/instrument.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
// Disable automatic instrumentation
process.env.OTEL_NO_PATCH_MODULES = '*'

const { registerInstrumentations } = require('@opentelemetry/instrumentation')
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',
const tracerProvider = new NodeTracerProvider({})
registerInstrumentations({
instrumentations: [
{
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' },
},
},
koa: { enabled: false, path: '@opentelemetry/koa-instrumentation' },
dns: { enabled: false, path: '@opentelemetry/plugin-dns' },
},
],
})

tracerProvider.addSpanProcessor(
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
"@babel/preset-react": "7.12.10",
"@fullhuman/postcss-purgecss": "1.3.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",
"@opentelemetry/exporter-zipkin": "0.17.0",
"@opentelemetry/node": "0.17.0",
"@opentelemetry/plugin-http": "0.17.0",
"@opentelemetry/plugin-https": "0.17.0",
"@opentelemetry/tracing": "0.17.0",
"@testing-library/react": "11.2.5",
"@types/cheerio": "0.22.16",
"@types/fs-extra": "8.1.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/next/build/tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api, { Span } from '@opentelemetry/api'
import api, { context, setSpan, Span } from '@opentelemetry/api'

export const tracer = api.trace.getTracer('next', process.env.__NEXT_VERSION)

Expand All @@ -15,7 +15,7 @@ export function stackPush(compiler: any, spanName: string, attrs?: any): any {
} else {
const parent = stack[stack.length - 1]
if (parent) {
tracer.withSpan(parent, () => {
context.with(parent, () => {
span = tracer.startSpan(spanName, attrs ? attrs() : undefined)
})
} else {
Expand Down Expand Up @@ -77,7 +77,7 @@ export function traceFn<T extends (...args: unknown[]) => ReturnType<T>>(
span: Span,
fn: T
): ReturnType<T> {
return tracer.withSpan(span, () => {
return context.with(setSpan(context.active(), span), () => {
try {
return fn()
} finally {
Expand All @@ -90,7 +90,7 @@ export function traceAsyncFn<T extends (...args: unknown[]) => ReturnType<T>>(
span: Span,
fn: T
): Promise<ReturnType<T>> {
return tracer.withSpan(span, async () => {
return context.with(setSpan(context.active(), span), async () => {
try {
return await fn()
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { context, setSpan } from '@opentelemetry/api'
import loaderUtils from 'next/dist/compiled/loader-utils'
import { tracer, traceFn } from '../../tracer'

Expand All @@ -8,7 +9,7 @@ export type ClientPagesLoaderOptions = {

// this parameter: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters
function nextClientPagesLoader(this: any) {
return tracer.withSpan(this.currentTraceSpan, () => {
return context.with(setSpan(context.active(), this.currentTraceSpan), () => {
const span = tracer.startSpan('next-client-pages-loader')
return traceFn(span, () => {
const { absolutePagePath, page } = loaderUtils.getOptions(
Expand Down
5 changes: 3 additions & 2 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Rewrite } from '../../../lib/load-custom-routes'
import { getSortedRoutes } from '../../../next-server/lib/router/utils'
import { tracer, traceFn } from '../../tracer'
import { spans } from './profiling-plugin'
import { context, setSpan } from '@opentelemetry/api'

type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }

Expand All @@ -31,7 +32,7 @@ function generateClientManifest(
assetMap: BuildManifest,
rewrites: Rewrite[]
): string {
return tracer.withSpan(spans.get(compiler), () => {
return context.with(setSpan(context.active(), spans.get(compiler)), () => {
const span = tracer.startSpan('NextJsBuildManifest-generateClientManifest')
return traceFn(span, () => {
const clientManifest: ClientBuildManifest = {
Expand Down Expand Up @@ -103,7 +104,7 @@ export default class BuildManifestPlugin {
}

createAssets(compiler: any, compilation: any, assets: any) {
return tracer.withSpan(spans.get(compiler), () => {
return context.with(setSpan(context.active(), spans.get(compiler)), () => {
const span = tracer.startSpan('NextJsBuildManifest-createassets')
return traceFn(span, () => {
const namedChunks: Map<string, webpack.compilation.Chunk> =
Expand Down
76 changes: 40 additions & 36 deletions packages/next/build/webpack/plugins/build-stats-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import bfj from 'next/dist/compiled/bfj'
import { spans } from './profiling-plugin'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { tracer, traceAsyncFn } from '../../tracer'
import { context, setSpan } from '@opentelemetry/api'

const STATS_VERSION = 0

Expand Down Expand Up @@ -75,46 +76,49 @@ export default class BuildStatsPlugin {
compiler.hooks.done.tapAsync(
'NextJsBuildStats',
async (stats, callback) => {
tracer.withSpan(spans.get(compiler), async () => {
try {
const writeStatsSpan = tracer.startSpan('NextJsBuildStats')
await traceAsyncFn(writeStatsSpan, () => {
return new Promise((resolve, reject) => {
const statsJson = reduceSize(
stats.toJson({
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
modules: true,
// @ts-ignore this option exists
ids: true,
context.with(
setSpan(context.active(), spans.get(compiler)),
async () => {
try {
const writeStatsSpan = tracer.startSpan('NextJsBuildStats')
await traceAsyncFn(writeStatsSpan, () => {
return new Promise((resolve, reject) => {
const statsJson = reduceSize(
stats.toJson({
all: false,
cached: true,
reasons: true,
entrypoints: true,
chunks: true,
errors: false,
warnings: false,
maxModules: Infinity,
chunkModules: true,
modules: true,
// @ts-ignore this option exists
ids: true,
})
)
const fileStream = fs.createWriteStream(
path.join(this.distDir, 'next-stats.json')
)
const jsonStream = bfj.streamify({
version: STATS_VERSION,
stats: statsJson,
})
)
const fileStream = fs.createWriteStream(
path.join(this.distDir, 'next-stats.json')
)
const jsonStream = bfj.streamify({
version: STATS_VERSION,
stats: statsJson,
jsonStream.pipe(fileStream)
jsonStream.on('error', reject)
fileStream.on('error', reject)
jsonStream.on('dataError', reject)
fileStream.on('close', resolve)
})
jsonStream.pipe(fileStream)
jsonStream.on('error', reject)
fileStream.on('error', reject)
jsonStream.on('dataError', reject)
fileStream.on('close', resolve)
})
})
callback()
} catch (err) {
callback(err)
callback()
} catch (err) {
callback(err)
}
}
})
)
}
)
}
Expand Down
Loading