@@ -28,8 +28,9 @@ import (
28
28
"path/filepath"
29
29
"strconv"
30
30
"sync"
31
- "time"
32
31
32
+ "github.com/prometheus/client_golang/prometheus"
33
+ "github.com/prometheus/client_golang/prometheus/promhttp"
33
34
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
34
35
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/certwatcher"
35
36
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
@@ -123,13 +124,19 @@ func (s *Server) Register(path string, hook http.Handler) {
123
124
124
125
// instrumentedHook adds some instrumentation on top of the given webhook.
125
126
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
+ )
133
140
}
134
141
135
142
// Start runs the server.
0 commit comments