@@ -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,23 @@ 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
+ // Initialize the most likely HTTP status codes.
134
+ cnt .WithLabelValues ("200" )
135
+ cnt .WithLabelValues ("500" )
136
+
137
+ return promhttp .InstrumentHandlerDuration (
138
+ lat ,
139
+ promhttp .InstrumentHandlerCounter (
140
+ cnt ,
141
+ promhttp .InstrumentHandlerInFlight (gge , hookRaw ),
142
+ ),
143
+ )
133
144
}
134
145
135
146
// Start runs the server.
0 commit comments