From d8e0b9c937dc974fef7484db3f85fabfe9272d7d Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Sat, 4 Jan 2025 14:22:20 +0100 Subject: prometheus: Add basic metrics for alerting - Detect if we're falling behind while downloading - Detect if there are any found certificates alerting --- internal/metrics/metrics.go | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 internal/metrics/metrics.go (limited to 'internal/metrics/metrics.go') diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go new file mode 100644 index 0000000..113d28c --- /dev/null +++ b/internal/metrics/metrics.go @@ -0,0 +1,71 @@ +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 +} + +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"}, + ), + } + + registry.MustRegister(m.logSize, m.logIndex, m.logTimestamp, m.certificateAlert) + 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())) + } +} -- cgit v1.2.3