Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pprof profiling #1527

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof" // #nosec
"time"

"github.com/open-policy-agent/cert-controller/pkg/rotator"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -48,6 +50,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 +82,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 +101,19 @@ func mainErr() error {
config := ctrl.GetConfigOrDie()
config.UserAgent = version.GetUserAgent("webhook")

if enableProfile {
entryLog.Info("enabling pprof profiling", "port", profilePort)
go func() {
server := &http.Server{
Addr: fmt.Sprintf("localhost:%d", profilePort),
ReadHeaderTimeout: 5 * time.Second,
}
if err := server.ListenAndServe(); 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
Loading