Skip to content

Commit

Permalink
feat: add pprof profiling
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <[email protected]>
  • Loading branch information
aramase committed Jan 2, 2025
1 parent f6028b9 commit 167b32f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof" // #nosec

"github.com/open-policy-agent/cert-controller/pkg/rotator"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -48,6 +49,8 @@ var (
disableCertRotation bool
metricsBackend string
logLevel string
enableProfile bool
profilePort int

// DNSName is <service name>.<namespace>.svc
dnsName = fmt.Sprintf("%s.%s.svc", serviceName, util.GetNamespace())
Expand Down Expand Up @@ -78,6 +81,8 @@ func mainErr() error {
flag.StringVar(&metricsBackend, "metrics-backend", "prometheus", "Backend used for metrics")
flag.StringVar(&logLevel, "log-level", "",
"In order of increasing verbosity: unset (empty string), info, debug, trace and all.")
flag.BoolVar(&enableProfile, "enable-pprof", false, "enable pprof profiling")
flag.IntVar(&profilePort, "pprof-port", 6065, "port for pprof profiling")
flag.Parse()

ctx := signals.SetupSignalHandler()
Expand All @@ -95,6 +100,15 @@ func mainErr() error {
config := ctrl.GetConfigOrDie()
config.UserAgent = version.GetUserAgent("webhook")

if enableProfile {
entryLog.Info("enabling pprof profiling", "port", profilePort)
go func() {
if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", profilePort), nil); err != nil {
panic(fmt.Errorf("entrypoint: failed to start pprof server: %w", err))
}
}()
}

// initialize metrics exporter before creating measurements
entryLog.Info("initializing metrics backend", "backend", metricsBackend)
if err := metrics.InitMetricsExporter(metricsBackend); err != nil {
Expand Down

0 comments on commit 167b32f

Please sign in to comment.