From b67da63a4e09c2038563903d9bcf95f691e4fc32 Mon Sep 17 00:00:00 2001 From: Tharun Date: Sun, 22 Nov 2020 12:13:58 +0530 Subject: [PATCH] Added profiling for controller Signed-off-by: Tharun --- cmd/controller/app/controller.go | 2 +- cmd/controller/app/options/options.go | 6 ++++++ pkg/metrics/metrics.go | 10 +++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/controller/app/controller.go b/cmd/controller/app/controller.go index 96a6d76d1..6c9ba900e 100644 --- a/cmd/controller/app/controller.go +++ b/cmd/controller/app/controller.go @@ -69,7 +69,7 @@ func Run(opts *options.ControllerOptions, stopCh <-chan struct{}) { os.Exit(1) } - metricsServer, err := ctx.Metrics.Start(opts.MetricsListenAddress) + metricsServer, err := ctx.Metrics.Start(opts.MetricsListenAddress, opts.EnablePprof) if err != nil { log.Error(err, "failed to listen on prometheus address", "address", opts.MetricsListenAddress) os.Exit(1) diff --git a/cmd/controller/app/options/options.go b/cmd/controller/app/options/options.go index a4f6a7b66..3b6ddee14 100644 --- a/cmd/controller/app/options/options.go +++ b/cmd/controller/app/options/options.go @@ -90,6 +90,9 @@ type ControllerOptions struct { // The host and port address, separated by a ':', that the Prometheus server // should expose metrics on. MetricsListenAddress string + // EnablePprof controls whether net/http/pprof handlers are registered with + // the HTTP listener. + EnablePprof bool DNS01CheckRetryPeriod time.Duration } @@ -182,6 +185,7 @@ func NewControllerOptions() *ControllerOptions { EnableCertificateOwnerRef: defaultEnableCertificateOwnerRef, MetricsListenAddress: defaultPrometheusMetricsServerAddress, DNS01CheckRetryPeriod: defaultDNS01CheckRetryPeriod, + EnablePprof: false, } } @@ -285,6 +289,8 @@ func (s *ControllerOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.MetricsListenAddress, "metrics-listen-address", defaultPrometheusMetricsServerAddress, ""+ "The host and port that the metrics endpoint should listen on.") + fs.BoolVar(&s.EnablePprof, "enable-profiling", false, ""+ + "Enable profiling for controller.") } func (o *ControllerOptions) Validate() error { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 173e93d52..2f28ce511 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -27,6 +27,7 @@ import ( "context" "net" "net/http" + pprof "net/http/pprof" "time" logf "github.com/jetstack/cert-manager/pkg/logs" @@ -134,7 +135,7 @@ func New(log logr.Logger) *Metrics { } // Start will register the Prometheu metrics, and start the Prometheus server -func (m *Metrics) Start(listenAddress string) (*http.Server, error) { +func (m *Metrics) Start(listenAddress string, enablePprof bool) (*http.Server, error) { m.registry.MustRegister(m.certificateExpiryTimeSeconds) m.registry.MustRegister(m.certificateReadyStatus) m.registry.MustRegister(m.acmeClientRequestDurationSeconds) @@ -143,6 +144,13 @@ func (m *Metrics) Start(listenAddress string) (*http.Server, error) { router := mux.NewRouter() router.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})) + if enablePprof { + router.HandleFunc("/debug/pprof/", pprof.Index) + router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + router.HandleFunc("/debug/pprof/profile", pprof.Profile) + router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + router.HandleFunc("/debug/pprof/trace", pprof.Trace) + } ln, err := net.Listen("tcp", listenAddress) if err != nil {