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 }) }