Go Tip: Profiling
Mario Carrion
Posted on December 12, 2020
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 officially 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
But perhaps the missing key is the pprof
documentation where details are explained a little better, not to mention the really useful option to compare profiles using -diff_base
.
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)
Ideally that /debug/pprof/
is behind some sort of Auth service to avoid leaking proprietary source code.
Posted on December 12, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.