Skip to content

Commit 49d5b67

Browse files
committed
feat: add AttachHandlers method to net/http/pprof to support non-default muxes
1 parent 932ec2b commit 49d5b67

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/net/http/pprof/pprof.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
// By default, all the profiles listed in [runtime/pprof.Profile] are
2626
// available (via [Handler]), in addition to the [Cmdline], [Profile], [Symbol],
2727
// and [Trace] profiles defined in this package.
28-
// If you are not using DefaultServeMux, you will have to register handlers
29-
// with the mux you are using.
28+
//
29+
// If you are not using DefaultServeMux, you can register the pprof handlers
30+
// using the `AttachHandlers` function:
31+
//
32+
// mux := http.NewServeMux()
33+
// pprof.AttachHandlers("", mux)
3034
//
3135
// # Parameters
3236
//
@@ -97,11 +101,17 @@ func init() {
97101
if godebug.New("httpmuxgo121").Value() != "1" {
98102
prefix = "GET "
99103
}
100-
http.HandleFunc(prefix+"/debug/pprof/", Index)
101-
http.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline)
102-
http.HandleFunc(prefix+"/debug/pprof/profile", Profile)
103-
http.HandleFunc(prefix+"/debug/pprof/symbol", Symbol)
104-
http.HandleFunc(prefix+"/debug/pprof/trace", Trace)
104+
AttachHandlers(prefix, http.DefaultServeMux)
105+
}
106+
107+
// AttachHandlers attaches all known pprof handlers to the provided mux.
108+
// All routes are prefixed with the provided prefix.
109+
func AttachHandlers(prefix string, mux *http.ServeMux) {
110+
mux.HandleFunc(prefix+"/debug/pprof/", Index)
111+
mux.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline)
112+
mux.HandleFunc(prefix+"/debug/pprof/profile", Profile)
113+
mux.HandleFunc(prefix+"/debug/pprof/symbol", Symbol)
114+
mux.HandleFunc(prefix+"/debug/pprof/trace", Trace)
105115
}
106116

107117
// Cmdline responds with the running program's

0 commit comments

Comments
 (0)