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 = ...@@ -72,7 +72,6 @@ const FORCE_TRANSPILE_CONDITIONS =
async function loaderTransform( async function loaderTransform(
this: any, this: any,
parentTrace: any,
source?: string, source?: string,
inputSourceMap?: any inputSourceMap?: any
) { ) {
...@@ -168,9 +167,7 @@ async function loaderTransform( ...@@ -168,9 +167,7 @@ async function loaderTransform(
this.mode === 'development' this.mode === 'development'
} }
const swcSpan = parentTrace.traceChild('next-swc-transform') return transform(source as any, programmaticOptions).then((output) => {
return swcSpan.traceAsyncFn(() =>
transform(source as any, programmaticOptions).then((output) => {
if (output.eliminatedPackages && this.eliminatedPackages) { if (output.eliminatedPackages && this.eliminatedPackages) {
for (const pkg of JSON.parse(output.eliminatedPackages)) { for (const pkg of JSON.parse(output.eliminatedPackages)) {
this.eliminatedPackages.add(pkg) this.eliminatedPackages.add(pkg)
...@@ -178,7 +175,6 @@ async function loaderTransform( ...@@ -178,7 +175,6 @@ async function loaderTransform(
} }
return [output.code, output.map ? JSON.parse(output.map) : undefined] return [output.code, output.map ? JSON.parse(output.map) : undefined]
}) })
)
} }
const EXCLUDED_PATHS = const EXCLUDED_PATHS =
...@@ -204,11 +200,8 @@ export function pitch(this: any) { ...@@ -204,11 +200,8 @@ export function pitch(this: any) {
isAbsolute(this.resourcePath) && isAbsolute(this.resourcePath) &&
!(await isWasm()) !(await isWasm())
) { ) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
this.addDependency(this.resourcePath) this.addDependency(this.resourcePath)
return loaderSpan.traceAsyncFn(() => return loaderTransform.call(this)
loaderTransform.call(this, loaderSpan)
)
} }
})().then((r) => { })().then((r) => {
if (r) return callback(null, ...r) if (r) return callback(null, ...r)
...@@ -221,13 +214,8 @@ export default function swcLoader( ...@@ -221,13 +214,8 @@ export default function swcLoader(
inputSource: string, inputSource: string,
inputSourceMap: any inputSourceMap: any
) { ) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
const callback = this.async() const callback = this.async()
loaderSpan loaderTransform.call(this, inputSource, inputSourceMap).then(
.traceAsyncFn(() =>
loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)
)
.then(
([transformedSource, outputSourceMap]: any) => { ([transformedSource, outputSourceMap]: any) => {
callback(null, transformedSource, outputSourceMap || inputSourceMap) callback(null, transformedSource, outputSourceMap || inputSourceMap)
}, },
......
...@@ -203,10 +203,7 @@ export class ProfilingPlugin { ...@@ -203,10 +203,7 @@ export class ProfilingPlugin {
register(tapInfo: any) { register(tapInfo: any) {
const fn = tapInfo.fn const fn = tapInfo.fn
tapInfo.fn = (loaderContext: any, callback: any) => { tapInfo.fn = (loaderContext: any, callback: any) => {
const moduleSpan =
loaderContext.currentTraceSpan.traceChild(`read-resource`)
fn(loaderContext, (err: any, result: any) => { fn(loaderContext, (err: any, result: any) => {
moduleSpan.stop()
callback(err, result) callback(err, result)
}) })
} }
......
...@@ -12,6 +12,10 @@ let defaultParentSpanId: SpanId | undefined ...@@ -12,6 +12,10 @@ let defaultParentSpanId: SpanId | undefined
let shouldSaveTraceEvents: boolean | undefined let shouldSaveTraceEvents: boolean | undefined
let savedTraceEvents: TraceEvent[] = [] 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 typescript has a bug with TS enums
/* eslint-disable no-shadow */ /* eslint-disable no-shadow */
export enum SpanStatus { export enum SpanStatus {
...@@ -90,11 +94,13 @@ export class Span { ...@@ -90,11 +94,13 @@ export class Span {
tags: this.attrs, tags: this.attrs,
startTime: this.now, startTime: this.now,
} }
if (duration > RECORD_SPAN_THRESHOLD_MS * 1000) {
reporter.report(traceEvent) reporter.report(traceEvent)
if (shouldSaveTraceEvents) { if (shouldSaveTraceEvents) {
savedTraceEvents.push(traceEvent) savedTraceEvents.push(traceEvent)
} }
} }
}
traceChild(name: string, attrs?: Attributes) { traceChild(name: string, attrs?: Attributes) {
return new Span({ name, parentId: this.id, attrs }) return new Span({ name, parentId: this.id, attrs })
......
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