Added profiling for controller

Signed-off-by: Tharun <rajendrantharun@live.com>
This commit is contained in:
Tharun
2020-11-22 12:15:52 +05:30
parent 92f4d7d349
commit b67da63a4e
3 changed files with 16 additions and 2 deletions
+1 -1
View File
@@ -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)
+6
View File
@@ -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 {
+9 -1
View File
@@ -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 {