Lolpro Lab
📖 Tutorial

How to Use the Go 1.25 Flight Recorder for Debugging Latency Issues

Last updated: 2026-05-01 12:24:21 Intermediate
Complete guide
Follow along with this comprehensive guide

Introduction

Go 1.25 introduces a powerful new diagnostic tool: the flight recorder. This feature builds on the enhanced execution traces introduced in 2024, allowing you to capture a rolling buffer of the last few seconds of your Go program's execution. The flight recorder is ideal for long-running web services where problems occur sporadically. When your program detects an error or latency spike, you can snapshot the buffer to analyze exactly what happened leading up to the issue—without needing to trace the entire runtime or set up complex sampling infrastructure.

How to Use the Go 1.25 Flight Recorder for Debugging Latency Issues
Source: blog.golang.org

What You Need

  • Go 1.25 or later installed on your system. You can download it from go.dev.
  • A Go application (preferably a long-running service or server) that you can modify to use the flight recorder.
  • Familiarity with the runtime/trace package.
  • Optional: A tool to analyze execution traces, such as go tool trace.

Step-by-Step Guide to Using the Flight Recorder

Step 1: Import the Required Packages

Add the runtime/trace package to your import block. This package contains the flight recorder API.

import "runtime/trace"

Step 2: Create a Flight Recorder Instance

Use trace.NewFlightRecorder to create a new flight recorder. You need to specify the buffer size (in seconds) that you want to keep in memory. For example, to keep the last 10 seconds:

recorder := trace.NewFlightRecorder(10 * time.Second)

Step 3: Start Recording

Call the Start method on the recorder. This will begin capturing execution traces into the circular buffer. You can start it at any point, typically early in your program's lifecycle, such as in main() or a server initialization function.

if err := recorder.Start(); err != nil {
    log.Fatalf("failed to start flight recorder: %v", err)
}

Step 4: Run Your Application Normally

Your application continues execution. The flight recorder continuously overwrites old trace data with new data, always keeping the most recent N seconds of activity.

Step 5: Capture a Snapshot When Something Goes Wrong

When your program detects an error condition (e.g., a request timeout, failed health check, or latency spike), call the Snapshot method. This returns the current buffer contents as a []byte in the same format as an execution trace. You can then write this data to disk or send it to your logging system.

buf, err := recorder.Snapshot()
if err != nil {
    log.Printf("failed to snapshot flight recorder: %v", err)
    return
}
// Save to file, send to a tracing service, etc.
os.WriteFile("trace.out", buf, 0644)

Step 6: Stop the Flight Recorder When Done

When your program shuts down or you no longer need tracing (e.g., during graceful shutdown), stop the recorder to free resources.

How to Use the Go 1.25 Flight Recorder for Debugging Latency Issues
Source: blog.golang.org
if err := recorder.Stop(); err != nil {
    log.Printf("failed to stop flight recorder: %v", err)
}

Step 7: Analyze the Captured Trace

Use the Go tool suite to analyze the saved trace file. For example, run:

go tool trace trace.out

This opens a web interface that shows goroutine activity, network blocking, syscalls, and more. Look for the exact moment of failure and the preceding events to identify root causes.

Tips for Effective Flight Recording

  • Choose the right buffer size. A buffer that is too short may not capture the root cause; too long wastes memory. Start with 10 seconds and adjust based on your application's typical latency patterns.
  • Use conditional snapshotting. Don't snapshot on every minor error. Filter to serious issues like request timeouts or repeated failures to avoid flooding your storage.
  • Combine with logging. Include contextual information (e.g., request ID, error message) alongside the trace snapshot for easier correlation.
  • Test in development. Simulate failures to ensure your snapshot logic works correctly before deploying to production.
  • Consider security. Execution traces may contain sensitive data like request parameters. Be mindful of where you store trace files.
  • Use the flight recorder as a complement, not a replacement. It's great for post-mortem debugging, but you may still need sampling or continuous profiling for proactive monitoring.

With the Go 1.25 flight recorder, you can now capture the crucial moments before a failure, making it easier to diagnose and fix latency problems in your services.