-
Notifications
You must be signed in to change notification settings - Fork 73
/
prometheus.go
119 lines (102 loc) · 3.54 KB
/
prometheus.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package metrics
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
promNamespace = "kubewebhook"
promWebhookSubsystem = "admission_webhook"
)
// Prometheus is the implementation of a metrics Recorder for
// Prometheus system.
type Prometheus struct {
// Metrics.
admissionReview *prometheus.CounterVec
admissionReviewErr *prometheus.CounterVec
admissionReviewDuration *prometheus.HistogramVec
// Validation Metrics
validationReviewResult *prometheus.CounterVec
reg prometheus.Registerer
}
// NewPrometheus returns a new Prometheus metrics backend.
func NewPrometheus(registry prometheus.Registerer) *Prometheus {
p := &Prometheus{
reg: registry,
admissionReview: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promWebhookSubsystem,
Name: "admission_reviews_total",
Help: "Total number of admission reviews handled.",
}, []string{"webhook", "namespace", "resource", "operation", "kind"}),
admissionReviewErr: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promWebhookSubsystem,
Name: "admission_review_errors_total",
Help: "Total number of admission review errors when handling.",
}, []string{"webhook", "namespace", "resource", "operation", "kind"}),
admissionReviewDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: promNamespace,
Subsystem: promWebhookSubsystem,
Name: "admission_review_duration_seconds",
Help: "The duration of the admission review.",
}, []string{"webhook", "namespace", "resource", "operation", "kind"}),
validationReviewResult: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promWebhookSubsystem,
Name: "validation_review_results_total",
Help: "Total number of validation reviews",
}, []string{"webhook", "namespace", "resource", "operation", "allowed"}),
}
p.registerMetrics()
return p
}
func (p *Prometheus) registerMetrics() {
p.reg.MustRegister(
p.admissionReview,
p.admissionReviewErr,
p.admissionReviewDuration,
p.validationReviewResult,
)
}
// IncAdmissionReview satisfies Recorder interface.
func (p *Prometheus) IncAdmissionReview(webhook, namespace, resource string, operation Operation, kind ReviewKind) {
p.admissionReview.WithLabelValues(
webhook,
namespace,
string(resource),
string(operation),
string(kind)).Inc()
}
// IncAdmissionReviewError satisfies Recorder interface.
func (p *Prometheus) IncAdmissionReviewError(webhook, namespace, resource string, operation Operation, kind ReviewKind) {
p.admissionReviewErr.WithLabelValues(
webhook,
namespace,
string(resource),
string(operation),
string(kind)).Inc()
}
// ObserveAdmissionReviewDuration satisfies Recorder interface.
func (p *Prometheus) ObserveAdmissionReviewDuration(webhook, namespace, resource string, operation Operation, kind ReviewKind, start time.Time) {
secs := p.getDuration(start).Seconds()
p.admissionReviewDuration.WithLabelValues(
webhook,
namespace,
string(resource),
string(operation),
string(kind)).Observe(secs)
}
// IncValidationReviewResult satisfies Recorder interface.
func (p *Prometheus) IncValidationReviewResult(webhook, namespace, resource string, operation Operation, allowed bool) {
p.validationReviewResult.WithLabelValues(
webhook,
namespace,
string(resource),
string(operation),
strconv.FormatBool(allowed),
).Inc()
}
func (p *Prometheus) getDuration(start time.Time) time.Duration {
return time.Since(start)
}