diff --git a/src/net/http/pprof/pprof.go b/src/net/http/pprof/pprof.go index 6ba6b2c8e033b9..7bf0fba048d423 100644 --- a/src/net/http/pprof/pprof.go +++ b/src/net/http/pprof/pprof.go @@ -25,8 +25,12 @@ // By default, all the profiles listed in [runtime/pprof.Profile] are // available (via [Handler]), in addition to the [Cmdline], [Profile], [Symbol], // and [Trace] profiles defined in this package. -// If you are not using DefaultServeMux, you will have to register handlers -// with the mux you are using. +// +// If you are not using DefaultServeMux, you can register the pprof handlers +// using the `AttachHandlers` function: +// +// mux := http.NewServeMux() +// pprof.AttachHandlers("", mux) // // # Parameters // @@ -97,11 +101,17 @@ func init() { if godebug.New("httpmuxgo121").Value() != "1" { prefix = "GET " } - http.HandleFunc(prefix+"/debug/pprof/", Index) - http.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline) - http.HandleFunc(prefix+"/debug/pprof/profile", Profile) - http.HandleFunc(prefix+"/debug/pprof/symbol", Symbol) - http.HandleFunc(prefix+"/debug/pprof/trace", Trace) + AttachHandlers(prefix, http.DefaultServeMux) +} + +// AttachHandlers attaches all known pprof handlers to the provided mux. +// All routes are prefixed with the provided prefix. +func AttachHandlers(prefix string, mux *http.ServeMux) { + mux.HandleFunc(prefix+"/debug/pprof/", Index) + mux.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline) + mux.HandleFunc(prefix+"/debug/pprof/profile", Profile) + mux.HandleFunc(prefix+"/debug/pprof/symbol", Symbol) + mux.HandleFunc(prefix+"/debug/pprof/trace", Trace) } // Cmdline responds with the running program's