Execution Tracing
go tool trace for goroutine-level analysis
Execution Tracing is a free Go Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
What is execution tracing?
The Go execution tracer records a timeline of goroutine scheduling, GC events, syscalls, and heap growth. It provides a visual view of parallelism and latency unavailable from CPU or heap profiles.
Capturing a trace
Capture a trace via the HTTP endpoint:
curl http://localhost:6060/debug/pprof/trace?seconds=5 -o trace.out
go tool trace trace.outFile-based trace
Use runtime/trace to record a trace programmatically:
f, _ := os.Create("trace.out")
trace.Start(f)
defer trace.Stop()
// run workloadgo test -trace
Capture a trace during test/benchmark runs:
go test -trace trace.out -bench=. ./...
go tool trace trace.outTrace viewer
The trace viewer (go tool trace) opens in a browser showing goroutine timelines, GC events, heap size, and per-goroutine stack traces at any moment in time.
Goroutine analysis
The Goroutine Analysis view groups goroutines by function and shows how long each spends in states: running, runnable (waiting for a P), blocked (channel/mutex/syscall), and sleeping.
Finding scheduler latency
High "runnable" time means goroutines are ready but not scheduled. This indicates too many goroutines competing for GOMAXPROCS Ps, or a CPU-starved process.
GC impact
GC stop-the-world pauses appear as grey bars across all goroutines. Large pauses or frequent GC indicate excessive allocation — combine with heap profiling to reduce allocations.
User annotations
Add custom spans to the trace using trace.WithRegion and trace.Log to annotate your own code sections for easier analysis.
trace.WithRegion(ctx, "parse request", func() {
parseRequest(req)
})task and region
trace.NewTask groups related regions under a named task, letting you measure end-to-end latency for a logical operation across multiple goroutines.
ctx, task := trace.NewTask(ctx, "handle request")
defer task.End()Trace vs profile
CPU profile: which functions consume CPU over time. Heap profile: which functions allocate memory. Trace: when goroutines run, block, and how GC interleaves — different tools for different questions.
Quick Check
What does "runnable" goroutine state mean in the execution trace?
Recap: Execution Tracing
Key points:
- go tool trace shows goroutine timelines, GC events, and scheduling
- Capture via HTTP endpoint or runtime/trace package
- Runnable time → scheduler contention; GC pauses → excessive allocations
- User annotations (WithRegion, NewTask) mark your own sections
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Execution Tracing” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Execution Tracing”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “Execution Tracing”?
go tool trace for goroutine-level analysis You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Execution Tracing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Benchmarks with testing.B
- CPU Profiling with pprof
- Heap and Allocation Profiling
- Execution Tracing