-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
104 lines (85 loc) · 2.75 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
package main
import (
"net/http"
"path"
"sync"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type (
// prometheusHandler is a wrapper around a Prometheus GaugeVec
prometheusHandler struct {
expiresInDaysMetric *prometheus.GaugeVec
keyRegistry map[string][]string
lock sync.RWMutex
}
)
func newPrometheusHandler(metricsPrefix string) (*prometheusHandler, error) {
expiresInDaysMetric := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Help: "Number of hours the certificate will expire in",
Name: "expires_in",
Namespace: metricsPrefix,
},
[]string{"namespace", "name", "key"},
)
if err := prometheus.Register(expiresInDaysMetric); err != nil {
return nil, errors.Wrap(err, "registering expires_in metric")
}
return &prometheusHandler{
expiresInDaysMetric: expiresInDaysMetric,
keyRegistry: make(map[string][]string),
}, nil
}
// AddHandler installs the HTTP listener in the default HTTP mux
func (p *prometheusHandler) AddHandler() {
http.Handle("/metrics", promhttp.Handler())
}
// RemoveCertExpiry removes all metrics belonging to the given secret in given namespace
func (p *prometheusHandler) RemoveCertExpiry(namespace, name string) {
for _, key := range p.getKeys(namespace, name) {
p.expiresInDaysMetric.Delete(prometheus.Labels{
"namespace": namespace,
"name": name,
"key": key,
})
}
p.clearKeys(namespace, name)
}
// SetCertExpiry adds or updates the expiry for the given key in namespace/name combination
func (p *prometheusHandler) SetCertExpiry(namespace, name, key string, expiresIn time.Duration) {
p.expiresInDaysMetric.With(prometheus.Labels{
"namespace": namespace,
"name": name,
"key": key,
}).Set(float64(expiresIn) / float64(time.Hour))
p.registerKey(namespace, name, key)
}
// clearKeys removes the keys for the given secret namespace/name
func (p *prometheusHandler) clearKeys(namespace, name string) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.keyRegistry, path.Join(namespace, name))
}
// getKeys returns the known keys within the secret in namespace/name combination
func (p *prometheusHandler) getKeys(namespace, name string) []string {
p.lock.RLock()
defer p.lock.RUnlock()
return p.keyRegistry[path.Join(namespace, name)]
}
// registerKey registers a new key for the given secret in namespace/name combination for later removal of the metric
func (p *prometheusHandler) registerKey(namespace, name, key string) {
p.lock.Lock()
defer p.lock.Unlock()
for _, k := range p.keyRegistry[path.Join(namespace, name)] {
if k == key {
return
}
}
p.keyRegistry[path.Join(namespace, name)] = append(
p.keyRegistry[path.Join(namespace, name)],
key,
)
}