Go runtime/trace: execution traces, regions, tasks, and flight recording
Answer: Package trace contains facilities for programs to generate traces of their execution, plus user annotation APIs and a FlightRecorder.
Execution traces
The trace captures a wide range of events, including garbage collection, heap-size changes, and processor start and stop. Events carry a nanosecond timestamp and a stack trace.
When CPU profiling is active, the tracer also tries to include that information.
Start(w) turns tracing on and writes a buffered trace to w. It returns an error if tracing is already enabled. Stop ends the current trace, if any, and returns only after all writes have completed.
IsEnabled reports whether tracing is enabled. The information is advisory only.
Annotations and flight recording
There are three types of user annotations: log messages, regions, and tasks.
A region records a time span inside one goroutine and can nest. WithRegion starts a region, runs fn, and ends it. StartRegion returns a Region whose End method closes it.
A task is a higher-level marker for logical work that may span goroutines. Log messages and regions attach to the current task, if any. NewTask returns a context and a Task. The End method finishes that task. The trace tool computes the latency of a task.
Log and Logf emit a one-off event with a category and message. Category can be empty. The API assumes only a handful of unique categories.
FlightRecorder tracks a moving window over the execution trace. NewFlightRecorder creates a new flight recorder. Start activates it, Stop ends recording, and WriteTo writes the window. At most one flight recorder may be active at any given time.
See also
Claims
- id: C1 claim: The package provides facilities for programs to generate traces. source: S1 evidence: "facilities for programs to generate traces"
- id: C2 claim: Start enables tracing for the current program and writes the trace to a writer. source: S1 evidence: "Start enables tracing for the current program"
- id: C3 claim: A region logs a time interval during one goroutine's execution. source: S2 evidence: "A region is for logging a time interval during a goroutine"
Sources
- [S1] trace package - runtime/trace. The Go Authors. https://pkg.go.dev/runtime/trace . License: BSD-3-Clause. Accessed 2026-11-27.
- [S2] trace.go (source). The Go Authors. https://go.dev/src/runtime/trace/trace.go . License: BSD-3-Clause. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, trace package - runtime/trace; The Go Authors, trace.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, trace package - runtime/trace; The Go Authors, trace.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go runtime/trace: execution traces, regions, tasks, and flight recording." iinventedlight Library, iil:note:jx0pwpvp, version of 2026-11-27. https://iinventedlight.com/library/notes/go-runtime-trace/. License: CC-BY-4.0.
@misc{iil_note_jx0pwpvp,
author = {{iinventedlight}},
title = {{Go runtime/trace: execution traces, regions, tasks, and flight recording}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/go-runtime-trace/},
note = {Permanent ID iil:note:jx0pwpvp. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-runtime-trace.schema.json (schema; CC-BY-4.0)
- go-runtime-trace.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-runtime-trace.md.