|
25 | 25 | // By default, all the profiles listed in [runtime/pprof.Profile] are
|
26 | 26 | // available (via [Handler]), in addition to the [Cmdline], [Profile], [Symbol],
|
27 | 27 | // 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) |
30 | 34 | //
|
31 | 35 | // # Parameters
|
32 | 36 | //
|
@@ -97,11 +101,17 @@ func init() {
|
97 | 101 | if godebug.New("httpmuxgo121").Value() != "1" {
|
98 | 102 | prefix = "GET "
|
99 | 103 | }
|
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) |
105 | 115 | }
|
106 | 116 |
|
107 | 117 | // Cmdline responds with the running program's
|
|
0 commit comments