This article was originally published on bmf-tech.com.
Overview
A note on how to use net/http/pprof with routers other than DefaultServeMux (Go's standard router).
Pitfalls
Simply using a blank import for pprof is not enough.
package main
import (
_ "net/http/pprof"
)
When using a router other than DefaultServeMux, simply using a blank import will not enable pprof.
Refer to net/http/pprof, which states:
If you are not using DefaultServeMux, you will have to register handlers with the mux you are using.
Solution
Below is an example using my custom router bmf-san/goblin.
package main
import (
"net/http/pprof"
)
func main() {
r.Methods(http.MethodGet).Handler("/debug/pprof/", http.HandlerFunc(pprof.Index))
r.Methods(http.MethodGet).Handler("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
r.Methods(http.MethodGet).Handler("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
r.Methods(http.MethodGet).Handler("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
r.Methods(http.MethodGet).Handler("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
r.Methods(http.MethodGet).Handler("/debug/pprof/goroutine", pprof.Handler("goroutine"))
r.Methods(http.MethodGet).Handler("/debug/pprof/heap", pprof.Handler("heap"))
r.Methods(http.MethodGet).Handler("/debug/pprof/mutex", pprof.Handler("mutex"))
r.Methods(http.MethodGet).Handler("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
r.Methods(http.MethodGet).Handler("/debug/pprof/block", pprof.Handler("block"))
}
As shown above, you need to set up the routing yourself and configure the pprof Handlers.
For httprouter, the following issue might be helpful:
pprof issue with httprouter #236
Side Note
I encountered this issue when trying to set up Go profiling in Pull mode with pyroscope.
Top comments (0)