Skip to content

Commit

Permalink
fix: statusCode != 200 trigger an error and increase error counter
Browse files Browse the repository at this point in the history
closes #138

Signed-off-by: Clément Nussbaumer <clement.nussbaumer@postfinance.ch>
  • Loading branch information
clementnuss committed Jun 3, 2024
1 parent 277d047 commit b403ddd
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion internal/servicecheck/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package servicecheck
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net/http"
"net/http/httptrace"
Expand Down Expand Up @@ -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
})
}

0 comments on commit b403ddd

Please sign in to comment.