Radar · Go · Archive · Week 5 · Jan 27 – Feb 2, 2025
runtime/trace: flight recording
Worth knowingruntime
- What changed
- The
tracepackage now includes aFlightRecordertype that can record a moving window of execution trace data. Only one flight recorder may be active at a time, and it can run concurrently with a normal trace consumer. - Production impact
- The source does not say.
- Try it
- Create a
trace.NewFlightRecorderand callStart()to begin recording. - Source
- github.com/golang/go/issues/63185
Understand it, then run it
The Go trace package can record a program’s execution as a stream of events. Until now you could start a trace, let it run, and then write the collected data to a file. A new feature, called *flight recording*, adds a FlightRecorder type that keeps a moving window of the most recent trace data in memory. When something interesting happens you can ask the recorder to dump that recent window to a file, without having to restart the program or replay a long trace.
Run it now
// This program demonstrates the current trace API. The new FlightRecorder
// type is not yet available in Go 1.27.1, so we use the standard trace.Start
// and trace.Stop functions instead.
package main
import (
"fmt"
"os"
"time"
"runtime/trace"
)
func main() {
// Open a file to write the trace data.
f, err := os.Create("trace.out")
if err != nil {
fmt.Println("could not create trace file:", err)
return
}
defer f.Close()
// Start tracing. The trace will capture all events until Stop is called.
if err := trace.Start(f); err != nil {
fmt.Println("could not start trace:", err)
return
}
defer trace.Stop()
// Simulate some work.
time.Sleep(2 * time.Second)
// The trace data is written to trace.out when Stop is called.
fmt.Println("trace written to trace.out")
}
What it printed when we ran it on Go 1.27.1
trace written to trace.out
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed