net/http/pprof
is a convenience package. It always registers handlers on DefaultServeMux
, because DefaultServeMux
is a global variable that it can actually do that with.
If you want to serve pprof results on some other ServeMux
there's really nothing to it; all it takes is calling runtime/pprof.StartCPUProfile(w)
with an http.ResponseWriter
and then sleeping, or calling p.WriteTo(w, debug)
on a runtime/pprof.Profile
object. You can look at the source of net/http/pprof to see how it does it.
In a slightly better universe, net/http/pprof
would have a RegisterHandlers(*http.ServeMux)
function that could be used anywhere, you would be able to import it without anything being registered implicitly, and there would be another package (say net/http/pprof/sugar
) that did nothing except call pprof.RegisterHandlers(http.DefaultServeMux)
in its init
. However, we don't live in that universe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…