-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
metrics.go
69 lines (54 loc) · 2.39 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/cilium/cilium/operator/metrics"
)
// PrometheusMetrics is an implementation of Prometheus metrics for external
// API usage
type PrometheusMetrics struct {
registry metrics.RegisterGatherer
APIDuration *prometheus.HistogramVec
RateLimit *prometheus.HistogramVec
}
// NewPrometheusMetrics returns a new metrics tracking implementation to cover
// external API usage.
func NewPrometheusMetrics(namespace, subsystem string, registry metrics.RegisterGatherer) *PrometheusMetrics {
m := &PrometheusMetrics{registry: registry}
m.APIDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "api_duration_seconds",
Help: "Duration of interactions with API",
}, []string{"operation", "response_code"})
m.RateLimit = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "api_rate_limit_duration_seconds",
Help: "Duration of client-side rate limiter blocking",
}, []string{"operation"})
registry.MustRegister(m.APIDuration)
registry.MustRegister(m.RateLimit)
return m
}
// ObserveAPICall must be called on every API call made with the operation
// performed, the status code received and the duration of the call
func (p *PrometheusMetrics) ObserveAPICall(operation, status string, duration float64) {
p.APIDuration.WithLabelValues(operation, status).Observe(duration)
}
// ObserveRateLimit must be called in case an API call was subject to rate limiting
func (p *PrometheusMetrics) ObserveRateLimit(operation string, delay time.Duration) {
p.RateLimit.WithLabelValues(operation).Observe(delay.Seconds())
}
// NoOpMetrics is a no-op implementation
type NoOpMetrics struct{}
// ObserveAPICall must be called on every API call made with the operation
// performed, the status code received and the duration of the call. This No-op
// implementation will perform no metrics accounting in return.
func (m *NoOpMetrics) ObserveAPICall(call, status string, duration float64) {}
// ObserveRateLimit must be called in case an API call was subject to rate
// limiting. This No-op implementation will perform no metrics accounting in
// return.
func (m *NoOpMetrics) ObserveRateLimit(operation string, duration time.Duration) {}