Skip to content

Commit 167b32f

Browse files
committed
feat: add pprof profiling
Signed-off-by: Anish Ramasekar <[email protected]>
1 parent f6028b9 commit 167b32f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

cmd/webhook/main.go

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

910
"github.com/open-policy-agent/cert-controller/pkg/rotator"
1011
"k8s.io/apimachinery/pkg/runtime"
@@ -48,6 +49,8 @@ var (
4849
disableCertRotation bool
4950
metricsBackend string
5051
logLevel string
52+
enableProfile bool
53+
profilePort int
5154

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

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

103+
if enableProfile {
104+
entryLog.Info("enabling pprof profiling", "port", profilePort)
105+
go func() {
106+
if err := http.ListenAndServe(fmt.Sprintf("localhost:%d", profilePort), nil); err != nil {
107+
panic(fmt.Errorf("entrypoint: failed to start pprof server: %w", err))
108+
}
109+
}()
110+
}
111+
98112
// initialize metrics exporter before creating measurements
99113
entryLog.Info("initializing metrics backend", "backend", metricsBackend)
100114
if err := metrics.InitMetricsExporter(metricsBackend); err != nil {

0 commit comments

Comments
 (0)