From 7be82abcb746b4e5c20cf16922b75c0536db4ff6 Mon Sep 17 00:00:00 2001 From: Sergey Kamardin Date: Mon, 22 Mar 2021 20:00:58 +0300 Subject: [PATCH] signal/grpc: ability to pprof the server (#472) Thanks @gobwas ! --- cmd/signal/grpc/main.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/signal/grpc/main.go b/cmd/signal/grpc/main.go index 345d938c0..3d1bb7518 100644 --- a/cmd/signal/grpc/main.go +++ b/cmd/signal/grpc/main.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "net/http" + _ "net/http/pprof" "os" "github.com/pion/ion-sfu/pkg/middlewares/datachannel" @@ -37,7 +38,9 @@ var ( addr string metricsAddr string verbosityLevel int - logger = log.New() + paddr string + + logger = log.New() ) const ( @@ -50,6 +53,7 @@ func showHelp() { fmt.Println(" -a {listen addr}") fmt.Println(" -h (show help info)") fmt.Println(" -v {0-10} (verbosity level, default 0)") + fmt.Println(" -paddr {pprof listen addr}") } @@ -97,8 +101,14 @@ func parse() bool { flag.StringVar(&addr, "a", ":50051", "address to use") flag.StringVar(&metricsAddr, "m", ":8100", "merics to use") flag.IntVar(&verbosityLevel, "v", -1, "verbosity level, higher value - more logs") + flag.StringVar(&paddr, "paddr", "", "pprof listening address") help := flag.Bool("h", false, "help info") flag.Parse() + + if paddr == "" { + paddr = getEnv("paddr") + } + if !load() { return false } @@ -109,6 +119,14 @@ func parse() bool { return true } +func getEnv(key string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + + return "" +} + func startMetrics(addr string) { // start metrics server m := http.NewServeMux() @@ -151,6 +169,13 @@ func main() { os.Exit(1) } + if paddr != "" { + go func() { + logger.Info("PProf Listening", "addr", paddr) + _ = http.ListenAndServe(paddr, http.DefaultServeMux) + }() + } + s := grpc.NewServer( grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor), )