简体   繁体   中英

How to start a new http server or using an existing one for pprof?

The pprof package documentation says

The package is typically only imported for the side effect of registering its HTTP handlers. The handled paths all begin with /debug/pprof/."

The documentation says if you already have an http server running you don't need to start another one but if you are not using DefaultServeMux, you will have to register handlers with the mux you are using.

Shouldn't I always use a separate port for pprof? Is it okay to use the same port that I am using for prometheus metrics?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM