A few months ago we started noticing issues related to what we believed, and eventually confirmed due to our NewRelic/AWS CloudWatch dashboards, were memory leaks, this quickly reminded me of the time I attended Bill Kennedy’s Advanced Go Workshop where he described different ways for profiling code, back at that time the web UI for visualizing profiles was not really oficially released. On a side note I highly recommend you attending any of Bill’s presentations and/or workshops, they are phenomenal!
Profiling in Go reminds me of the old good days when I was profiling C programs in my first job 15 years ago, it’s funny how the UX hasn’t changed that much really, except for the HTTP-based profiling.
For Go there are some great official resources for getting familiar with the ecosystem, specifically:
- golang.org Diagnostics,
- Profiling Go Programs,
- Standard library: runtime/pprof
- Standard library: net/http/pprof
For http programs the important bit to use is the handler defined in
net/http/pprof in your http mux. For example using gorilla.Mux doing something like the following is enough:
import "net/http/pprof" // ... some other relevant code here ... router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
/debug/pprof/ is behind some sort of Auth service to avoid leaking proprietary source code.