From 92c98331f4d57fbe0d815172fecb698faffbe88d Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Tue, 20 Jan 2026 11:09:14 -0300 Subject: [PATCH] Migrate notifier metrics with callbacks Signed-off-by: Arthur Silva Sens --- notifier/metric.go | 23 ++++++++--------------- notifier/semconv/metrics.go | 34 ++++++++++------------------------ notifier/semconv/registry.yaml | 8 ++++++-- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/notifier/metric.go b/notifier/metric.go index 9dfa0f6617..415876e803 100644 --- a/notifier/metric.go +++ b/notifier/metric.go @@ -36,21 +36,14 @@ func newAlertMetrics( queueLen, alertmanagersDiscovered func() float64, ) *alertMetrics { m := &alertMetrics{ - latencySummary: metrics.NewPrometheusNotificationsLatencySeconds(), - latencyHistogram: metrics.NewPrometheusNotificationsLatencyHistogramSeconds(), - errors: metrics.NewPrometheusNotificationsErrorsTotal(), - sent: metrics.NewPrometheusNotificationsSentTotal(), - dropped: metrics.NewPrometheusNotificationsDroppedTotal(), - // GaugeFunc metrics require callbacks, so they remain manual - queueLength: prometheus.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "prometheus_notifications_queue_length", - Help: "The number of alert notifications in the queue.", - }, queueLen), - queueCapacity: metrics.NewPrometheusNotificationsQueueCapacity(), - alertmanagersDiscovered: prometheus.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "prometheus_notifications_alertmanagers_discovered", - Help: "The number of alertmanagers discovered and active.", - }, alertmanagersDiscovered), + latencySummary: metrics.NewPrometheusNotificationsLatencySeconds(), + latencyHistogram: metrics.NewPrometheusNotificationsLatencyHistogramSeconds(), + errors: metrics.NewPrometheusNotificationsErrorsTotal(), + sent: metrics.NewPrometheusNotificationsSentTotal(), + dropped: metrics.NewPrometheusNotificationsDroppedTotal(), + queueLength: prometheus.NewGaugeFunc(metrics.PrometheusNotificationsQueueLengthOpts(), queueLen), + queueCapacity: metrics.NewPrometheusNotificationsQueueCapacity(), + alertmanagersDiscovered: prometheus.NewGaugeFunc(metrics.PrometheusNotificationsAlertmanagersDiscoveredOpts(), alertmanagersDiscovered), } m.queueCapacity.Set(float64(queueCap)) diff --git a/notifier/semconv/metrics.go b/notifier/semconv/metrics.go index 1b405c40f4..e34d86e365 100644 --- a/notifier/semconv/metrics.go +++ b/notifier/semconv/metrics.go @@ -26,17 +26,10 @@ func (a AlertmanagerAttr) Value() string { } // PrometheusNotificationsAlertmanagersDiscovered records the number of alertmanagers discovered and active. -type PrometheusNotificationsAlertmanagersDiscovered struct { - prometheus.Gauge -} - -// NewPrometheusNotificationsAlertmanagersDiscovered returns a new PrometheusNotificationsAlertmanagersDiscovered instrument. -func NewPrometheusNotificationsAlertmanagersDiscovered() PrometheusNotificationsAlertmanagersDiscovered { - return PrometheusNotificationsAlertmanagersDiscovered{ - Gauge: prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "prometheus_notifications_alertmanagers_discovered", - Help: "The number of alertmanagers discovered and active.", - }), +func PrometheusNotificationsAlertmanagersDiscoveredOpts() prometheus.GaugeOpts { + return prometheus.GaugeOpts{ + Name: "prometheus_notifications_alertmanagers_discovered", + Help: "The number of alertmanagers discovered and active.", } } @@ -148,8 +141,8 @@ func NewPrometheusNotificationsLatencySeconds() PrometheusNotificationsLatencySe Name: "prometheus_notifications_latency_seconds", Help: "Latency quantiles for sending alert notifications.", Objectives: map[float64]float64{ - 0.5: 0.05, - 0.9: 0.01, + 0.5: 0.05, + 0.9: 0.01, 0.99: 0.001, }, }, labels), @@ -191,17 +184,10 @@ func NewPrometheusNotificationsQueueCapacity() PrometheusNotificationsQueueCapac } // PrometheusNotificationsQueueLength records the number of alert notifications in the queue. -type PrometheusNotificationsQueueLength struct { - prometheus.Gauge -} - -// NewPrometheusNotificationsQueueLength returns a new PrometheusNotificationsQueueLength instrument. -func NewPrometheusNotificationsQueueLength() PrometheusNotificationsQueueLength { - return PrometheusNotificationsQueueLength{ - Gauge: prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "prometheus_notifications_queue_length", - Help: "The number of alert notifications in the queue.", - }), +func PrometheusNotificationsQueueLengthOpts() prometheus.GaugeOpts { + return prometheus.GaugeOpts{ + Name: "prometheus_notifications_queue_length", + Help: "The number of alert notifications in the queue.", } } diff --git a/notifier/semconv/registry.yaml b/notifier/semconv/registry.yaml index d681e6b4fa..b93af27ac1 100644 --- a/notifier/semconv/registry.yaml +++ b/notifier/semconv/registry.yaml @@ -24,7 +24,9 @@ groups: metric_name: prometheus_notifications_alertmanagers_discovered instrument: gauge unit: "{alertmanager}" - # Note: This is implemented as GaugeFunc in code (requires callback) + annotations: + prometheus: + only_opts: true # Implemented as GaugeFunc - id: metric.prometheus_notifications_dropped_total type: metric @@ -49,7 +51,9 @@ groups: metric_name: prometheus_notifications_queue_length instrument: gauge unit: "{notification}" - # Note: This is implemented as GaugeFunc in code (requires callback) + annotations: + prometheus: + only_opts: true # Implemented as GaugeFunc # Metrics with alertmanager label - id: metric.prometheus_notifications_errors_total