| 1 | // Package debug serves Go's profiler, net/http/pprof, to our ops tools: CPU |
| 2 | // and allocation profiles, goroutine dumps and execution traces, from a |
| 3 | // running service. Only ever behind mTLS, on an internal port. |
| 4 | package debug |
| 5 | |
| 6 | import ( |
| 7 | "net/http" |
| 8 | "net/http/pprof" |
| 9 | ) |
| 10 | |
| 11 | // Handler serves the profiler under /internal/debug/pprof/. Importing |
| 12 | // net/http/pprof also registers it on http.DefaultServeMux: none of our |
| 13 | // servers use that mux, and none ever should. |
| 14 | func Handler() http.Handler { |
| 15 | mux := http.NewServeMux() |
| 16 | // A CPU profile, or an execution trace, of the next ?seconds=. |
| 17 | mux.HandleFunc("GET /internal/debug/pprof/profile", pprof.Profile) |
| 18 | mux.HandleFunc("GET /internal/debug/pprof/trace", pprof.Trace) |
| 19 | // Every other profile by name, like allocs or goroutine. Index looks for |
| 20 | // them under /debug/pprof/, so it gets the path without /internal. |
| 21 | mux.Handle("GET /internal/debug/pprof/", http.StripPrefix("/internal", http.HandlerFunc(pprof.Index))) |
| 22 | return mux |
| 23 | } |