Skip to content

Commit

Permalink
feat: add request type to httptrace and request duration metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Nussbaumer <[email protected]>
  • Loading branch information
clementnuss committed Mar 14, 2024
1 parent bd1ee9f commit cdcc063
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions internal/servicecheck/httptrace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package servicecheck

import (
"context"
"crypto/tls"
"log"
"net/http"
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/servicecheck/servicecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cdcc063

Please sign in to comment.