Skip to content

Commit 5e91f42

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

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
//
@@ -93,15 +97,21 @@ import (
9397
)
9498

9599
func init() {
100+
AttachHandlers(http.DefaultServeMux)
101+
}
102+
103+
// AttachHandlers attaches all known pprof handlers to the provided mux.
104+
func AttachHandlers(mux *http.ServeMux) {
96105
prefix := ""
97106
if godebug.New("httpmuxgo121").Value() != "1" {
98107
prefix = "GET "
99108
}
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)
109+
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)