Programming

How to Capture and Analyze Go Execution Traces with the Flight Recorder

2026-05-02 19:29:04

Introduction

In Go 1.25, the flight recorder introduces a powerful new way to diagnose latency issues in long-running services. Unlike traditional execution traces that require you to start recording before a problem occurs, the flight recorder continuously buffers the last few seconds of trace data in memory. When your program detects an anomaly—such as a timeout or a failed health check—it can instantly snapshot that exact window, giving you the precise trace data you need to find the root cause. This guide walks you through setting up and using the flight recorder step by step.

How to Capture and Analyze Go Execution Traces with the Flight Recorder
Source: blog.golang.org

What You Need

Step-by-Step Guide

Step 1: Import the Necessary Package

Start by importing the runtime/trace package in your Go source file. This package contains both the traditional trace API and the new flight recorder functionality.

import "runtime/trace"

Step 2: Create a Flight Recorder Instance

Initialize a flight recorder with a specific buffer duration. This determines how many seconds of execution trace are kept in memory. For most services, a buffer of 10–30 seconds provides a good balance between memory usage and diagnostic coverage.

fr := trace.NewFlightRecorder(15 * time.Second) // buffer last 15 seconds

Step 3: Start the Flight Recorder

Call the Start method to begin continuous tracing. This replaces the old pattern of calling trace.Start and trace.Stop manually. The flight recorder runs in the background, constantly overwriting the buffer.

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

Step 4: Define Your Anomaly Detection Logic

Identify the conditions that indicate a problem in your application. Common triggers include:

For example, in an HTTP server, you might log a warning when a handler takes longer than 500ms:

http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
    start := time.Now()
    // ... handle request ...
    if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
        // Trigger snapshot here (see step 5)
    }
})

Step 5: Capture a Snapshot When a Problem Occurs

When your detection logic fires, call the flight recorder’s Snapshot method to obtain the buffered trace data. This returns a []byte slice containing the trace in the standard format.

data, err := fr.Snapshot()
if err != nil {
    log.Printf("snapshot failed: %v", err)
    return
}

Step 6: Write the Snapshot to a File

Save the captured trace to a file so you can analyze it later. Use a descriptive filename that includes a timestamp or request identifier.

How to Capture and Analyze Go Execution Traces with the Flight Recorder
Source: blog.golang.org
filename := fmt.Sprintf("trace_snapshot_%d.out", time.Now().Unix())
if err := os.WriteFile(filename, data, 0644); err != nil {
    log.Printf("failed to write trace file: %v", err)
    return
}

Step 7: Analyze the Trace

Use the go tool trace command to open and examine the captured file. This tool provides a web-based viewer that shows goroutine lifetimes, network activity, GC events, and more.

go tool trace trace_snapshot_1234567890.out

In the viewer, focus on the time window just before the snapshot was taken to identify what caused the slowdown or failure.

Step 8: Integrate with Your Logging and Alerting

For production use, combine snapshot capture with your existing logging and alerting systems. For example:

Tips for Effective Use

By following these steps, you can leverage the Go flight recorder to quickly diagnose latency and reliability issues in your services, turning a vague “something went wrong” into actionable trace data.

Explore

BYD's Denza Z: 1,000+ HP Electric Hypercar Set to Challenge Europe's Elite How to Capture a Rocket Launch from Orbit: A Satellite Photographer's Guide 10 Iconic Heroes and Villains From the World of Eternia You Need to Know How Paleontologists Unearthed a 50-Foot Prehistoric Snake: A Step-by-Step Guide Aerobic Exercise Triumphs as Top Remedy for Knee Arthritis Pain, Landmark Study Finds