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 }