Skip to content

Commit 0d89471

Browse files
fengruotjtanjie.master
authored andcommitted
Support RateLimiterLatency & RequestLatency for client-go metrics (client, workqueue, reflector)
Signed-off-by: tanjie.master <[email protected]>
1 parent aa78068 commit 0d89471

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

pkg/metrics/client_go_adapter.go

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

1919
import (
2020
"context"
21+
"net/url"
22+
"time"
2123

2224
"github.com/prometheus/client_golang/prometheus"
2325
clientmetrics "k8s.io/client-go/tools/metrics"
@@ -30,6 +32,26 @@ import (
3032
var (
3133
// client metrics.
3234

35+
requestLatency = prometheus.NewHistogramVec(
36+
prometheus.HistogramOpts{
37+
Name: "rest_client_request_duration_seconds",
38+
Help: "Request latency in seconds. Broken down by verb and URL.",
39+
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,
40+
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},
41+
},
42+
[]string{"verb", "host", "path"},
43+
)
44+
45+
rateLimiterLatency = prometheus.NewHistogramVec(
46+
prometheus.HistogramOpts{
47+
Name: "rest_client_rate_limiter_duration_seconds",
48+
Help: "Client side rate limiter latency in seconds. Broken down by verb and URL.",
49+
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,
50+
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},
51+
},
52+
[]string{"verb", "host", "path"},
53+
)
54+
3355
requestResult = prometheus.NewCounterVec(
3456
prometheus.CounterOpts{
3557
Name: "rest_client_requests_total",
@@ -47,10 +69,14 @@ func init() {
4769
func registerClientMetrics() {
4870
// register the metrics with our registry
4971
Registry.MustRegister(requestResult)
72+
Registry.MustRegister(rateLimiterLatency)
73+
Registry.MustRegister(requestLatency)
5074

5175
// register the metrics with client-go
5276
clientmetrics.Register(clientmetrics.RegisterOpts{
53-
RequestResult: &resultAdapter{metric: requestResult},
77+
RequestResult: &resultAdapter{metric: requestResult},
78+
RateLimiterLatency: &latencyAdapter{metric: rateLimiterLatency},
79+
RequestLatency: &latencyAdapter{metric: requestLatency},
5480
})
5581
}
5682

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

0 commit comments

Comments
 (0)