Skip to content

Commit

Permalink
pprof: add an option to enable pprof profiling (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
tam7t authored Feb 10, 2021
1 parent 8bc8c7b commit 0dc3d7a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Digest: `TODO`
* Initial prometheus metrics collection [#85](https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/pull/85)
* `livenessProbe` [#85](https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/pull/85)
* [Debugging documentation](https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/blob/v0.3.0/docs/debugging.md) [#85](https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/pull/85)
* Optional pprof debugging endpoint [#88](https://github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/pull/88)

## v0.2.0

Expand Down
10 changes: 10 additions & 0 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ kubectl port-forward csi-secrets-store-provider-gcp-vmqct --namespace=kube-syste
curl localhost:8095/metrics
```

## pprof

Starting the plugin with `-enable-pprof=true` will enable a debug http endpoint
at `-debug_addr`. Accessing this will also require `port-forward`:

```cli
kubectl port-forward csi-secrets-store-provider-gcp-vmqct --namespace=kube-system 6060:6060
curl localhost:6060/debug/pprof
```

## Objects

View `SecretProviderClass`s:
Expand Down
31 changes: 28 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"path/filepath"
Expand All @@ -42,6 +43,8 @@ var (
kubeconfig = flag.String("kubeconfig", "", "absolute path to kubeconfig file")
logFormatJSON = flag.Bool("log-format-json", true, "set log formatter to json")
metricsAddr = flag.String("metrics_addr", ":8095", "configure http listener for reporting metrics")
enableProfile = flag.Bool("enable-pprof", false, "enable pprof profiling")
debugAddr = flag.String("debug_addr", "localhost:6060", "port for pprof profiling")

version = "dev"
)
Expand Down Expand Up @@ -85,8 +88,10 @@ func main() {
go g.Serve(l)

// initialize metrics and health http server
mux := http.NewServeMux()
ms := http.Server{
Addr: *metricsAddr,
Addr: *metricsAddr,
Handler: mux,
}
defer ms.Shutdown(ctx)

Expand All @@ -98,8 +103,8 @@ func main() {
if err := runtime.Start(runtime.WithMeterProvider(ex.MeterProvider())); err != nil {
klog.ErrorS(err, "unable to start runtime metrics monitoring")
}
http.HandleFunc("/metrics", ex.ServeHTTP)
http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/metrics", ex.ServeHTTP)
mux.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
go func() {
Expand All @@ -109,6 +114,26 @@ func main() {
}()
klog.InfoS("health server listening", "addr", *metricsAddr)

if *enableProfile {
dmux := http.NewServeMux()
dmux.HandleFunc("/debug/pprof/", pprof.Index)
dmux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
dmux.HandleFunc("/debug/pprof/profile", pprof.Profile)
dmux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
dmux.HandleFunc("/debug/pprof/trace", pprof.Trace)
ds := http.Server{
Addr: *debugAddr,
Handler: dmux,
}
defer ds.Shutdown(ctx)
go func() {
if err := ds.ListenAndServe(); err != nil && err != http.ErrServerClosed {
klog.ErrorS(err, "debug http server error")
}
}()
klog.InfoS("debug server listening", "addr", *debugAddr)
}

<-ctx.Done()
klog.InfoS("terminating")
g.GracefulStop()
Expand Down

0 comments on commit 0dc3d7a

Please sign in to comment.