Skip to content

Commit 12c19f7

Browse files
committed
move InstrumentedHook to metrics package
1 parent 2c24442 commit 12c19f7

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

pkg/webhook/admission/webhook.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"net/http"
2323

2424
"github.com/go-logr/logr"
25-
"github.com/prometheus/client_golang/prometheus"
26-
"github.com/prometheus/client_golang/prometheus/promhttp"
2725
jsonpatch "gomodules.xyz/jsonpatch/v2"
2826
admissionv1 "k8s.io/api/admission/v1"
2927
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -209,27 +207,6 @@ func (w *Webhook) InjectFunc(f inject.Func) error {
209207
return setFields(w.Handler)
210208
}
211209

212-
// InstrumentedHook adds some instrumentation on top of the given webhook.
213-
func InstrumentedHook(path string, hookRaw http.Handler) http.Handler {
214-
lbl := prometheus.Labels{"webhook": path}
215-
216-
lat := metrics.RequestLatency.MustCurryWith(lbl)
217-
cnt := metrics.RequestTotal.MustCurryWith(lbl)
218-
gge := metrics.RequestInFlight.With(lbl)
219-
220-
// Initialize the most likely HTTP status codes.
221-
cnt.WithLabelValues("200")
222-
cnt.WithLabelValues("500")
223-
224-
return promhttp.InstrumentHandlerDuration(
225-
lat,
226-
promhttp.InstrumentHandlerCounter(
227-
cnt,
228-
promhttp.InstrumentHandlerInFlight(gge, hookRaw),
229-
),
230-
)
231-
}
232-
233210
// StandaloneOptions let you configure a StandaloneWebhook.
234211
type StandaloneOptions struct {
235212
// Scheme is the scheme used to resolve runtime.Objects to GroupVersionKinds / Resources
@@ -265,5 +242,5 @@ func StandaloneWebhook(hook *Webhook, opts StandaloneOptions) (http.Handler, err
265242
if opts.Path == "" {
266243
return hook, nil
267244
}
268-
return InstrumentedHook(opts.Path, hook), nil
245+
return metrics.InstrumentedHook(opts.Path, hook), nil
269246
}

pkg/webhook/internal/metrics/metrics.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package metrics
1818

1919
import (
20+
"net/http"
21+
2022
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/promhttp"
2124

2225
"sigs.k8s.io/controller-runtime/pkg/metrics"
2326
)
@@ -59,3 +62,24 @@ var (
5962
func init() {
6063
metrics.Registry.MustRegister(RequestLatency, RequestTotal, RequestInFlight)
6164
}
65+
66+
// InstrumentedHook adds some instrumentation on top of the given webhook.
67+
func InstrumentedHook(path string, hookRaw http.Handler) http.Handler {
68+
lbl := prometheus.Labels{"webhook": path}
69+
70+
lat := RequestLatency.MustCurryWith(lbl)
71+
cnt := RequestTotal.MustCurryWith(lbl)
72+
gge := RequestInFlight.With(lbl)
73+
74+
// Initialize the most likely HTTP status codes.
75+
cnt.WithLabelValues("200")
76+
cnt.WithLabelValues("500")
77+
78+
return promhttp.InstrumentHandlerDuration(
79+
lat,
80+
promhttp.InstrumentHandlerCounter(
81+
cnt,
82+
promhttp.InstrumentHandlerInFlight(gge, hookRaw),
83+
),
84+
)
85+
}

0 commit comments

Comments
 (0)