Skip to content

Commit 3007b78

Browse files
committed
feat: add pprof profiling
Signed-off-by: Anish Ramasekar <[email protected]>
1 parent 52d5b6c commit 3007b78

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cmd/webhook/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"flag"
66
"fmt"
77
"net/http"
8+
_ "net/http/pprof" // #nosec
9+
"time"
810

911
"github.com/open-policy-agent/cert-controller/pkg/rotator"
1012
"k8s.io/apimachinery/pkg/runtime"
@@ -48,6 +50,8 @@ var (
4850
disableCertRotation bool
4951
metricsBackend string
5052
logLevel string
53+
enableProfile bool
54+
profilePort int
5155

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

8389
ctx := signals.SetupSignalHandler()
@@ -95,6 +101,19 @@ func mainErr() error {
95101
config := ctrl.GetConfigOrDie()
96102
config.UserAgent = version.GetUserAgent("webhook")
97103

104+
if enableProfile {
105+
entryLog.Info("enabling pprof profiling", "port", profilePort)
106+
go func() {
107+
server := &http.Server{
108+
Addr: fmt.Sprintf("localhost:%d", profilePort),
109+
ReadHeaderTimeout: 5 * time.Second,
110+
}
111+
if err := server.ListenAndServe(); err != nil {
112+
panic(fmt.Errorf("entrypoint: failed to start pprof server: %w", err))
113+
}
114+
}()
115+
}
116+
98117
// initialize metrics exporter before creating measurements
99118
entryLog.Info("initializing metrics backend", "backend", metricsBackend)
100119
if err := metrics.InitMetricsExporter(metricsBackend); err != nil {

0 commit comments

Comments
 (0)