A Go service is burning CPU, GC pauses are climbing, or the heap is growing by the hour — and you need to localize the cost without taking the service down. pprof is the right tool, but exposing it carelessly turns it into a remote unauthenticated diagnostic backdoor. This article shows how Stack Harbor wires net/http/pprof in production, captures profiles from live services, and reads the output to find what is actually expensive.
How to verify
Confirm the pprof endpoint is reachable behind an internal listener:
curl -s http://127.0.0.1:6060/debug/pprof/ | head
go tool pprof -seconds=30 http://127.0.0.1:6060/debug/pprof/profile
go tool pprof http://127.0.0.1:6060/debug/pprof/heap
go tool pprof http://127.0.0.1:6060/debug/pprof/allocs
go tool pprof http://127.0.0.1:6060/debug/pprof/goroutine
Inside the interactive shell, top10, list <funcName>, web (renders SVG), and peek (call-graph context) are the bread-and-butter commands.
What’s happening
The Go runtime samples four kinds of events on demand. CPU profile samples the program counter ~100 times per second for the duration you ask for; it tells you where wall-clock time is spent. Heap profile samples allocations and reports either live objects (inuse_space) or cumulative since startup (alloc_space). Goroutine profile is a snapshot of every running goroutine and its current stack — invaluable for deadlock and leak diagnosis. Allocs profile is the running tape of every allocation sample since process start.
net/http/pprof registers handlers under /debug/pprof/. Importing the package with a blank import (_ "net/http/pprof") wires those handlers into http.DefaultServeMux. If your main HTTP listener uses DefaultServeMux, you have just made /debug/pprof/ public — which is the failure mode we keep finding in audits. The fix is to run pprof on a separate mux bound to a private listener (loopback or an internal-only port behind your firewall).
A profile is a sample, not a transaction trace. Reading a flame graph correctly means: width = cumulative time/allocations in that call frame, depth = call stack. The hot frames at the top are the ones to investigate.
The procedure
-
Wire pprof on a private listener. Do NOT use
_ "net/http/pprof"on the main mux:package main import ( "log/slog" "net/http" _ "net/http/pprof" ) func main() { go func() { // Loopback only — exposed via kubectl port-forward / SSH tunnel. mux := http.DefaultServeMux if err := http.ListenAndServe("127.0.0.1:6060", mux); err != nil { slog.Error("pprof listener failed", "err", err) } }() startMainServer() } -
Capture a 30-second CPU profile from prod. Port-forward the pprof port to your workstation, then:
kubectl port-forward deploy/api 6060:6060 & go tool pprof -http=:8000 -seconds=30 http://127.0.0.1:6060/debug/pprof/profile-http=:8000boots an interactive browser UI with flame graph, peek, source, top, and call-graph views. -
Capture a heap snapshot. Heap profiles are cheap (sampling), so you can take one at any time:
curl -o heap.pb.gz http://127.0.0.1:6060/debug/pprof/heap go tool pprof -http=:8000 heap.pb.gzCompare two snapshots to find a leak:
go tool pprof -http=:8000 -base heap-t0.pb.gz heap-t1.pb.gzOnly allocations that grew between t0 and t1 will be visible — exactly what you want when hunting a slow leak.
-
Dump every goroutine when the service hangs. A
?debug=2query returns text-formatted stacks for every goroutine:curl -s 'http://127.0.0.1:6060/debug/pprof/goroutine?debug=2' > goroutines.txt grep -c '^goroutine ' goroutines.txtA million goroutines stuck in
chan receiveis a leak — see golang-goroutine-leak-detection. -
Enable continuous profiling (recommended). Tools like Pyroscope, Polar Signals, or Grafana Cloud Profiles scrape your pprof endpoints on a schedule and store time-series profile data:
scrape_configs: - job_name: api scrape_interval: 15s static_configs: - targets: ['api.internal:6060'] profiling_config: pprof_config: profile: enabled: true path: /debug/pprof/profile delta: true memory: enabled: true path: /debug/pprof/allocsThen you can compare today’s flame graph to last Tuesday without re-creating the incident.
-
Read the profile honestly. Width = cost. A wide frame at the top is the bottleneck. Use
list <funcName>(orpeek) to see which line allocated or which call was hot. Cumulative is not self:runtime.gcBgMarkWorkerbeing 30% wide means you allocate too much, not that the GC is broken.
Common pitfalls
_ "net/http/pprof"on the public mux is a CVE-class mistake. It exposes heap dumps and goroutine stacks to anyone who can reach the port — secrets in env vars eventually surface there. Always use a private listener.- CPU profile less than 10 seconds is statistical noise. Aim for 30-60 seconds during steady-state load.
- Heap profile shows samples, not exact bytes. Default sample rate is one allocation per 512KB. Tune with
runtime.MemProfileRate = 1024to sample more often during a leak hunt. alloc_spacevsinuse_space.alloc_spaceshows everything ever allocated and is great for finding GC pressure.inuse_spaceshows what is currently live — that is the leak detector.- Profile labels can lie. A wide frame from
net/httpdoes not mean http is slow; it usually means your handler is slow inside it. Drill in.
Stack Harbor wires pprof and continuous profiling for every Go service we operate, with the endpoint on a private listener and Pyroscope-style scrape from inside the cluster. See how that fits into our broader observability operating model.