Go ships with strong built-in diagnostics: runtime/pprof and net/http/pprof for CPU, heap and goroutine profiles, runtime/trace for the execution tracer, runtime/metrics and expvar for counters, and log/slog for structured logs. For production monitoring you add a metrics library such as the Prometheus client or OpenTelemetry, plus health-check endpoints.
This article shows which tool answers which question, with working code for profiling, metrics, health checks and tracing.
| Question | Tool |
|---|---|
| Which functions burn CPU? | CPU profile (pprof) |
| Where is memory allocated or leaked? | Heap and allocs profiles |
| Why are goroutines piling up or blocked? | Goroutine, block and mutex profiles |
| Why is latency high or the scheduler idle? | Execution trace (runtime/trace) |
| What is the request rate, error rate and latency? | Metrics (Prometheus or OpenTelemetry) |
| What happened to this one request? | Structured logs and distributed traces |
| Is the process alive and ready? | Liveness and readiness endpoints |
Profiles and traces are for diagnosing a problem. Metrics, logs and traces are for watching a system all the time.
The simplest way to profile a long-running service is to import net/http/pprof, which registers handlers under /debug/pprof/. Serve them on a separate, private port, because profiles can leak internals and cost CPU.
Then collect and inspect profiles with the go tool pprof command:
For a command-line program, write a profile to a file with runtime/pprof. Check every error and stop the profile before the program exits:
Block and mutex profiles are off by default. Enable them with runtime.SetBlockProfileRate and runtime.SetMutexProfileFraction when you suspect contention. Go also supports profile-guided optimisation: place a representative CPU profile as default.pgo in the main package and go build uses it.
The execution tracer records scheduler, garbage collection and goroutine events, which is how you find latency that a CPU profile cannot explain.
For numbers instead of profiles, runtime/metrics exposes stable runtime counters such as heap size, GC cycles and goroutine counts, and runtime.ReadMemStats gives a coarser snapshot. The expvar package publishes variables as JSON at /debug/vars, which is useful for small internal tools.
Prometheus pulls metrics from an HTTP endpoint. The official client library, github.com/prometheus/client_golang, also exports Go runtime and process metrics by default.
Keep label values bounded. Using a user ID or a raw URL as a label creates unbounded series and can overload Prometheus.
Provide a liveness endpoint that returns 200 while the process runs, and a readiness endpoint that checks dependencies such as the database:
For logs, use log/slog (standard since Go 1.21) with JSON output and key-value pairs, so a log platform can filter by field:
For distributed tracing across services, use OpenTelemetry (go.opentelemetry.io/otel), which exports spans and metrics to any compatible backend and propagates context through context.Context.
Yes if you protect it. Profiling has low overhead when sampling, but the endpoints reveal internals, so bind them to localhost or an internal network and require authentication.
A profile aggregates samples to show where time or memory goes. A trace records individual events over time, so it explains latency, scheduling and GC pauses.
They are compatible. Prometheus is simple and very common for metrics. OpenTelemetry covers metrics, traces and logs with one API and can export to Prometheus.
Watch the go_goroutines metric, then fetch /debug/pprof/goroutine?debug=2 to see where the goroutines are blocked.
Use pprof for CPU, memory and contention, the execution tracer for latency, and runtime/metrics or a Prometheus client for live numbers. Add readiness checks, structured logs with slog, and OpenTelemetry for traces. Keep diagnostic endpoints private and metric labels bounded.