diff --git a/pkg/server/debug/pprofui/server.go b/pkg/server/debug/pprofui/server.go index beec96377103..70625d0c4273 100644 --- a/pkg/server/debug/pprofui/server.go +++ b/pkg/server/debug/pprofui/server.go @@ -65,33 +65,29 @@ func NewServer(storage Storage, hook func(profile string, labels bool, do func() hook: hook, } - s.profileTypes = map[string]http.HandlerFunc{ - // The CPU profile endpoint is special in that the handler actually blocks - // for a predetermined duration (recording the profile in the meantime). - // It is not included in `runtimepprof.Profiles` below. - "profile": func(w http.ResponseWriter, r *http.Request) { - const defaultProfileDurationSeconds = 5 - if r.Form == nil { - r.Form = url.Values{} - } - if r.Form.Get("seconds") == "" { - r.Form.Set("seconds", strconv.Itoa(defaultProfileDurationSeconds)) - } - s.profileSem.Lock() - defer s.profileSem.Unlock() - pprof.Profile(w, r) - }, - } - // Register the endpoints for heap, block, threadcreate, etc. + s.profileTypes = map[string]http.HandlerFunc{} for _, p := range runtimepprof.Profiles() { - p := p // copy - s.profileTypes[p.Name()] = func(w http.ResponseWriter, r *http.Request) { - if err := p.WriteTo(w, 0 /* debug */); err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = w.Write([]byte(err.Error())) - } + name := p.Name() + s.profileTypes[name] = func(w http.ResponseWriter, r *http.Request) { + pprof.Handler(name).ServeHTTP(w, r) + } + } + // The CPU profile endpoint is special cased because a) it's not in the map + // yet and b) it always needs to block. We want to default to 5s if profiling + // if nothing is specified, we use a convenience mutex to serialize concurrent + // attempts to get a profile (the endpoint otherwise returns an error). + s.profileTypes["profile"] = func(w http.ResponseWriter, r *http.Request) { + const defaultProfileDurationSeconds = 5 + if r.Form == nil { + r.Form = url.Values{} + } + if r.Form.Get("seconds") == "" { + r.Form.Set("seconds", strconv.Itoa(defaultProfileDurationSeconds)) } + s.profileSem.Lock() + defer s.profileSem.Unlock() + pprof.Profile(w, r) } return s diff --git a/pkg/ui/src/views/reports/containers/debug/index.tsx b/pkg/ui/src/views/reports/containers/debug/index.tsx index dd86940513bb..44d07b8200f1 100644 --- a/pkg/ui/src/views/reports/containers/debug/index.tsx +++ b/pkg/ui/src/views/reports/containers/debug/index.tsx @@ -207,6 +207,7 @@ export default function Debug() { +