Skip to content

Commit

Permalink
feat: add AttachHandlers method to net/http/pprof to support non-defa…
Browse files Browse the repository at this point in the history
…ult muxes
  • Loading branch information
DerekTBrown committed Jan 13, 2025
1 parent 932ec2b commit 5e91f42
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/net/http/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -93,15 +97,21 @@ import (
)

func init() {
AttachHandlers(http.DefaultServeMux)
}

// AttachHandlers attaches all known pprof handlers to the provided mux.
func AttachHandlers(mux *http.ServeMux) {
prefix := ""
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)

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
Expand Down

0 comments on commit 5e91f42

Please sign in to comment.