From e4ae031faf6bea1873ec1bd800a4e705c3488e6d Mon Sep 17 00:00:00 2001 From: Paul Hewlett Date: Fri, 29 Nov 2024 13:33:24 +0000 Subject: [PATCH] Enable pprof AB#8893 --- .env.tools | 2 +- httpserver/pprofserver.go | 21 +++++++++++++++++++++ startup/listener.go | 11 +++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 httpserver/pprofserver.go diff --git a/.env.tools b/.env.tools index 61bd6ab..a7f0723 100644 --- a/.env.tools +++ b/.env.tools @@ -1 +1 @@ -export TOOLS_BUILDNUMBER=20240829.1 +export TOOLS_BUILDNUMBER=20241108.3 diff --git a/httpserver/pprofserver.go b/httpserver/pprofserver.go new file mode 100644 index 0000000..768ec53 --- /dev/null +++ b/httpserver/pprofserver.go @@ -0,0 +1,21 @@ +package httpserver + +import ( + "net/http" + + "github.com/datatrails/go-datatrails-common/environment" +) + +func NewPPROF(log Logger, name string) *Server { + port, err := environment.GetRequired("PPROF_PORT") + if err != nil { + return nil + } + h := http.NewServeMux() + h.HandleFunc(prefix+"/debug/pprof/", prof.Index) + h.HandleFunc(prefix+"/debug/pprof/cmdline", prof.Cmdline) + h.HandleFunc(prefix+"/debug/pprof/profile", prof.Profile) + h.HandleFunc(prefix+"/debug/pprof/symbol", prof.Symbol) + h.HandleFunc(prefix+"/debug/pprof/trace", prof.Trace) + return New(log, name, port, h) +} diff --git a/startup/listener.go b/startup/listener.go index e701218..69f00a3 100644 --- a/startup/listener.go +++ b/startup/listener.go @@ -44,6 +44,17 @@ func WithListeners(listeners ...Listener) ListenersOption { } } +// WithOptionalListeners add multiple listeners. Nil listeners are ignored. +func WithOptionalListeners(listeners ...Listener) ListenersOption { + return func(l *Listeners) { + for _, listener := range listeners { + if listener != nil { + l.listeners = append(l.listeners, listener) + } + } + } +} + func NewListeners(log Logger, name string, opts ...ListenersOption) Listeners { l := Listeners{name: strings.ToLower(name)} for _, opt := range opts {