Unverified Commit f74ee78c authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add trace file for "next dev" (#29633)

Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 7af61f69
...@@ -132,6 +132,7 @@ export default async function build( ...@@ -132,6 +132,7 @@ export default async function build(
.traceChild('load-next-config') .traceChild('load-next-config')
.traceAsyncFn(() => loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)) .traceAsyncFn(() => loadConfig(PHASE_PRODUCTION_BUILD, dir, conf))
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir) setGlobal('distDir', distDir)
const { target } = config const { target } = config
......
...@@ -286,6 +286,8 @@ export default class DevServer extends Server { ...@@ -286,6 +286,8 @@ export default class DevServer extends Server {
} }
async prepare(): Promise<void> { async prepare(): Promise<void> {
setGlobal('distDir', this.distDir)
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
await verifyTypeScriptSetup( await verifyTypeScriptSetup(
this.dir, this.dir,
this.pagesDir!, this.pagesDir!,
......
...@@ -3,11 +3,64 @@ import { batcher } from './to-zipkin' ...@@ -3,11 +3,64 @@ import { batcher } from './to-zipkin'
import { traceGlobals } 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'
let writeStream: fs.WriteStream let writeStream: RotatingWriteStream
let traceId: string let traceId: string
let batch: ReturnType<typeof batcher> | undefined let batch: ReturnType<typeof batcher> | undefined
const writeStreamOptions = {
flags: 'a',
encoding: 'utf8',
}
class RotatingWriteStream {
file: string
writeStream!: fs.WriteStream
size: number
sizeLimit: number
isRotating: Promise<void> | undefined
constructor(file: string, sizeLimit: number) {
this.file = file
this.size = 0
this.sizeLimit = sizeLimit
this.createWriteStream()
}
private createWriteStream() {
this.writeStream = fs.createWriteStream(this.file, writeStreamOptions)
}
// Recreate the file
private rotate(): void {
this.end()
try {
fs.unlinkSync(this.file)
} catch (err: any) {
// It's fine if the file does not exist yet
if (err.code !== 'ENOENT') {
throw err
}
}
this.size = 0
this.createWriteStream()
}
async write(data: string): Promise<void> {
this.size += data.length
if (this.size > this.sizeLimit) {
this.rotate()
}
if (!this.writeStream.write(data, 'utf8')) {
await new Promise<void>((resolve, _reject) => {
this.writeStream.once('drain', resolve)
})
}
}
end(): void {
this.writeStream.end('', 'utf8')
}
}
const reportToLocalHost = ( const reportToLocalHost = (
name: string, name: string,
duration: number, duration: number,
...@@ -17,7 +70,8 @@ const reportToLocalHost = ( ...@@ -17,7 +70,8 @@ const reportToLocalHost = (
attrs?: Object attrs?: Object
) => { ) => {
const distDir = traceGlobals.get('distDir') const distDir = traceGlobals.get('distDir')
if (!distDir) { const phase = traceGlobals.get('phase')
if (!distDir || !phase) {
return return
} }
...@@ -30,18 +84,15 @@ const reportToLocalHost = ( ...@@ -30,18 +84,15 @@ const reportToLocalHost = (
if (!writeStream) { if (!writeStream) {
await fs.promises.mkdir(distDir, { recursive: true }) await fs.promises.mkdir(distDir, { recursive: true })
const file = path.join(distDir, 'trace') const file = path.join(distDir, 'trace')
writeStream = fs.createWriteStream(file, { writeStream = new RotatingWriteStream(
flags: 'a', file,
encoding: 'utf8', // Development is limited to 50MB, production is unlimited
}) phase === PHASE_DEVELOPMENT_SERVER ? 52428800 : Infinity
)
} }
const eventsJson = JSON.stringify(events) const eventsJson = JSON.stringify(events)
try { try {
await new Promise<void>((resolve, reject) => { await writeStream.write(eventsJson + '\n')
writeStream.write(eventsJson + '\n', 'utf8', (err) => {
err ? reject(err) : resolve()
})
})
} catch (err) { } catch (err) {
console.log(err) console.log(err)
} }
...@@ -63,7 +114,11 @@ export default { ...@@ -63,7 +114,11 @@ export default {
flushAll: () => flushAll: () =>
batch batch
? batch.flushAll().then(() => { ? batch.flushAll().then(() => {
writeStream.end('', 'utf8') const phase = traceGlobals.get('phase')
// Only end writeStream when manually flushing in production
if (phase !== PHASE_DEVELOPMENT_SERVER) {
writeStream.end()
}
}) })
: undefined, : undefined,
report: reportToLocalHost, report: reportToLocalHost,
......
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