Unverified Commit 6ee3d4bc authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Trace upload: only send traces for current session (#71838)

The `.next/trace` file contains trace events spanning multiple traces
from different sessions. This changes our upload process to only send
trace events for the current session that’s ending.

Before this change, we would send a single set of metadata for many
traces, but these older session traces could have been invoked with
different parameters like `mode`, `isTurboSession`, etc. Because of
this, we started including `isTurboSession` as a span attribute on
spans, but this value was dependent on an environment variable that
wasn’t always set. In this case, the top-level `next-dev` span never had
the correct value.

This makes it so the metadata, now always determined at upload time,
always reflects the session sent, and never any others.

Other bugs fixed:

- Trace events within a single logical session had different trace ids
because of worker threads. This aligns them.
- Because we upload traces out-of-process, the Telemetry `sessionId` did
not reflect the real session id of other analytics sent. This passes it
along to the external process.

Test Plan:

In a test app, using `NEXT_TRACE_UPLOAD_DEBUG=1` and a running Otel
server:
- Start a non-turbopack dev session and end it. Verify trace events are
sent with `isTurboSession: false` and note the trace id.
- Start a turbopack dev session and end it. Verify trace events are sent
with `isTurboSession: true` and note events are sent for this trace, and
not the previous one.
- Start a build and verify trace events are sent with `mode: build` and
`isTurboSession: false` and that no older trace events are sent.


Closes PACK-3338
parent f9b281a8
...@@ -3865,6 +3865,7 @@ export default async function build( ...@@ -3865,6 +3865,7 @@ export default async function build(
mode: 'build', mode: 'build',
projectDir: dir, projectDir: dir,
distDir: loadedConfig.distDir, distDir: loadedConfig.distDir,
isTurboSession: turboNextBuild,
sync: true, sync: true,
}) })
} }
......
...@@ -37,6 +37,7 @@ import os from 'os' ...@@ -37,6 +37,7 @@ import os from 'os'
import { once } from 'node:events' import { once } from 'node:events'
import { clearTimeout } from 'timers' import { clearTimeout } from 'timers'
import { flushAllTraces, trace } from '../trace' import { flushAllTraces, trace } from '../trace'
import { traceId } from '../trace/shared'
export type NextDevOptions = { export type NextDevOptions = {
turbo?: boolean turbo?: boolean
...@@ -137,6 +138,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -137,6 +138,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
mode: 'dev', mode: 'dev',
projectDir: dir, projectDir: dir,
distDir: config.distDir, distDir: config.distDir,
isTurboSession,
}) })
} }
...@@ -277,6 +279,7 @@ const nextDev = async ( ...@@ -277,6 +279,7 @@ const nextDev = async (
...defaultEnv, ...defaultEnv,
TURBOPACK: process.env.TURBOPACK, TURBOPACK: process.env.TURBOPACK,
NEXT_PRIVATE_WORKER: '1', NEXT_PRIVATE_WORKER: '1',
NEXT_PRIVATE_TRACE_ID: traceId,
NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate
? startServerOptions.selfSignedCertificate.rootCA ? startServerOptions.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS, : defaultEnv.NODE_EXTRA_CA_CERTS,
...@@ -309,6 +312,7 @@ const nextDev = async ( ...@@ -309,6 +312,7 @@ const nextDev = async (
mode: 'dev', mode: 'dev',
projectDir: dir, projectDir: dir,
distDir: config.distDir, distDir: config.distDir,
isTurboSession,
sync: true, sync: true,
}) })
} }
......
import { randomBytes } from 'crypto' import { traceGlobals, traceId } from '../shared'
import { traceGlobals } from '../shared'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants' import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants'
...@@ -44,7 +43,6 @@ export function batcher(reportEvents: (evts: Event[]) => Promise<void>) { ...@@ -44,7 +43,6 @@ export function batcher(reportEvents: (evts: Event[]) => Promise<void>) {
} }
let writeStream: RotatingWriteStream let writeStream: RotatingWriteStream
let traceId: string
let batch: ReturnType<typeof batcher> | undefined let batch: ReturnType<typeof batcher> | undefined
const writeStreamOptions = { const writeStreamOptions = {
...@@ -117,10 +115,6 @@ const reportToLocalHost = (event: TraceEvent) => { ...@@ -117,10 +115,6 @@ const reportToLocalHost = (event: TraceEvent) => {
return return
} }
if (!traceId) {
traceId = process.env.TRACE_ID || randomBytes(8).toString('hex')
}
if (!batch) { if (!batch) {
batch = batcher(async (events: Event[]) => { batch = batcher(async (events: Event[]) => {
if (!writeStream) { if (!writeStream) {
......
import { randomBytes } from 'node:crypto'
let _traceGlobals: Map<any, any> = (global as any)._traceGlobals let _traceGlobals: Map<any, any> = (global as any)._traceGlobals
if (!_traceGlobals) { if (!_traceGlobals) {
...@@ -9,3 +11,8 @@ export const traceGlobals: Map<any, any> = _traceGlobals ...@@ -9,3 +11,8 @@ export const traceGlobals: Map<any, any> = _traceGlobals
export const setGlobal = (key: any, val: any) => { export const setGlobal = (key: any, val: any) => {
traceGlobals.set(key, val) traceGlobals.set(key, val)
} }
export const traceId =
process.env.TRACE_ID ||
process.env.NEXT_PRIVATE_TRACE_ID ||
randomBytes(8).toString('hex')
...@@ -7,7 +7,6 @@ import os from 'os' ...@@ -7,7 +7,6 @@ import os from 'os'
import { createInterface } from 'readline' import { createInterface } from 'readline'
import { createReadStream } from 'fs' import { createReadStream } from 'fs'
import path from 'path' import path from 'path'
import { Telemetry } from '../telemetry/storage'
const COMMON_ALLOWED_EVENTS = ['memory-usage'] const COMMON_ALLOWED_EVENTS = ['memory-usage']
...@@ -66,7 +65,19 @@ const { ...@@ -66,7 +65,19 @@ const {
const isDebugEnabled = !!NEXT_TRACE_UPLOAD_DEBUG || !!NEXT_TRACE_UPLOAD_FULL const isDebugEnabled = !!NEXT_TRACE_UPLOAD_DEBUG || !!NEXT_TRACE_UPLOAD_FULL
const shouldUploadFullTrace = !!NEXT_TRACE_UPLOAD_FULL const shouldUploadFullTrace = !!NEXT_TRACE_UPLOAD_FULL
const [, , traceUploadUrl, mode, projectDir, distDir] = process.argv const [
,
,
traceUploadUrl,
mode,
projectDir,
distDir,
_isTurboSession,
traceId,
anonymousId,
sessionId,
] = process.argv
const isTurboSession = _isTurboSession === 'true'
type TraceRequestBody = { type TraceRequestBody = {
metadata: TraceMetadata metadata: TraceMetadata
...@@ -105,8 +116,6 @@ interface TraceMetadata { ...@@ -105,8 +116,6 @@ interface TraceMetadata {
) )
).version ).version
const telemetry = new Telemetry({ distDir })
const projectPkgJsonPath = await findUp('package.json') const projectPkgJsonPath = await findUp('package.json')
assert(projectPkgJsonPath) assert(projectPkgJsonPath)
...@@ -129,11 +138,15 @@ interface TraceMetadata { ...@@ -129,11 +138,15 @@ interface TraceMetadata {
crlfDelay: Infinity, crlfDelay: Infinity,
}) })
let isTurboSession = false const sessionTrace = []
const traces = new Map<string, TraceEvent[]>()
for await (const line of readLineInterface) { for await (const line of readLineInterface) {
const lineEvents: TraceEvent[] = JSON.parse(line) const lineEvents: TraceEvent[] = JSON.parse(line)
for (const event of lineEvents) { for (const event of lineEvents) {
if (event.traceId !== traceId) {
// Only consider events for the current session
continue
}
if ( if (
// Always include root spans // Always include root spans
event.parentId === undefined || event.parentId === undefined ||
...@@ -142,22 +155,14 @@ interface TraceMetadata { ...@@ -142,22 +155,14 @@ interface TraceMetadata {
? DEV_ALLOWED_EVENTS.has(event.name) ? DEV_ALLOWED_EVENTS.has(event.name)
: BUILD_ALLOWED_EVENTS.has(event.name)) : BUILD_ALLOWED_EVENTS.has(event.name))
) { ) {
let trace = traces.get(event.traceId) sessionTrace.push(event)
if (trace === undefined) {
trace = []
traces.set(event.traceId, trace)
}
if (typeof event.tags.isTurbopack === 'boolean') {
isTurboSession = event.tags.isTurbopack
}
trace.push(event)
} }
} }
} }
const body: TraceRequestBody = { const body: TraceRequestBody = {
metadata: { metadata: {
anonymousId: telemetry.anonymousId, anonymousId,
arch: os.arch(), arch: os.arch(),
commit, commit,
cpus: os.cpus().length, cpus: os.cpus().length,
...@@ -166,9 +171,12 @@ interface TraceMetadata { ...@@ -166,9 +171,12 @@ interface TraceMetadata {
nextVersion, nextVersion,
pkgName, pkgName,
platform: os.platform(), platform: os.platform(),
sessionId: telemetry.sessionId, sessionId,
}, },
traces: [...traces.values()], // The trace file can contain events spanning multiple sessions.
// Only submit traces for the current session, as the metadata we send is
// intended for this session only.
traces: [sessionTrace],
} }
if (isDebugEnabled) { if (isDebugEnabled) {
......
...@@ -52,10 +52,6 @@ export class Span { ...@@ -52,10 +52,6 @@ export class Span {
this.name = name this.name = name
this.parentId = parentId ?? defaultParentSpanId this.parentId = parentId ?? defaultParentSpanId
this.attrs = attrs ? { ...attrs } : {} this.attrs = attrs ? { ...attrs } : {}
if (this.parentId === undefined) {
// Attach additional information to root spans
this.attrs.isTurbopack = Boolean(process.env.TURBOPACK)
}
this.status = SpanStatus.Started this.status = SpanStatus.Started
this.id = getId() this.id = getId()
......
import { traceId } from './shared'
import { Telemetry } from '../telemetry/storage'
export default function uploadTrace({ export default function uploadTrace({
traceUploadUrl, traceUploadUrl,
mode, mode,
projectDir, projectDir,
distDir, distDir,
isTurboSession,
sync, sync,
}: { }: {
traceUploadUrl: string traceUploadUrl: string
mode: 'dev' | 'build' mode: 'dev' | 'build'
projectDir: string projectDir: string
distDir: string distDir: string
isTurboSession: boolean
sync?: boolean sync?: boolean
}) { }) {
const { NEXT_TRACE_UPLOAD_DEBUG } = process.env const { NEXT_TRACE_UPLOAD_DEBUG } = process.env
const telemetry = new Telemetry({ distDir })
// Note: cross-spawn is not used here as it causes // Note: cross-spawn is not used here as it causes
// a new command window to appear when we don't want it to // a new command window to appear when we don't want it to
...@@ -33,6 +39,10 @@ export default function uploadTrace({ ...@@ -33,6 +39,10 @@ export default function uploadTrace({
mode, mode,
projectDir, projectDir,
distDir, distDir,
String(isTurboSession),
traceId,
telemetry.anonymousId,
telemetry.sessionId,
], ],
{ {
detached: !NEXT_TRACE_UPLOAD_DEBUG, detached: !NEXT_TRACE_UPLOAD_DEBUG,
......
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