Skip to content

Commit ecaffdc

Browse files
author
tanjie.master
committed
Support RateLimiterLatency & RequestLatency for client-go metrics (client, workqueue, reflector)
1 parent aa78068 commit ecaffdc

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

pkg/metrics/client_go_adapter.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package metrics
1818

1919
import (
2020
"context"
21+
"github.com/prometheus/client_golang/prometheus/promauto"
22+
"net/url"
23+
"time"
2124

2225
"github.com/prometheus/client_golang/prometheus"
2326
clientmetrics "k8s.io/client-go/tools/metrics"
@@ -30,6 +33,26 @@ import (
3033
var (
3134
// client metrics.
3235

36+
requestLatency = promauto.NewHistogramVec(
37+
prometheus.HistogramOpts{
38+
Name: "rest_client_request_duration_seconds",
39+
Help: "Request latency in seconds. Broken down by verb and URL.",
40+
Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
41+
1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 60},
42+
},
43+
[]string{"verb", "host", "path"},
44+
)
45+
46+
rateLimiterLatency = promauto.NewHistogramVec(
47+
prometheus.HistogramOpts{
48+
Name: "rest_client_rate_limiter_duration_seconds",
49+
Help: "Client side rate limiter latency in seconds. Broken down by verb and URL.",
50+
Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
51+
1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 60},
52+
},
53+
[]string{"verb", "host", "path"},
54+
)
55+
3356
requestResult = prometheus.NewCounterVec(
3457
prometheus.CounterOpts{
3558
Name: "rest_client_requests_total",
@@ -50,7 +73,9 @@ func registerClientMetrics() {
5073

5174
// register the metrics with client-go
5275
clientmetrics.Register(clientmetrics.RegisterOpts{
53-
RequestResult: &resultAdapter{metric: requestResult},
76+
RequestResult: &resultAdapter{metric: requestResult},
77+
RateLimiterLatency: &latencyAdapter{metric: rateLimiterLatency},
78+
RequestLatency: &latencyAdapter{metric: requestLatency},
5479
})
5580
}
5681

@@ -69,3 +94,11 @@ type resultAdapter struct {
6994
func (r *resultAdapter) Increment(_ context.Context, code, method, host string) {
7095
r.metric.WithLabelValues(code, method, host).Inc()
7196
}
97+
98+
type latencyAdapter struct {
99+
metric *prometheus.HistogramVec
100+
}
101+
102+
func (l *latencyAdapter) Observe(_ context.Context, verb string, u url.URL, latency time.Duration) {
103+
l.metric.WithLabelValues(verb, u.Host, u.Path).Observe(latency.Seconds())
104+
}

0 commit comments

Comments
 (0)