Unverified Commit 96dd8ff4 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Tracing: add opt-in flag to send a subset of development traces to url (#53880)

This adds an **opt-in** feature to send a subset of trace spans and metadata to a remote http server. The url must be provided as an option like so:

`next dev --experimental-upload-trace=http://localhost:3000/traces`

These traces are derived from the tracing data stored at `.next/trace`. The request payload looks like this:

```json
{
  "metadata": {
    "commit": "69b0a3366734182f4efce6c0089c2592044d7124",
    "mode": "dev",
    "pkgName": "nextjs-project",
    "arch": "arm64",
    "cpus": 10,
    "platform": "darwin"
  },
  "traces": [
    [
      {
        "traceId": "2f041a20b06027ab",
        "name": "hot-reloader",
        "id": 1,
        "timestamp": 28712742599,
        "duration": 21,
        "tags": {
          "version": "13.4.14-canary.1"
        },
        "startTime": 1691707223226
      },
      {
        "traceId": "2f041a20b06027ab",
        "parentId": 1,
        "name": "webpack-invalidated-server",
        "id": 63,
        "timestamp": 28713776446,
        "duration": 244628,
        "tags": {
          "trigger": "manual"
        },
        "startTime": 1691707224260
      },
      {
        "traceId": "2f041a20b06027ab",
        "parentId": 1,
        "name": "webpack-invalidated-server",
        "id": 111,
        "timestamp": 28714080210,
        "duration": 115267,
        "tags": {
          "trigger": "manual"
        },
        "startTime": 1691707224563
      },
      {
        "traceId": "2f041a20b06027ab",
        "parentId": 1,
        "name": "webpack-invalidated-server",
        "id": 168,
        "timestamp": 28715243617,
        "duration": 69471,
        "tags": {
          "trigger": "[project]/pages/index.tsx"
        },
        "startTime": 1691707225727
      },
      {
        "traceId": "2f041a20b06027ab",
        "parentId": 1,
        "name": "client-hmr-latency",
        "id": 222,
        "timestamp": 1691707225727000,
        "duration": 72000,
        "tags": {
          "clientId": 1691707224946
        },
        "startTime": 1691707225799
      },
      {
        "traceId": "2f041a20b06027ab",
        "parentId": 1,
        "name": "webpack-invalidated-server",
        "id": 223,
        "timestamp": 28716999898,
        "duration": 47425,
        "tags": {
          "trigger": "[project]/pages/index.tsx"
        },
        "startTime": 1691707227483
      }
    ]
  ]
}
```

Currently only events of type `client-hmr-latency`, `hot-reloader`, `webpack-invalid-client`, `webpack-invalidated-server` are included, as well as all root spans. Traces are expected to include the ancestry of any given span through the `parentId` property.
parent 0b3e366f
......@@ -25,10 +25,12 @@ import { getValidatedArgs } from '../lib/get-validated-args'
import { Worker } from 'next/dist/compiled/jest-worker'
import type { ChildProcess } from 'child_process'
import { checkIsNodeDebugging } from '../server/lib/is-node-debugging'
import uploadTrace from '../trace/upload-trace'
let dir: string
let config: NextConfigComplete
let isTurboSession = false
let traceUploadUrl: string
let sessionStopHandled = false
let sessionStarted = Date.now()
......@@ -86,6 +88,16 @@ const handleSessionStop = async () => {
// noise to the output
}
if (traceUploadUrl) {
uploadTrace({
traceUploadUrl,
mode: 'dev',
isTurboSession,
projectDir: dir,
distDir: config.distDir,
})
}
// ensure we re-enable the terminal cursor before exiting
// the program, or the cursor could remain hidden
process.stdout.write('\x1B[?25h')
......@@ -190,6 +202,7 @@ const nextDev: CliCommand = async (argv) => {
'--turbo': Boolean,
'--experimental-turbo': Boolean,
'--experimental-test-proxy': Boolean,
'--experimental-upload-trace': String,
// To align current messages with native binary.
// Will need to adjust subcommand later.
......@@ -217,6 +230,7 @@ const nextDev: CliCommand = async (argv) => {
Options
--port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application (default: 0.0.0.0)
--experimental-upload-trace=<trace-url> [EXPERIMENTAL] Report a subset of the debugging trace to a remote http url. Includes sensitive data. Disabled by default and url must be provided.
--help, -h Displays this message
`)
process.exit(0)
......@@ -277,6 +291,10 @@ const nextDev: CliCommand = async (argv) => {
config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir)
const isExperimentalTestProxy = args['--experimental-test-proxy']
if (args['--experimental-upload-trace']) {
traceUploadUrl = args['--experimental-upload-trace']
}
const devServerOptions: StartServerOptions = {
dir,
port,
......
import findUp from 'next/dist/compiled/find-up'
import fsPromise from 'fs/promises'
import child_process from 'child_process'
import assert from 'assert'
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
import os from 'os'
import { createInterface } from 'readline'
import { createReadStream } from 'fs'
import path from 'path'
const EVENT_FILTER = new Set([
'client-hmr-latency',
'hot-reloader',
'webpack-invalid-client',
'webpack-invalidated-server',
])
const { NEXT_TRACE_UPLOAD_DEBUG } = process.env
const [, , traceUploadUrl, mode, _isTurboSession, projectDir, distDir] =
process.argv
const isTurboSession = _isTurboSession === 'true'
type TraceRequestBody = {
metadata: TraceMetadata
traces: TraceEvent[][]
}
interface TraceEvent {
traceId: string
parentId?: number
name: string
id: number
startTime: number
timestamp: number
duration: number
tags: Record<string, unknown>
}
interface TraceMetadata {
arch: string
commit: string
cpus: number
mode: string
pkgName: string
platform: string
isTurboSession: boolean
}
;(async function upload() {
const projectPkgJsonPath = await findUp('package.json')
assert(projectPkgJsonPath)
const projectPkgJson = JSON.parse(
await fsPromise.readFile(projectPkgJsonPath, 'utf-8')
)
const pkgName = projectPkgJson.name
const commit = child_process
.spawnSync(
os.platform() === 'win32' ? 'git.cmd' : 'git',
['rev-parse', 'HEAD'],
{ shell: true }
)
.stdout.toString()
.trimEnd()
const readLineInterface = createInterface({
input: createReadStream(path.join(projectDir, distDir, 'trace')),
crlfDelay: Infinity,
})
const traces = new Map<string, TraceEvent[]>()
for await (const line of readLineInterface) {
const lineEvents: TraceEvent[] = JSON.parse(line)
for (const event of lineEvents) {
if (
// Always include root spans
event.parentId === undefined ||
EVENT_FILTER.has(event.name)
) {
if (
typeof event.tags.trigger === 'string' &&
path.isAbsolute(event.tags.trigger)
) {
event.tags.trigger =
'[project]/' +
path
.relative(projectDir, event.tags.trigger)
.replaceAll(path.sep, '/')
}
let trace = traces.get(event.traceId)
if (trace === undefined) {
trace = []
traces.set(event.traceId, trace)
}
trace.push(event)
}
}
}
const body: TraceRequestBody = {
metadata: {
commit,
mode,
pkgName,
isTurboSession,
arch: os.arch(),
cpus: os.cpus().length,
platform: os.platform(),
},
traces: [...traces.values()],
}
if (NEXT_TRACE_UPLOAD_DEBUG) {
console.log('Sending request with body', JSON.stringify(body, null, 2))
}
let res = await fetch(traceUploadUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
if (NEXT_TRACE_UPLOAD_DEBUG) {
console.log('Received response', res.status, await res.json())
}
})()
export default function uploadTrace({
traceUploadUrl,
mode,
isTurboSession,
projectDir,
distDir,
}: {
traceUploadUrl: string
mode: 'dev'
isTurboSession: boolean
projectDir: string
distDir: string
}) {
const { NEXT_TRACE_UPLOAD_DEBUG } = process.env
// Note: cross-spawn is not used here as it causes
// a new command window to appear when we don't want it to
const child_process =
require('child_process') as typeof import('child_process')
// we use spawnSync when debugging to ensure logs are piped
// correctly to stdout/stderr
const spawn = NEXT_TRACE_UPLOAD_DEBUG
? child_process.spawnSync
: child_process.spawn
spawn(
process.execPath,
[
require.resolve('./trace-uploader'),
traceUploadUrl,
mode,
String(isTurboSession),
projectDir,
distDir,
],
{
detached: !NEXT_TRACE_UPLOAD_DEBUG,
windowsHide: true,
shell: false,
...(NEXT_TRACE_UPLOAD_DEBUG
? {
stdio: 'inherit',
}
: {}),
}
)
}
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