Unverified Commit 8b347b63 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Tracing: Always end write stream on shutdown in dev (#70811)

While we don’t want to end the write stream in other situations in dev,
we _do_ want to do this when shutting down the web server.

This previously resulted in some spans, including the top-level
`next-dev` span, not always being written to disk as the process would
exit before it was flushed.

Test Plan: Ensured `next-dev` span is written to trace on devserver
shutdown
parent 9e0ce4f6
...@@ -86,7 +86,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -86,7 +86,7 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
} }
sessionSpan.stop() sessionSpan.stop()
await flushAllTraces() await flushAllTraces({ end: true })
try { try {
const { eventCliSessionStopped } = const { eventCliSessionStopped } =
......
...@@ -10,8 +10,8 @@ class MultiReporter implements Reporter { ...@@ -10,8 +10,8 @@ class MultiReporter implements Reporter {
this.reporters = reporters this.reporters = reporters
} }
async flushAll() { async flushAll(opts?: { end: boolean }) {
await Promise.all(this.reporters.map((reporter) => reporter.flushAll())) await Promise.all(this.reporters.map((reporter) => reporter.flushAll(opts)))
} }
report(event: TraceEvent) { report(event: TraceEvent) {
......
...@@ -147,12 +147,12 @@ const reportToLocalHost = (event: TraceEvent) => { ...@@ -147,12 +147,12 @@ const reportToLocalHost = (event: TraceEvent) => {
} }
export default { export default {
flushAll: () => flushAll: (opts?: { end: boolean }) =>
batch batch
? batch.flushAll().then(() => { ? batch.flushAll().then(() => {
const phase = traceGlobals.get('phase') const phase = traceGlobals.get('phase')
// Only end writeStream when manually flushing in production // Only end writeStream when manually flushing in production
if (phase !== PHASE_DEVELOPMENT_SERVER) { if (opts?.end || phase !== PHASE_DEVELOPMENT_SERVER) {
return writeStream.end() return writeStream.end()
} }
}) })
......
import type { TraceEvent } from '../types' import type { TraceEvent } from '../types'
export type Reporter = { export type Reporter = {
flushAll: () => Promise<void> | void flushAll: (opts?: { end: boolean }) => Promise<void> | void
report: (event: TraceEvent) => void report: (event: TraceEvent) => void
} }
...@@ -159,7 +159,8 @@ export const trace = ( ...@@ -159,7 +159,8 @@ export const trace = (
return new Span({ name, parentId, attrs }) return new Span({ name, parentId, attrs })
} }
export const flushAllTraces = () => reporter.flushAll() export const flushAllTraces = (opts?: { end: boolean }) =>
reporter.flushAll(opts)
// This code supports workers by serializing the state of tracers when the // This code supports workers by serializing the state of tracers when the
// worker is initialized, and serializing the trace events from the worker back // worker is initialized, and serializing the trace events from the worker back
......
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