Skip to content

Commit

Permalink
ci(trace): allow to opt in to upload full trace (#54347)
Browse files Browse the repository at this point in the history
### What?
 
Came to realize it might be useful to upload full trace. This is avoided by default as trace might grow excessively, however might be useful to audit individual trace if it can be uploaded.

Closes WEB-1416
  • Loading branch information
kwonoj committed Aug 21, 2023
1 parent 3370022 commit 36c14da
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/next/src/trace/trace-uploader.ts
Expand Up @@ -9,14 +9,26 @@ import { createInterface } from 'readline'
import { createReadStream } from 'fs'
import path from 'path'

// Predefined set of the event names to be included in the trace.
// If the trace span's name matches to one of the event names in the set,
// it'll up uploaded to the trace server.
const EVENT_FILTER = new Set([
'client-hmr-latency',
'hot-reloader',
'webpack-invalid-client',
'webpack-invalidated-server',
])

const { NEXT_TRACE_UPLOAD_DEBUG } = process.env
const {
NEXT_TRACE_UPLOAD_DEBUG,
// An external env to allow to upload full trace without picking up the relavant spans.
// This is mainly for the debugging purpose, to allwo manual audit for full trace for the given build.
// [NOTE] This may fail if build is large and generated trace is excessively large.
NEXT_TRACE_UPLOAD_FULL,
} = process.env

const isDebugEnabled = !!NEXT_TRACE_UPLOAD_DEBUG || !!NEXT_TRACE_UPLOAD_FULL
const shouldUploadFullTrace = !!NEXT_TRACE_UPLOAD_FULL

const [, , traceUploadUrl, mode, _isTurboSession, projectDir, distDir] =
process.argv
Expand Down Expand Up @@ -78,6 +90,7 @@ interface TraceMetadata {
if (
// Always include root spans
event.parentId === undefined ||
shouldUploadFullTrace ||
EVENT_FILTER.has(event.name)
) {
if (
Expand Down Expand Up @@ -114,19 +127,20 @@ interface TraceMetadata {
traces: [...traces.values()],
}

if (NEXT_TRACE_UPLOAD_DEBUG) {
if (isDebugEnabled) {
console.log('Sending request with body', JSON.stringify(body, null, 2))
}

let res = await fetch(traceUploadUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-trace-transfer-mode': shouldUploadFullTrace ? 'full' : 'default',
},
body: JSON.stringify(body),
})

if (NEXT_TRACE_UPLOAD_DEBUG) {
if (isDebugEnabled) {
console.log('Received response', res.status, await res.json())
}
})()

0 comments on commit 36c14da

Please sign in to comment.