package health import ( "net/http" _ "net/http/pprof" // Import for side effects (registers pprof handlers) "github.com/fraktal/mev-beta/internal/logger" ) // PprofHandler provides production-safe pprof profiling endpoints type PprofHandler struct { logger *logger.Logger enabled bool } // NewPprofHandler creates a new pprof handler func NewPprofHandler(logger *logger.Logger, enabled bool) *PprofHandler { return &PprofHandler{ logger: logger, enabled: enabled, } } // RegisterHTTPHandlers registers pprof handlers if enabled func (p *PprofHandler) RegisterHTTPHandlers(mux *http.ServeMux) { if !p.enabled { p.logger.Info("🔒 pprof profiling endpoints disabled") return } // pprof handlers are automatically registered on DefaultServeMux // We need to proxy them to our custom mux mux.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) { http.DefaultServeMux.ServeHTTP(w, r) }) mux.HandleFunc("/debug/pprof/cmdline", func(w http.ResponseWriter, r *http.Request) { http.DefaultServeMux.ServeHTTP(w, r) }) mux.HandleFunc("/debug/pprof/profile", func(w http.ResponseWriter, r *http.Request) { http.DefaultServeMux.ServeHTTP(w, r) }) mux.HandleFunc("/debug/pprof/symbol", func(w http.ResponseWriter, r *http.Request) { http.DefaultServeMux.ServeHTTP(w, r) }) mux.HandleFunc("/debug/pprof/trace", func(w http.ResponseWriter, r *http.Request) { http.DefaultServeMux.ServeHTTP(w, r) }) p.logger.Info("✅ pprof profiling endpoints enabled at /debug/pprof/") p.logger.Info(" Available endpoints:") p.logger.Info(" - /debug/pprof/ - Index of available profiles") p.logger.Info(" - /debug/pprof/heap - Memory heap profile") p.logger.Info(" - /debug/pprof/goroutine - Goroutine profile") p.logger.Info(" - /debug/pprof/profile - CPU profile (30s by default)") p.logger.Info(" - /debug/pprof/block - Block profile") p.logger.Info(" - /debug/pprof/mutex - Mutex profile") p.logger.Info(" - /debug/pprof/trace - Execution trace") p.logger.Info("") p.logger.Info(" Usage examples:") p.logger.Info(" go tool pprof http://localhost:6060/debug/pprof/heap") p.logger.Info(" go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30") p.logger.Info(" curl http://localhost:6060/debug/pprof/goroutine?debug=1") }