|
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 | //
|
@@ -93,15 +97,21 @@ import (
|
93 | 97 | )
|
94 | 98 |
|
95 | 99 | 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) { |
96 | 105 | prefix := ""
|
97 | 106 | if godebug.New("httpmuxgo121").Value() != "1" {
|
98 | 107 | prefix = "GET "
|
99 | 108 | }
|
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) |
105 | 115 | }
|
106 | 116 |
|
107 | 117 | // Cmdline responds with the running program's
|
|
0 commit comments