From 3c158ef231d3d5e7c64e8cdd30ddce635f0f929c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 4 Dec 2023 01:16:03 +0100 Subject: [PATCH] [core] Add way to enable profiling Signed-off-by: Knut Ahlers --- main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/main.go b/main.go index a5ba5b8..3002edf 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "math" "net" "net/http" + "net/http/pprof" "os" "strings" "sync" @@ -174,6 +175,20 @@ func main() { router.HandleFunc("/openapi.json", handleSwaggerRequest) router.HandleFunc("/selfcheck", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(runID)) }) + if os.Getenv("ENABLE_PROFILING") == "true" { + router.HandleFunc("/debug/pprof/", pprof.Index) + router.Handle("/debug/pprof/allocs", pprof.Handler("allocs")) + router.Handle("/debug/pprof/block", pprof.Handler("block")) + router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine")) + router.Handle("/debug/pprof/heap", pprof.Handler("heap")) + router.Handle("/debug/pprof/mutex", pprof.Handler("mutex")) + router.HandleFunc("/debug/pprof/profile", pprof.Profile) + router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate")) + router.HandleFunc("/debug/pprof/trace", pprof.Trace) + } + router.MethodNotAllowedHandler = corsMiddleware(http.HandlerFunc(func(res http.ResponseWriter, r *http.Request) { if r.Method == http.MethodOptions { // Most likely JS client asking for CORS headers