Skip to content

Commit a68f4a2

Browse files
committed
fix: run collectMetrics func in a dedicated goroutine
1 parent 44f9c9d commit a68f4a2

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

internal/servicecheck/httptrace.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
4040
// withHttptrace collects traces, measures durations and counts requests+errors.
4141
func withHttptrace(next http.RoundTripper, histogramGetter func(string) Histogram) http.RoundTripper {
4242
collectMetric := func(traceEventType string, start time.Time, r *http.Request, err error) {
43-
kubenurseTypeLabel := r.Context().Value(kubenurseTypeKey{}).(string)
44-
errorAccounted := r.Context().Value(kubenurseErrorAccountedKey{}).(*atomic.Bool)
45-
l := []string{"type", kubenurseTypeLabel, "event", traceEventType}
46-
47-
// If we get an error inside a trace, log it
48-
if err != nil {
49-
metrics.GetOrCreateCounter(util.GenMetricsName(errCounter, l...)).Inc()
50-
errorAccounted.Store(true) // mark the error as accounted, so we don't increase the error counter twice.
51-
slog.Error("request failure in httptrace", "event_type", traceEventType, "request_type", kubenurseTypeLabel, "err", err)
52-
53-
return
54-
}
43+
go func() { // we run the following in a separate goroutine, because the ClientTrace functions are called in a blocking manner
44+
kubenurseTypeLabel := r.Context().Value(kubenurseTypeKey{}).(string)
45+
errorAccounted := r.Context().Value(kubenurseErrorAccountedKey{}).(*atomic.Bool)
46+
l := []string{"type", kubenurseTypeLabel, "event", traceEventType}
47+
48+
// If we get an error inside a trace, log it
49+
if err != nil {
50+
metrics.GetOrCreateCounter(util.GenMetricsName(errCounter, l...)).Inc()
51+
errorAccounted.Store(true) // mark the error as accounted, so we don't increase the error counter twice.
52+
slog.Error("request failure in httptrace", "event_type", traceEventType, "request_type", kubenurseTypeLabel, "err", err)
53+
54+
return
55+
}
5556

56-
histogramGetter(util.GenMetricsName(hcTraceReqDurSec, l...)).UpdateDuration(start)
57+
histogramGetter(util.GenMetricsName(hcTraceReqDurSec, l...)).UpdateDuration(start)
58+
}()
5759
}
5860

5961
// Return a http.RoundTripper for tracing requests

0 commit comments

Comments
 (0)