Skip to content
Permalink
Browse files

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](235cedc).  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
  • Loading branch information
divmain committed Mar 10, 2021
1 parent fa02b19 commit e27b7e996d6e7e6772272f3d189ce93dac25fc3d
Showing with 1,534 additions and 1,579 deletions.
  1. +0 −39 bench/instrument.js
  2. +0 −7 package.json
  3. +1 −13 packages/next/bin/next.ts
  4. +196 −209 packages/next/build/index.ts
  5. +0 −100 packages/next/build/tracer.ts
  6. +110 −128 packages/next/build/utils.ts
  7. +11 −15 packages/next/build/webpack/loaders/babel-loader/src/cache.js
  8. +125 −133 packages/next/build/webpack/loaders/babel-loader/src/index.js
  9. +19 −19 packages/next/build/webpack/loaders/next-client-pages-loader.ts
  10. +3 −3 packages/next/build/webpack/loaders/next-serverless-loader/index.ts
  11. +127 −126 packages/next/build/webpack/plugins/build-manifest-plugin.ts
  12. +37 −38 packages/next/build/webpack/plugins/build-stats-plugin.ts
  13. +69 −76 packages/next/build/webpack/plugins/css-minimizer-plugin.ts
  14. +11 −31 packages/next/build/webpack/plugins/profiling-plugin.ts
  15. +150 −157 packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js
  16. +44 −45 packages/next/export/index.ts
  17. +325 −336 packages/next/export/worker.ts
  18. +3 −0 packages/next/server/next-dev-server.ts
  19. +71 −0 packages/next/telemetry/trace/autoparent.ts
  20. +5 −0 packages/next/telemetry/trace/index.ts
  21. +33 −0 packages/next/telemetry/trace/report/index.ts
  22. +26 −0 packages/next/telemetry/trace/report/to-console.ts
  23. +27 −0 packages/next/telemetry/trace/report/to-telemetry.ts
  24. +45 −0 packages/next/telemetry/trace/report/to-zipkin.ts
  25. +12 −0 packages/next/telemetry/trace/shared.ts
  26. +83 −0 packages/next/telemetry/trace/trace.ts
  27. +1 −104 yarn.lock

This file was deleted.

@@ -34,8 +34,6 @@
"publish-stable": "lerna version --force-publish",
"lint-staged": "lint-staged",
"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"
},
"pre-commit": "lint-staged",
@@ -45,11 +43,6 @@
"@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",
"@testing-library/react": "11.2.5",
"@types/cheerio": "0.22.16",
"@types/fs-extra": "8.1.0",
@@ -2,7 +2,6 @@
import * as log from '../build/output/log'
import arg from 'next/dist/compiled/arg/index.js'
import { NON_STANDARD_NODE_ENV } from '../lib/constants'
import opentelemetryApi from '@opentelemetry/api'
;['react', 'react-dom'].forEach((dependency) => {
try {
// When 'npm link' is used it checks the clone location. Not the project.
@@ -107,18 +106,7 @@ if (typeof React.Suspense === 'undefined') {
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))

commands[command]()
.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)
}
})
commands[command]().then((exec) => exec(forwardedArgs))

if (command === 'dev') {
const { CONFIG_FILE } = require('../next-server/lib/constants')

0 comments on commit e27b7e9

Please sign in to comment.