From 534420be32bb2480da02b8a49eb304083d0a8a39 Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Thu, 15 May 2025 10:39:24 -0400 Subject: [PATCH] refactor MetricBusService to an interface D-Bus metricbus server is being deprecated and being replaced with an https server that registers itself in sd and publishes its own stats. This is the first step in the refactor, converting `MetricBusService` to an interface and updating all references accordingly. Full implementation will come later. --- machines/client.go | 4 +-- machines/coredns_plugin/registry.go | 2 +- machines/coredns_plugin/stats.go | 2 +- machines/stats.go | 2 +- .../mbclient/{client.go => dbus_client.go} | 32 +++++++++---------- .../mbclient/{metrics.go => dbus_metrics.go} | 2 +- metrics/metricbus/mbclient/intf.go | 7 ++++ metrics/mtls/stats.go | 2 +- 8 files changed, 30 insertions(+), 23 deletions(-) rename metrics/metricbus/mbclient/{client.go => dbus_client.go} (85%) rename metrics/metricbus/mbclient/{metrics.go => dbus_metrics.go} (98%) diff --git a/machines/client.go b/machines/client.go index dc9a1f0..cbd35e8 100644 --- a/machines/client.go +++ b/machines/client.go @@ -48,7 +48,7 @@ var ( type MachinesClient interface { APICall(route string, data interface{}, response any) error NewEventListener(ctx context.Context) (chan MachinesMqttEvent, error) - SetupStats(metrics *mbclient.MetricBusService) + SetupStats(metrics mbclient.MetricBusService) } type machinesClient struct { @@ -219,7 +219,7 @@ func (mc *machinesClient) APICall(route string, data interface{}, response any) return nil } -func (mc *machinesClient) SetupStats(metrics *mbclient.MetricBusService) { +func (mc *machinesClient) SetupStats(metrics mbclient.MetricBusService) { mc.stats = makeStats(metrics) } diff --git a/machines/coredns_plugin/registry.go b/machines/coredns_plugin/registry.go index 0b6b608..0769e3b 100644 --- a/machines/coredns_plugin/registry.go +++ b/machines/coredns_plugin/registry.go @@ -43,7 +43,7 @@ type registry struct { client machines.MachinesClient didLiveFetch bool - mb *mbclient.MetricBusService + mb mbclient.MetricBusService stats *dnsStats defDomain string diff --git a/machines/coredns_plugin/stats.go b/machines/coredns_plugin/stats.go index 8751ef8..1293c30 100644 --- a/machines/coredns_plugin/stats.go +++ b/machines/coredns_plugin/stats.go @@ -15,7 +15,7 @@ type dnsStats struct { eventsReceived mbclient.CounterMetric } -func makeStats(client *mbclient.MetricBusService) *dnsStats { +func makeStats(client mbclient.MetricBusService) *dnsStats { recordLookups := client.DefineCounter( constants.OrgSlug+"_machines_dns_record_lookups", "DNS lookups that resolved to custom DNS records", diff --git a/machines/stats.go b/machines/stats.go index ea2da48..2c96ec0 100644 --- a/machines/stats.go +++ b/machines/stats.go @@ -11,7 +11,7 @@ type clientStats struct { mqttMessages mbclient.CounterMetric } -func makeStats(client *mbclient.MetricBusService) *clientStats { +func makeStats(client mbclient.MetricBusService) *clientStats { apiRequests := client.DefineCounter( constants.OrgSlug+"_machines_client_api_requests", "Requests made by the Machines client to the API", diff --git a/metrics/metricbus/mbclient/client.go b/metrics/metricbus/mbclient/dbus_client.go similarity index 85% rename from metrics/metricbus/mbclient/client.go rename to metrics/metricbus/mbclient/dbus_client.go index a74435f..76c019c 100644 --- a/metrics/metricbus/mbclient/client.go +++ b/metrics/metricbus/mbclient/dbus_client.go @@ -17,7 +17,7 @@ import ( type KV = metricbus.KV -type MetricBusService struct { +type metricBusServiceImpl struct { name string instance string @@ -39,7 +39,7 @@ type MetricBusService struct { var defaultMetricBusServiceName string var defaultMetricBusServiceDiscriminator string -var store map[string]*MetricBusService +var store map[string]*metricBusServiceImpl var storeLock sync.Mutex func init() { @@ -48,16 +48,16 @@ func init() { flag.StringVar(&defaultMetricBusServiceDiscriminator, "metricbus.client.service-discriminator", "", "discriminator to use by default for publishing metrics") } -func NewService(ctx context.Context) *MetricBusService { +func NewService(ctx context.Context) MetricBusService { return NewServiceWithDiscriminator(ctx, defaultMetricBusServiceDiscriminator) } -func NewServiceWithDiscriminator(ctx context.Context, instanceDiscriminator string) *MetricBusService { +func NewServiceWithDiscriminator(ctx context.Context, instanceDiscriminator string) MetricBusService { storeLock.Lock() defer storeLock.Unlock() if store == nil { - store = make(map[string]*MetricBusService) + store = make(map[string]*metricBusServiceImpl) } if instance, ok := store[instanceDiscriminator]; ok { @@ -70,9 +70,9 @@ func NewServiceWithDiscriminator(ctx context.Context, instanceDiscriminator stri return instance } -func newServiceWithDiscriminator(ctx context.Context, instanceDiscriminator string) *MetricBusService { +func newServiceWithDiscriminator(ctx context.Context, instanceDiscriminator string) *metricBusServiceImpl { childCtx, cancel := context.WithCancel(context.Background()) - svc := &MetricBusService{ + svc := &metricBusServiceImpl{ name: defaultMetricBusServiceName, instance: instanceDiscriminator, @@ -92,7 +92,7 @@ func newServiceWithDiscriminator(ctx context.Context, instanceDiscriminator stri return svc } -func (s *MetricBusService) FlushAndWait() { +func (s *metricBusServiceImpl) FlushAndWait() { if s.childCtx.Err() == context.Canceled || s.serviceCookie == "" { return } @@ -105,7 +105,7 @@ func (s *MetricBusService) FlushAndWait() { defer s.mu.Unlock() } -func (s *MetricBusService) DefineCounter(metricName, descr string, labelNames ...string) CounterMetric { +func (s *metricBusServiceImpl) DefineCounter(metricName, descr string, labelNames ...string) CounterMetric { s.mu.Lock() defer s.mu.Unlock() @@ -131,7 +131,7 @@ func (s *MetricBusService) DefineCounter(metricName, descr string, labelNames .. } } -func (s *MetricBusService) DefineGauge(metricName, descr string, labelNames ...string) GaugeMetric { +func (s *metricBusServiceImpl) DefineGauge(metricName, descr string, labelNames ...string) GaugeMetric { s.mu.Lock() defer s.mu.Unlock() @@ -157,7 +157,7 @@ func (s *MetricBusService) DefineGauge(metricName, descr string, labelNames ...s } } -func (s *MetricBusService) setLocalStateDeregistered() { +func (s *metricBusServiceImpl) setLocalStateDeregistered() { s.mu.Lock() defer s.mu.Unlock() @@ -169,7 +169,7 @@ func (s *MetricBusService) setLocalStateDeregistered() { } } -func (s *MetricBusService) tryRegister() { +func (s *metricBusServiceImpl) tryRegister() { s.mu.Lock() defer s.mu.Unlock() @@ -185,7 +185,7 @@ func (s *MetricBusService) tryRegister() { s.serviceCookie = cookie } -func (s *MetricBusService) Flush() { +func (s *metricBusServiceImpl) Flush() { var err error s.mu.Lock() @@ -234,7 +234,7 @@ handleServerRestarted: goto flushRestart } -func (s *MetricBusService) deregister() { +func (s *metricBusServiceImpl) deregister() { s.mu.Lock() defer s.mu.Unlock() @@ -250,7 +250,7 @@ func (s *MetricBusService) deregister() { s.childCtxCancel() } -func (s *MetricBusService) loop() { +func (s *metricBusServiceImpl) loop() { ticker := time.NewTicker(1 * time.Second) for { @@ -264,7 +264,7 @@ func (s *MetricBusService) loop() { } } -func (s *MetricBusService) event(metricName string, labelValues []string) *metricValue { +func (s *metricBusServiceImpl) event(metricName string, labelValues []string) *metricValue { s.mu.Lock() defer s.mu.Unlock() diff --git a/metrics/metricbus/mbclient/metrics.go b/metrics/metricbus/mbclient/dbus_metrics.go similarity index 98% rename from metrics/metricbus/mbclient/metrics.go rename to metrics/metricbus/mbclient/dbus_metrics.go index 45bd0c5..f0774db 100644 --- a/metrics/metricbus/mbclient/metrics.go +++ b/metrics/metricbus/mbclient/dbus_metrics.go @@ -6,7 +6,7 @@ import ( ) type metricMetadata struct { - s *MetricBusService + s *metricBusServiceImpl name string help string metricType metricbus.MetricType diff --git a/metrics/metricbus/mbclient/intf.go b/metrics/metricbus/mbclient/intf.go index 8b5ccdb..98c8f0b 100644 --- a/metrics/metricbus/mbclient/intf.go +++ b/metrics/metricbus/mbclient/intf.go @@ -22,6 +22,13 @@ type metricBusLowLevelConnection interface { Post(serviceCookie string, values []metricValue) error } +type MetricBusService interface { + Flush() + FlushAndWait() + DefineCounter(metricName, descr string, labelNames ...string) CounterMetric + DefineGauge(metricName, descr string, labelNames ...string) GaugeMetric +} + type CounterMetric interface { WithLabelValues(metricbus.KV) CounterMetric Add(float64) diff --git a/metrics/mtls/stats.go b/metrics/mtls/stats.go index 8315d96..ce8c0a9 100644 --- a/metrics/mtls/stats.go +++ b/metrics/mtls/stats.go @@ -19,7 +19,7 @@ import ( type mtlsMetrics struct { log log.Logger - svc *mbclient.MetricBusService + svc mbclient.MetricBusService secondsUntilExpiration mbclient.GaugeMetric rotationsObserved mbclient.CounterMetric -- 2.50.1