Files
aether/metrics_prometheus.go
Hugo Nijhuis e3dbe3d52d
All checks were successful
CI / build (push) Successful in 19s
[Issue #22] Add EventBroadcaster metrics (#49)
2026-01-10 18:52:32 +00:00

124 lines
3.4 KiB
Go

package aether
import (
"github.com/prometheus/client_golang/prometheus"
)
// PrometheusMetricsAdapter exposes BroadcasterMetrics as Prometheus metrics.
// It implements prometheus.Collector and can be registered with a Prometheus registry.
type PrometheusMetricsAdapter struct {
metrics BroadcasterMetrics
eventsPublishedDesc *prometheus.Desc
eventsReceivedDesc *prometheus.Desc
activeSubscriptionsDesc *prometheus.Desc
publishErrorsDesc *prometheus.Desc
subscribeErrorsDesc *prometheus.Desc
droppedEventsDesc *prometheus.Desc
}
// NewPrometheusMetricsAdapter creates a new PrometheusMetricsAdapter that wraps
// a BroadcasterMetrics implementation and exposes it as Prometheus metrics.
//
// The adapter implements prometheus.Collector and should be registered with
// a Prometheus registry:
//
// eb := aether.NewEventBus()
// adapter := aether.NewPrometheusMetricsAdapter(eb.Metrics())
// prometheus.MustRegister(adapter)
func NewPrometheusMetricsAdapter(metrics BroadcasterMetrics) *PrometheusMetricsAdapter {
return &PrometheusMetricsAdapter{
metrics: metrics,
eventsPublishedDesc: prometheus.NewDesc(
"aether_events_published_total",
"Total number of events published per namespace",
[]string{"namespace"},
nil,
),
eventsReceivedDesc: prometheus.NewDesc(
"aether_events_received_total",
"Total number of events received per namespace",
[]string{"namespace"},
nil,
),
activeSubscriptionsDesc: prometheus.NewDesc(
"aether_active_subscriptions",
"Number of active subscriptions per namespace",
[]string{"namespace"},
nil,
),
publishErrorsDesc: prometheus.NewDesc(
"aether_publish_errors_total",
"Total number of publish errors per namespace",
[]string{"namespace"},
nil,
),
subscribeErrorsDesc: prometheus.NewDesc(
"aether_subscribe_errors_total",
"Total number of subscribe errors per namespace",
[]string{"namespace"},
nil,
),
droppedEventsDesc: prometheus.NewDesc(
"aether_dropped_events_total",
"Total number of dropped events per namespace",
[]string{"namespace"},
nil,
),
}
}
// Describe implements prometheus.Collector.
func (a *PrometheusMetricsAdapter) Describe(ch chan<- *prometheus.Desc) {
ch <- a.eventsPublishedDesc
ch <- a.eventsReceivedDesc
ch <- a.activeSubscriptionsDesc
ch <- a.publishErrorsDesc
ch <- a.subscribeErrorsDesc
ch <- a.droppedEventsDesc
}
// Collect implements prometheus.Collector.
func (a *PrometheusMetricsAdapter) Collect(ch chan<- prometheus.Metric) {
namespaces := a.metrics.Namespaces()
for _, ns := range namespaces {
ch <- prometheus.MustNewConstMetric(
a.eventsPublishedDesc,
prometheus.CounterValue,
float64(a.metrics.EventsPublished(ns)),
ns,
)
ch <- prometheus.MustNewConstMetric(
a.eventsReceivedDesc,
prometheus.CounterValue,
float64(a.metrics.EventsReceived(ns)),
ns,
)
ch <- prometheus.MustNewConstMetric(
a.activeSubscriptionsDesc,
prometheus.GaugeValue,
float64(a.metrics.ActiveSubscriptions(ns)),
ns,
)
ch <- prometheus.MustNewConstMetric(
a.publishErrorsDesc,
prometheus.CounterValue,
float64(a.metrics.PublishErrors(ns)),
ns,
)
ch <- prometheus.MustNewConstMetric(
a.subscribeErrorsDesc,
prometheus.CounterValue,
float64(a.metrics.SubscribeErrors(ns)),
ns,
)
ch <- prometheus.MustNewConstMetric(
a.droppedEventsDesc,
prometheus.CounterValue,
float64(a.metrics.DroppedEvents(ns)),
ns,
)
}
}