Unverified Commit 2c0aeeeb authored by mknichel's avatar mknichel Committed by GitHub

[trace] Reduce the size of .next/trace files (#65101)

- Remove `next-swc-*` and `read-resource` spans since they are high
volume and generally very fast
- Added `NEXT_TRACE_SPAN_THRESHOLD_MS` env var to only record trace
spans that take longer than a threshold.
parent 03c8780e
......@@ -72,7 +72,6 @@ const FORCE_TRANSPILE_CONDITIONS =
async function loaderTransform(
this: any,
parentTrace: any,
source?: string,
inputSourceMap?: any
) {
......@@ -168,17 +167,14 @@ async function loaderTransform(
this.mode === 'development'
}
const swcSpan = parentTrace.traceChild('next-swc-transform')
return swcSpan.traceAsyncFn(() =>
transform(source as any, programmaticOptions).then((output) => {
if (output.eliminatedPackages && this.eliminatedPackages) {
for (const pkg of JSON.parse(output.eliminatedPackages)) {
this.eliminatedPackages.add(pkg)
}
return transform(source as any, programmaticOptions).then((output) => {
if (output.eliminatedPackages && this.eliminatedPackages) {
for (const pkg of JSON.parse(output.eliminatedPackages)) {
this.eliminatedPackages.add(pkg)
}
return [output.code, output.map ? JSON.parse(output.map) : undefined]
})
)
}
return [output.code, output.map ? JSON.parse(output.map) : undefined]
})
}
const EXCLUDED_PATHS =
......@@ -204,11 +200,8 @@ export function pitch(this: any) {
isAbsolute(this.resourcePath) &&
!(await isWasm())
) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
this.addDependency(this.resourcePath)
return loaderSpan.traceAsyncFn(() =>
loaderTransform.call(this, loaderSpan)
)
return loaderTransform.call(this)
}
})().then((r) => {
if (r) return callback(null, ...r)
......@@ -221,20 +214,15 @@ export default function swcLoader(
inputSource: string,
inputSourceMap: any
) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
const callback = this.async()
loaderSpan
.traceAsyncFn(() =>
loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)
)
.then(
([transformedSource, outputSourceMap]: any) => {
callback(null, transformedSource, outputSourceMap || inputSourceMap)
},
(err: Error) => {
callback(err)
}
)
loaderTransform.call(this, inputSource, inputSourceMap).then(
([transformedSource, outputSourceMap]: any) => {
callback(null, transformedSource, outputSourceMap || inputSourceMap)
},
(err: Error) => {
callback(err)
}
)
}
// accept Buffers instead of strings
......
......@@ -203,10 +203,7 @@ export class ProfilingPlugin {
register(tapInfo: any) {
const fn = tapInfo.fn
tapInfo.fn = (loaderContext: any, callback: any) => {
const moduleSpan =
loaderContext.currentTraceSpan.traceChild(`read-resource`)
fn(loaderContext, (err: any, result: any) => {
moduleSpan.stop()
callback(err, result)
})
}
......
......@@ -12,6 +12,10 @@ let defaultParentSpanId: SpanId | undefined
let shouldSaveTraceEvents: boolean | undefined
let savedTraceEvents: TraceEvent[] = []
const RECORD_SPAN_THRESHOLD_MS = parseInt(
process.env.NEXT_TRACE_SPAN_THRESHOLD_MS ?? '-1'
)
// eslint typescript has a bug with TS enums
/* eslint-disable no-shadow */
export enum SpanStatus {
......@@ -90,9 +94,11 @@ export class Span {
tags: this.attrs,
startTime: this.now,
}
reporter.report(traceEvent)
if (shouldSaveTraceEvents) {
savedTraceEvents.push(traceEvent)
if (duration > RECORD_SPAN_THRESHOLD_MS * 1000) {
reporter.report(traceEvent)
if (shouldSaveTraceEvents) {
savedTraceEvents.push(traceEvent)
}
}
}
......
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