From b403dddabbcd5ee30a532c05a735754cb7c95044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Nussbaumer?= Date: Mon, 3 Jun 2024 08:37:56 +0200 Subject: [PATCH] fix: statusCode != 200 trigger an error and increase error counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes https://github.com/postfinance/kubenurse/issues/138 Signed-off-by: Clément Nussbaumer --- internal/servicecheck/httptrace.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/servicecheck/httptrace.go b/internal/servicecheck/httptrace.go index 2b1a7df..f107684 100644 --- a/internal/servicecheck/httptrace.go +++ b/internal/servicecheck/httptrace.go @@ -3,6 +3,7 @@ package servicecheck import ( "context" "crypto/tls" + "fmt" "log/slog" "net/http" "net/http/httptrace" @@ -129,6 +130,26 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durHis rt = promhttp.InstrumentRoundTripperCounter(httpclientReqTotal, rt, typeFromCtxFn) rt = promhttp.InstrumentRoundTripperDuration(httpclientReqDuration, rt, typeFromCtxFn) - return rt.RoundTrip(r) + kubenurseRequestType := r.Context().Value(kubenurseTypeKey{}).(string) + resp, err := rt.RoundTrip(r) + + if err == nil { + if resp.StatusCode != http.StatusOK { + eventType := fmt.Sprintf("status_code_%d", resp.StatusCode) + + errorCounter.WithLabelValues(eventType, kubenurseRequestType).Inc() + slog.Error("request failure in httptrace", + "event_type", eventType, + "request_type", kubenurseRequestType) + } + } else { + eventType := "round_trip_error" + errorCounter.WithLabelValues(eventType, kubenurseRequestType).Inc() + slog.Error("request failure in httptrace", + "event_type", eventType, + "request_type", kubenurseRequestType, "err", err) + } + + return resp, err }) }