From cdcc0633a3a084ed17f29dd9d37d6d8109c87911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Nussbaumer?= Date: Thu, 14 Mar 2024 15:34:32 +0100 Subject: [PATCH] feat: add request type to httptrace and request duration metrics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Clément Nussbaumer --- internal/servicecheck/httptrace.go | 23 ++++++++++------------- internal/servicecheck/servicecheck.go | 2 +- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/internal/servicecheck/httptrace.go b/internal/servicecheck/httptrace.go index f47ef82f..942aca93 100644 --- a/internal/servicecheck/httptrace.go +++ b/internal/servicecheck/httptrace.go @@ -1,6 +1,7 @@ package servicecheck import ( + "context" "crypto/tls" "log" "net/http" @@ -30,8 +31,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durati Name: "httpclient_requests_total", Help: "A counter for requests from the kubenurse http client.", }, - // []string{"code", "method", "type"}, // TODO - []string{"code", "method"}, + []string{"code", "method", "type"}, ) httpclientReqDuration := prometheus.NewHistogramVec( @@ -41,8 +41,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durati Help: "A latency histogram of request latencies from the kubenurse http client.", Buckets: durationHistogram, }, - // []string{"type"}, // TODO - []string{}, + []string{"type"}, ) httpclientTraceReqDuration := prometheus.NewHistogramVec( @@ -52,8 +51,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durati Help: "Latency histogram for requests from the kubenurse http client. Time in seconds since the start of the http request.", Buckets: durationHistogram, }, - []string{"event"}, - // []string{"event", "type"}, // TODO + []string{"event", "type"}, ) registry.MustRegister(httpclientReqTotal, httpclientReqDuration, httpclientTraceReqDuration) @@ -68,7 +66,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durati return } - httpclientTraceReqDuration.WithLabelValues(traceEventType).Observe(td) // TODO: add back kubenurseTypeKey + httpclientTraceReqDuration.WithLabelValues(traceEventType, kubenurseTypeLabel).Observe(td) } // Return a http.RoundTripper for tracing requests @@ -110,14 +108,13 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durati // Do request with tracing enabled r = r.WithContext(httptrace.WithClientTrace(r.Context(), trace)) - // // TODO: uncomment when issue #55 is solved (N^2 request will increase cardinality of path_ metrics too much otherwise) - // typeFromCtxFn := promhttp.WithLabelFromCtx("type", func(ctx context.Context) string { - // return ctx.Value(kubenurseTypeKey{}).(string) - // }) + typeFromCtxFn := promhttp.WithLabelFromCtx("type", func(ctx context.Context) string { + return ctx.Value(kubenurseTypeKey{}).(string) + }) rt := next // variable pinning :) essential, to prevent always re-instrumenting the original variable - rt = promhttp.InstrumentRoundTripperCounter(httpclientReqTotal, rt) - rt = promhttp.InstrumentRoundTripperDuration(httpclientReqDuration, rt) + rt = promhttp.InstrumentRoundTripperCounter(httpclientReqTotal, rt, typeFromCtxFn) + rt = promhttp.InstrumentRoundTripperDuration(httpclientReqDuration, rt, typeFromCtxFn) return rt.RoundTrip(r) }) diff --git a/internal/servicecheck/servicecheck.go b/internal/servicecheck/servicecheck.go index eee65e37..dfb176f9 100644 --- a/internal/servicecheck/servicecheck.go +++ b/internal/servicecheck/servicecheck.go @@ -114,7 +114,7 @@ func (c *Checker) Run() (Result, bool) { if c.SkipCheckNeighbourhood { res.NeighbourhoodState = skippedStr } else { - res.Neighbourhood, err = c.GetNeighbours(context.TODO(), c.KubenurseNamespace, c.NeighbourFilter) + res.Neighbourhood, err = c.GetNeighbours(context.Background(), c.KubenurseNamespace, c.NeighbourFilter) haserr = haserr || (err != nil) // Neighbourhood special error treating