From 81df855b27900ad773d5d22439e94309552cd182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Fri, 29 Nov 2024 14:28:20 +0100 Subject: [PATCH 1/2] Add pprof build tag --- client/cmd/pprof.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 client/cmd/pprof.go diff --git a/client/cmd/pprof.go b/client/cmd/pprof.go new file mode 100644 index 00000000000..eb74b17c251 --- /dev/null +++ b/client/cmd/pprof.go @@ -0,0 +1,35 @@ +//go:build pprof + +package cmd + +import ( + "net/http" + _ "net/http/pprof" + "os" + + log "github.com/sirupsen/logrus" +) + +func init() { + if addr, ok := isPprofAddr(); ok { + go pprof(addr) + } else { + go pprof("localhost:6969") + } +} + +func isPprofAddr() (string, bool) { + listenAddr := os.Getenv("NB_PPROF_ADDR") + if listenAddr == "" { + return "", false + } + + return listenAddr, true +} + +func pprof(listenAddr string) { + log.Infof("listening pprof on: %s\n", listenAddr) + if err := http.ListenAndServe(listenAddr, nil); err != nil { + log.Fatalf("Failed to start pprof: %v", err) + } +} From 0011e653652e083f288ecdc274cbc0028f553778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Fri, 29 Nov 2024 14:36:40 +0100 Subject: [PATCH 2/2] Change env handling --- client/cmd/pprof.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/client/cmd/pprof.go b/client/cmd/pprof.go index eb74b17c251..37efd35f0cd 100644 --- a/client/cmd/pprof.go +++ b/client/cmd/pprof.go @@ -1,4 +1,5 @@ //go:build pprof +// +build pprof package cmd @@ -11,20 +12,17 @@ import ( ) func init() { - if addr, ok := isPprofAddr(); ok { - go pprof(addr) - } else { - go pprof("localhost:6969") - } + addr := pprofAddr() + go pprof(addr) } -func isPprofAddr() (string, bool) { +func pprofAddr() string { listenAddr := os.Getenv("NB_PPROF_ADDR") if listenAddr == "" { - return "", false + return "localhost:6969" } - return listenAddr, true + return listenAddr } func pprof(listenAddr string) {