chore: serve the profiling endpoints on their own listener - #358
chore: serve the profiling endpoints on their own listener#358mennatnaga wants to merge 3 commits into
Conversation
9d37047 to
7bc5439
Compare
7bc5439 to
bea336c
Compare
bea336c to
38e7f2e
Compare
38e7f2e to
dd151fc
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
tzdybal
left a comment
There was a problem hiding this comment.
I would prefix env variables with SHINZO_ for consistency with other flags and to distinguish from standard Go stdlib flags.
Splitting listen and serve can improve error handling very much - we can stop immediately if listen address is invalid, instead of running with single line of error (not even in log).
| mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
| mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
| mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
| mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
| mux.HandleFunc("/debug/pprof/trace", pprof.Trace) |
There was a problem hiding this comment.
| mux.HandleFunc("/debug/pprof/", pprof.Index) | |
| mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | |
| mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | |
| mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | |
| mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | |
| mux.HandleFunc("/GET debug/pprof/", pprof.Index) | |
| mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) | |
| mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) | |
| mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) | |
| mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) |
| // serveDebug starts the debug listener on addr. The listener is what makes the endpoints | ||
| // reachable, so leaving the address unset turns them off without a rebuild. | ||
| func serveDebug(addr string) { | ||
| if os.Getenv("PPROF_BLOCK_MUTEX") != "" { |
There was a problem hiding this comment.
| if os.Getenv("PPROF_BLOCK_MUTEX") != "" { | |
| if os.Getenv("SHINZO_PPROF_BLOCK_MUTEX") != "" { |
| } | ||
|
|
||
| go func() { | ||
| fmt.Fprintf(os.Stderr, "debug endpoints listening on %s\n", addr) |
There was a problem hiding this comment.
We probably should use logger here.
| } | ||
|
|
||
| func main() { | ||
| if addr := os.Getenv("PPROF_ADDR"); addr != "" { |
There was a problem hiding this comment.
| if addr := os.Getenv("PPROF_ADDR"); addr != "" { | |
| if addr := os.Getenv("SHINZO_PPROF_ADDR"); addr != "" { |
| runtime.SetMutexProfileFraction(1) | ||
| } | ||
|
|
||
| srv := &http.Server{ |
There was a problem hiding this comment.
If we split listen and serve, we can synchronously notify about invalid pprof server address
dd151fc to
fed764b
Compare
Stacked on #357.
The profiling endpoints are moved onto their own mux and listener. Giving a listener a nil handler makes net/http fall back to
http.DefaultServeMux, which carries whatever any linked package registered on it, so the profiling port would answer unrelated endpoints.The listener only starts when
PPROF_ADDRis set, so leaving it unset turns profiling off without a rebuild.It binds wherever
PPROF_ADDRpoints, with no loopback default. In the fleet that protection comes from the docker port mapping rather than from here.