/
prometheus.go
136 lines (117 loc) · 4.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package prometheus
import (
"context"
"fmt"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/slok/kubewebhook/v2/pkg/webhook"
)
const (
prefix = "kubewebhook"
)
// RecorderConfig is the configuration of the recorder.
type RecorderConfig struct {
Registry prometheus.Registerer
ReviewOpBuckets []float64
}
func (c *RecorderConfig) defaults() error {
if c.Registry == nil {
c.Registry = prometheus.DefaultRegisterer
}
if c.ReviewOpBuckets == nil {
c.ReviewOpBuckets = prometheus.DefBuckets
}
return nil
}
// Recorder knows how to measure the metrics of the library using Prometheus
// as the backend for the measurements.
type Recorder struct {
webhookValReviewDuration *prometheus.HistogramVec
webhookMutReviewDuration *prometheus.HistogramVec
webhookReviewWarnings *prometheus.CounterVec
}
// NewRecorder returns a new Prometheus metrics recorder.
func NewRecorder(config RecorderConfig) (*Recorder, error) {
err := config.defaults()
if err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
r := &Recorder{
webhookValReviewDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: prefix,
Subsystem: "validating_webhook",
Name: "review_duration_seconds",
Help: "The duration of the admission review handled by a validating webhook.",
Buckets: config.ReviewOpBuckets,
}, []string{"webhook_id", "webhook_version", "resource_namespace", "resource_kind", "operation", "dry_run", "success", "allowed"}),
webhookMutReviewDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: prefix,
Subsystem: "mutating_webhook",
Name: "review_duration_seconds",
Help: "The duration of the admission review handled by a mutating webhook.",
Buckets: config.ReviewOpBuckets,
}, []string{"webhook_id", "webhook_version", "resource_namespace", "resource_kind", "operation", "dry_run", "success", "mutated"}),
webhookReviewWarnings: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Subsystem: "webhook",
Name: "review_warnings_total",
Help: "The total number warnings the webhooks are returning on the review process.",
}, []string{"webhook_id", "webhook_version", "resource_namespace", "resource_kind", "operation", "dry_run", "success"}),
}
// Register our metrics on the received recorder.
config.Registry.MustRegister(
r.webhookValReviewDuration,
r.webhookMutReviewDuration,
r.webhookReviewWarnings,
)
return r, nil
}
var _ webhook.MetricsRecorder = Recorder{}
// MeasureValidatingWebhookReviewOp measures a validating webhook review operation on Prometheus.
func (r Recorder) MeasureValidatingWebhookReviewOp(_ context.Context, data webhook.MeasureValidatingOpData) {
// Measure Operation.
r.webhookValReviewDuration.With(prometheus.Labels{
"webhook_id": data.WebhookID,
"webhook_version": data.AdmissionReviewVersion,
"resource_namespace": data.ResourceNamespace,
"resource_kind": data.ResourceKind,
"operation": data.Operation,
"dry_run": strconv.FormatBool(data.DryRun),
"success": strconv.FormatBool(data.Success),
"allowed": strconv.FormatBool(data.Allowed),
}).Observe(data.Duration.Seconds())
// Measure warnings.
r.webhookReviewWarnings.With(prometheus.Labels{
"webhook_id": data.WebhookID,
"webhook_version": data.AdmissionReviewVersion,
"resource_namespace": data.ResourceNamespace,
"resource_kind": data.ResourceKind,
"operation": data.Operation,
"dry_run": strconv.FormatBool(data.DryRun),
"success": strconv.FormatBool(data.Success),
}).Add(float64(data.WarningsNumber))
}
// MeasureMutatingWebhookReviewOp measures a mutating webhook review operation on Prometheus.
func (r Recorder) MeasureMutatingWebhookReviewOp(_ context.Context, data webhook.MeasureMutatingOpData) {
// Measure operation.
r.webhookMutReviewDuration.With(prometheus.Labels{
"webhook_id": data.WebhookID,
"webhook_version": data.AdmissionReviewVersion,
"resource_namespace": data.ResourceNamespace,
"resource_kind": data.ResourceKind,
"operation": data.Operation,
"dry_run": strconv.FormatBool(data.DryRun),
"success": strconv.FormatBool(data.Success),
"mutated": strconv.FormatBool(data.Mutated),
}).Observe(data.Duration.Seconds())
// Measure warnings.
r.webhookReviewWarnings.With(prometheus.Labels{
"webhook_id": data.WebhookID,
"webhook_version": data.AdmissionReviewVersion,
"resource_namespace": data.ResourceNamespace,
"resource_kind": data.ResourceKind,
"operation": data.Operation,
"dry_run": strconv.FormatBool(data.DryRun),
"success": strconv.FormatBool(data.Success),
}).Add(float64(data.WarningsNumber))
}