Skip to content

Commit 16f7321

Browse files
Add webhook total and in-flight metrics
Add a gauge for requests currently being handled and a counter for the total amount of requests processed. Used [prometheus/promhttp](https://pkg.go.dev/github.com/prometheus/client_golang/prometheus/promhttp?tab=doc) package which provides nice decorators for `Http.Handler` to handle the metrics instead of the custom code.
1 parent c2dfe1a commit 16f7321

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

pkg/webhook/internal/metrics/metrics.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,32 @@ var (
3030
Name: "controller_runtime_webhook_latency_seconds",
3131
Help: "Histogram of the latency of processing admission requests",
3232
},
33-
[]string{"webhook"},
33+
[]string{"webhook", "code"},
3434
)
35+
36+
// RequestTotal is a prometheus metric which is a counter of the total processed admission requests.
37+
RequestTotal = func() *prometheus.CounterVec {
38+
return prometheus.NewCounterVec(
39+
prometheus.CounterOpts{
40+
Name: "controller_runtime_webhook_requests_total",
41+
Help: "Total number of admission requests by HTTP status code.",
42+
},
43+
[]string{"webhook", "code"},
44+
)
45+
}()
46+
47+
// RequestInFlight is a prometheus metric which is a gauge of the in-flight admission requests.
48+
RequestInFlight = func() *prometheus.GaugeVec {
49+
return prometheus.NewGaugeVec(
50+
prometheus.GaugeOpts{
51+
Name: "controller_runtime_webhook_requests_in_flight",
52+
Help: "Current number of admission requests being served.",
53+
},
54+
[]string{"webhook"},
55+
)
56+
}()
3557
)
3658

3759
func init() {
38-
metrics.Registry.MustRegister(
39-
RequestLatency)
60+
metrics.Registry.MustRegister(RequestLatency, RequestTotal, RequestInFlight)
4061
}

pkg/webhook/server.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import (
2828
"path/filepath"
2929
"strconv"
3030
"sync"
31-
"time"
3231

32+
"github.com/prometheus/client_golang/prometheus"
33+
"github.com/prometheus/client_golang/prometheus/promhttp"
3334
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3435
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/certwatcher"
3536
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
@@ -123,13 +124,19 @@ func (s *Server) Register(path string, hook http.Handler) {
123124

124125
// instrumentedHook adds some instrumentation on top of the given webhook.
125126
func instrumentedHook(path string, hookRaw http.Handler) http.Handler {
126-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
127-
startTS := time.Now()
128-
defer func() { metrics.RequestLatency.WithLabelValues(path).Observe(time.Since(startTS).Seconds()) }()
129-
hookRaw.ServeHTTP(resp, req)
130-
131-
// TODO(directxman12): add back in metric about total requests broken down by result?
132-
})
127+
lbl := prometheus.Labels{"webhook": path}
128+
129+
lat := metrics.RequestLatency.MustCurryWith(lbl)
130+
cnt := metrics.RequestTotal.MustCurryWith(lbl)
131+
gge := metrics.RequestInFlight.With(lbl)
132+
133+
return promhttp.InstrumentHandlerDuration(
134+
lat,
135+
promhttp.InstrumentHandlerCounter(
136+
cnt,
137+
promhttp.InstrumentHandlerInFlight(gge, hookRaw),
138+
),
139+
)
133140
}
134141

135142
// Start runs the server.

0 commit comments

Comments
 (0)