forked from bitnami-labs/sealed-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
121 lines (108 loc) · 3.78 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
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
package main
import (
"net/http"
"github.com/bitnami-labs/sealed-secrets/pkg/apis/sealed-secrets/v1alpha1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
v1 "k8s.io/api/core/v1"
)
// Define Prometheus Exporter namespace (prefix) for all metric names
const metricNamespace string = "sealed_secrets_controller"
const (
labelNamespace = "namespace"
labelName = "name"
labelCondition = "condition"
)
var conditionStatusToGaugeValue = map[v1.ConditionStatus]float64{
v1.ConditionFalse: -1,
v1.ConditionUnknown: 0,
v1.ConditionTrue: 1,
}
// Define Prometheus metrics to expose
var (
buildInfo = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: metricNamespace,
Name: "build_info",
Help: "Build information.",
ConstLabels: prometheus.Labels{"revision": VERSION},
},
)
// TODO: rename metric, change increment logic, or accept behaviour
// when a SealedSecret is deleted the unseal() function is called which is
// not technically an 'unseal request'.
unsealRequestsTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "unseal_requests_total",
Help: "Total number of sealed secret unseal requests",
},
)
unsealErrorsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "unseal_errors_total",
Help: "Total number of sealed secret unseal errors by reason",
},
[]string{"reason", "namespace"},
)
conditionInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricNamespace,
Name: "condition_info",
Help: "Current SealedSecret condition status. Values are -1 (false), 0 (unknown or absent), 1 (true)",
}, []string{labelNamespace, labelName, labelCondition})
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "http_requests_total",
Help: "A counter for requests to the wrapped handler.",
},
[]string{"path", "code", "method"},
)
httpRequestDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: metricNamespace,
Name: "http_request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: prometheus.DefBuckets,
},
[]string{"path", "method"},
)
)
func init() {
// Register metrics with Prometheus
prometheus.MustRegister(buildInfo)
prometheus.MustRegister(prometheus.NewBuildInfoCollector())
prometheus.MustRegister(unsealRequestsTotal)
prometheus.MustRegister(unsealErrorsTotal)
prometheus.MustRegister(conditionInfo)
prometheus.MustRegister(httpRequestsTotal)
prometheus.MustRegister(httpRequestDurationSeconds)
}
// ObserveCondition sets a `condition_info` Gauge according to a SealedSecret status.
func ObserveCondition(ssecret *v1alpha1.SealedSecret) {
if ssecret.Status == nil {
return
}
for _, condition := range ssecret.Status.Conditions {
conditionInfo.With(prometheus.Labels{
labelNamespace: ssecret.Namespace,
labelName: ssecret.Name,
labelCondition: string(condition.Type),
}).Set(conditionStatusToGaugeValue[condition.Status])
}
}
// UnregisterCondition unregisters Gauges associated to a SealedSecret conditions.
func UnregisterCondition(ssecret *v1alpha1.SealedSecret) {
if ssecret.Status == nil {
return
}
for _, condition := range ssecret.Status.Conditions {
conditionInfo.MetricVec.DeleteLabelValues(ssecret.Namespace, ssecret.Name, string(condition.Type))
}
}
// Instrument HTTP handler
func Instrument(path string, h http.Handler) http.Handler {
return promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(prometheus.Labels{"path": path}),
promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(prometheus.Labels{"path": path}), h))
}