From 5b30db9e48985c6652f7418e7422c8133136d249 Mon Sep 17 00:00:00 2001 From: Ben McClelland Date: Wed, 27 Mar 2024 09:26:19 -0700 Subject: [PATCH] feat: add optional pprof debug endpoint Fixes #359. This adds the pprof listening endpoint when configured. The option requires providing the listening port. Once enabled, pprof debug utilities are provided at this endpoint. For example, adding to following option: --pprof 127.0.0.1:9999 Creates a listener on localhost port 9999. You can then point a browser to http://localhost:9999/debug/pprof/ to get access to the debug utilities. Another useful case is to get goroutine stack traces live with the following: curl 'http://localhost:9999/debug/pprof/goroutine?debug=1' --- cmd/versitygw/main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/versitygw/main.go b/cmd/versitygw/main.go index e711a30..b5bc888 100644 --- a/cmd/versitygw/main.go +++ b/cmd/versitygw/main.go @@ -19,6 +19,8 @@ import ( "crypto/tls" "fmt" "log" + "net/http" + _ "net/http/pprof" "os" "github.com/gofiber/fiber/v2" @@ -44,6 +46,7 @@ var ( accessLog string healthPath string debug bool + pprof string quiet bool iamDir string ldapURL, ldapBindDN, ldapPassword string @@ -187,6 +190,12 @@ func initFlags() []cli.Flag { EnvVars: []string{"VGW_DEBUG"}, Destination: &debug, }, + &cli.StringFlag{ + Name: "pprof", + Usage: "enable pprof debug on specified port", + EnvVars: []string{"VGW_PPROF"}, + Destination: &pprof, + }, &cli.BoolFlag{ Name: "quiet", Usage: "silence stdout request logging output", @@ -373,6 +382,14 @@ func runGateway(ctx context.Context, be backend.Backend) error { return fmt.Errorf("root user access and secret key must be provided") } + if pprof != "" { + // listen on specified port for pprof debug + // point browser to http:///debug/pprof/ + go func() { + log.Fatal(http.ListenAndServe(pprof, nil)) + }() + } + app := fiber.New(fiber.Config{ AppName: "versitygw", ServerHeader: "VERSITYGW",