Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/pprof: add AttachHandlers method to net/http/pprof to support non-default muxes #71214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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