0b58286ca2
Separate *http.Server + *http.ServeMux (AD-024), default disabled. Operator opts in via --pprof <addr>. WARN logged on startup. All pprof handlers explicitly registered on dedicated mux (no DefaultServeMux side-effect). I-308 deferred since v0.2 now implemented. 6 new tests. ---ci--- project: orca phase: 4 milestone: v0.7 status: verify requirements: covered: [REQ-056] partial: [] ---/ci---
46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package daemon
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/pprof"
|
|
"time"
|
|
)
|
|
|
|
func StartPprof(addr string, log *slog.Logger) (*http.Server, error) {
|
|
if addr == "" {
|
|
return nil, nil
|
|
}
|
|
mux := http.NewServeMux()
|
|
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.Handle("/debug/pprof/heap", pprof.Handler("heap"))
|
|
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
|
|
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
|
|
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
|
|
mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
|
|
|
|
server := &http.Server{
|
|
Addr: addr,
|
|
Handler: mux,
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
}
|
|
|
|
log.Warn("pprof endpoint exposed",
|
|
slog.String("addr", addr),
|
|
slog.String("warning", "unauthenticated, operator-only — do not expose publicly"))
|
|
|
|
go func() {
|
|
err := server.ListenAndServe()
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Error("pprof server stopped", slog.String("addr", addr), slog.Any("err", err))
|
|
}
|
|
}()
|
|
|
|
return server, nil
|
|
}
|