Skip to content

Commit

Permalink
fix: use same histogram buckets everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Nussbaumer <[email protected]>
  • Loading branch information
clementnuss committed Jan 22, 2024
1 parent 330d2d4 commit 03505e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
14 changes: 5 additions & 9 deletions internal/servicecheck/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@ import (
// unique type for context.Context to avoid collisions.
type kubenurseTypeKey struct{}

// http.RoundTripper
// TODO: Easier method to get a round tripper?
// // http.RoundTripper
type RoundTripperFunc func(req *http.Request) (*http.Response, error)

func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
}

// Ensure RoundTripperFunc is a http.RoundTripper
var _ http.RoundTripper = (*RoundTripperFunc)(nil)

// This collects traces and logs errors. As promhttp.InstrumentRoundTripperTrace doesn't process
// errors, this is custom made and inspired by prometheus/client_golang's promhttp
func withHttptrace(registry *prometheus.Registry, next http.RoundTripper) http.RoundTripper {
func withHttptrace(registry *prometheus.Registry, next http.RoundTripper, durationHistogram []float64) http.RoundTripper {
httpclientReqTotal := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricsNamespace,
Expand All @@ -43,7 +39,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper) http.R
Namespace: metricsNamespace,
Name: "httpclient_request_duration_seconds",
Help: "A latency histogram of request latencies from the kubenurse http client.",
Buckets: prometheus.DefBuckets,
Buckets: durationHistogram,
},
// []string{"type"}, // TODO
[]string{},
Expand All @@ -54,7 +50,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper) http.R
Namespace: metricsNamespace,
Name: "httpclient_trace_request_duration_seconds",
Help: "Latency histogram for requests from the kubenurse http client. Time in seconds since the start of the http request.",
Buckets: []float64{.0005, .005, .01, .025, .05, .1, .25, .5, 1}, // TODO: Which buckets are really needed?
Buckets: durationHistogram,
},
[]string{"event"},
// []string{"event", "type"}, // TODO
Expand Down Expand Up @@ -119,7 +115,7 @@ func withHttptrace(registry *prometheus.Registry, next http.RoundTripper) http.R
// return ctx.Value(kubenurseTypeKey{}).(string)
// })

rt := next
rt := next // variable pinning :) essential, to prevent always re-instrumenting the original variable
rt = promhttp.InstrumentRoundTripperCounter(httpclientReqTotal, rt)
rt = promhttp.InstrumentRoundTripperDuration(httpclientReqDuration, rt)
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 @@ -75,7 +75,7 @@ func New(_ context.Context, discovery *kubediscovery.Client, promRegistry *prome

httpClient := &http.Client{
Timeout: 5 * time.Second,
Transport: withHttptrace(promRegistry, transport),
Transport: withHttptrace(promRegistry, transport, durationHistogramBuckets),
}

return &Checker{
Expand Down

0 comments on commit 03505e9

Please sign in to comment.