Skip to content

Commit c7a8fb2

Browse files
committed
Add rest client request duration and size metrics
Signed-off-by: Stefan Büringer [email protected]
1 parent f6f37e6 commit c7a8fb2

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

pkg/metrics/client_go_adapter.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
const (
3434
RestClientSubsystem = "rest_client"
3535
LatencyKey = "request_latency_seconds"
36-
ResultKey = "requests_total"
3736
)
3837

3938
var (
@@ -62,11 +61,44 @@ var (
6261
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
6362
}, []string{"verb", "url"})
6463

65-
requestResult = prometheus.NewCounterVec(prometheus.CounterOpts{
66-
Subsystem: RestClientSubsystem,
67-
Name: ResultKey,
68-
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
69-
}, []string{"code", "method", "host"})
64+
// requestLatency is a Prometheus Histogram metric type partitioned by
65+
// "verb", and "host" labels. It is used for the rest client latency metrics.
66+
requestLatency = prometheus.NewHistogramVec(
67+
prometheus.HistogramOpts{
68+
Name: "rest_client_request_duration_seconds",
69+
Help: "Request latency in seconds. Broken down by verb, and host.",
70+
Buckets: []float64{0.005, 0.025, 0.1, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0, 15.0, 30.0, 60.0},
71+
},
72+
[]string{"verb", "host"},
73+
)
74+
75+
requestSize = prometheus.NewHistogramVec(
76+
prometheus.HistogramOpts{
77+
Name: "rest_client_request_size_bytes",
78+
Help: "Request size in bytes. Broken down by verb and host.",
79+
// 64 bytes to 16MB
80+
Buckets: []float64{64, 256, 512, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216},
81+
},
82+
[]string{"verb", "host"},
83+
)
84+
85+
responseSize = prometheus.NewHistogramVec(
86+
prometheus.HistogramOpts{
87+
Name: "rest_client_response_size_bytes",
88+
Help: "Response size in bytes. Broken down by verb and host.",
89+
// 64 bytes to 16MB
90+
Buckets: []float64{64, 256, 512, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216},
91+
},
92+
[]string{"verb", "host"},
93+
)
94+
95+
requestResult = prometheus.NewCounterVec(
96+
prometheus.CounterOpts{
97+
Name: "rest_client_requests_total",
98+
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
99+
},
100+
[]string{"code", "method", "host"},
101+
)
70102
)
71103

72104
func init() {
@@ -76,11 +108,17 @@ func init() {
76108
// registerClientMetrics sets up the client latency metrics from client-go.
77109
func registerClientMetrics() {
78110
// register the metrics with our registry
111+
Registry.MustRegister(requestLatency)
112+
Registry.MustRegister(requestSize)
113+
Registry.MustRegister(responseSize)
79114
Registry.MustRegister(requestResult)
80115

81116
// register the metrics with client-go
82117
clientmetrics.Register(clientmetrics.RegisterOpts{
83-
RequestResult: &resultAdapter{metric: requestResult},
118+
RequestLatency: &LatencyAdapter{metric: requestLatency},
119+
RequestSize: &sizeAdapter{metric: requestSize},
120+
ResponseSize: &sizeAdapter{metric: responseSize},
121+
RequestResult: &resultAdapter{metric: requestResult},
84122
})
85123
}
86124

@@ -102,6 +140,14 @@ func (l *LatencyAdapter) Observe(_ context.Context, verb string, u url.URL, late
102140
l.metric.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
103141
}
104142

143+
type sizeAdapter struct {
144+
metric *prometheus.HistogramVec
145+
}
146+
147+
func (s *sizeAdapter) Observe(ctx context.Context, verb string, host string, size float64) {
148+
s.metric.WithLabelValues(verb, host).Observe(size)
149+
}
150+
105151
type resultAdapter struct {
106152
metric *prometheus.CounterVec
107153
}

0 commit comments

Comments
 (0)