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
|
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"rgdd.se/silentct/internal/monitor"
"rgdd.se/silentct/pkg/storage/index"
)
type Metrics struct {
logSize *prometheus.GaugeVec
logIndex *prometheus.GaugeVec
logTimestamp *prometheus.GaugeVec
certificateAlert *prometheus.GaugeVec
errorCounter prometheus.Counter
needRestart prometheus.Gauge
}
func NewMetrics(registry *prometheus.Registry) *Metrics {
m := &Metrics{
logSize: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "silentct_log_size",
Help: "The number of entries in the log.",
},
[]string{"id"},
),
logIndex: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "silentct_log_index",
Help: "The next log entry to be downloaded.",
},
[]string{"id"},
),
logTimestamp: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "silentct_log_timestamp",
Help: "The log's UNIX timestamp in ms.",
},
[]string{"id"},
),
certificateAlert: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "silentct_certificate_alert",
Help: "The time the certificate without allowlisting was found.",
},
[]string{"stored_at"},
),
errorCounter: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "silentct_error_counter",
Help: "The number of errors propagated to the main loop.",
},
),
needRestart: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "silentct_need_restart",
Help: "A non-zero value if the monitor needs restarting.",
},
),
}
registry.MustRegister(m.logSize, m.logIndex, m.logTimestamp, m.certificateAlert, m.errorCounter, m.needRestart)
return m
}
func (m *Metrics) LogState(state monitor.State) {
id := state.LogID.Base64String()
m.logIndex.WithLabelValues(id).Set(float64(state.NextIndex))
m.logSize.WithLabelValues(id).Set(float64(state.TreeSize))
m.logTimestamp.WithLabelValues(id).Set(float64(state.Timestamp))
}
func (m *Metrics) RemoveLogState(state monitor.State) {
id := state.LogID.Base64String()
m.logIndex.Delete(prometheus.Labels{"id": id})
m.logSize.Delete(prometheus.Labels{"id": id})
m.logTimestamp.Delete(prometheus.Labels{"id": id})
}
func (m *Metrics) CertificateAlert(alerts []index.CertificateInfo) {
m.certificateAlert.Reset()
for _, alert := range alerts {
m.certificateAlert.WithLabelValues(alert.StoredAt).Set(float64(alert.ObservedAt.Unix()))
}
}
func (m *Metrics) CountError() {
m.errorCounter.Inc()
}
func (m *Metrics) NeedRestart() {
m.needRestart.Set(float64(1))
}
|