runtime/trace

Guided tour · Runtime & Debug · pkg.go.dev →

Execution tracer — fine-grained timeline of goroutines, syscalls, GC, and user-defined regions. Analyze with `go tool trace`.

Record a trace

Start / Stop

f, _ := os.Create("trace.out")
trace.Start(f)
defer trace.Stop()

// ... workload ...

User annotations

Regions and tasks

ctx, task := trace.NewTask(ctx, "request")
defer task.End()

trace.WithRegion(ctx, "db.query", func() {
    db.Query(...)
})

trace.Log(ctx, "user_id", "42")

Analyze

go tool trace

go tool trace trace.out
# opens browser with goroutine, network, GC views