From b511fad9682ce373c484070ef5b785b1302370c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 15 Jul 2022 14:43:48 +0200 Subject: [PATCH] Manager: add profiler support Add a `-pprof` CLI option to enable the profiler. It will expose profiler info on the web interface at `/debug/pprof/`. To have a nice view of this, including flame graphs, run: ``` go tool pprof -http localhost:8082 http://localhost:8080/debug/pprof/profile ``` --- cmd/flamenco-manager/main.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmd/flamenco-manager/main.go b/cmd/flamenco-manager/main.go index 6b4b5ca4..b2e65ef6 100644 --- a/cmd/flamenco-manager/main.go +++ b/cmd/flamenco-manager/main.go @@ -11,10 +11,12 @@ import ( "math/rand" "net" "net/http" + http_pprof "net/http/pprof" "net/url" "os" "os/signal" "runtime" + "runtime/pprof" "strings" "sync" "syscall" @@ -55,6 +57,7 @@ var cliArgs struct { writeConfig bool delayResponses bool firstTimeWizard bool + pprof bool } const ( @@ -381,6 +384,20 @@ func buildWebService( return c.Redirect(http.StatusTemporaryRedirect, "/app/") }) + // Register profiler functions. + if cliArgs.pprof { + e.GET("/debug/pprof/", echo.WrapHandler(http.HandlerFunc(http_pprof.Index))) + e.GET("/debug/pprof/cmdline", echo.WrapHandler(http.HandlerFunc(http_pprof.Cmdline))) + e.GET("/debug/pprof/profile", echo.WrapHandler(http.HandlerFunc(http_pprof.Profile))) + e.GET("/debug/pprof/symbol", echo.WrapHandler(http.HandlerFunc(http_pprof.Symbol))) + e.GET("/debug/pprof/trace", echo.WrapHandler(http.HandlerFunc(http_pprof.Trace))) + for _, profile := range pprof.Profiles() { + name := profile.Name() + e.GET("/debug/pprof/"+name, echo.WrapHandler(http_pprof.Handler(name))) + } + log.Info().Msg("profiler debugging info available on /debug/pprof/") + } + // Log available routes routeLogger := log.Level(zerolog.TraceLevel) routeLogger.Trace().Msg("available routes:") @@ -476,6 +493,7 @@ func parseCliArgs() { flag.BoolVar(&cliArgs.delayResponses, "delay", false, "Add a random delay to any HTTP responses. This aids in development of Flamenco Manager's web frontend.") flag.BoolVar(&cliArgs.firstTimeWizard, "wizard", false, "Open a webbrowser with the first-time configuration wizard.") + flag.BoolVar(&cliArgs.pprof, "pprof", false, "Expose profiler endpoints on /debug/pprof/.") flag.Parse()