From 8ca8513f2e5d865832efc94a9b558dc01206a7d5 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 11:34:47 -0600 Subject: [PATCH] Add function for calling init metrics --- internal/metrics/metrics.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index b487838..bf5624a 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -76,6 +76,8 @@ type Metrics struct { // All the serviceMetrics interfaces that are registered serviceMetrics map[chiaService]serviceMetrics + + buildInfoMetric *prometheus.Gauge } // NewMetrics returns a new instance of metrics @@ -128,10 +130,7 @@ func NewMetrics(port uint16, logLevel log.Level) (*Metrics, error) { // If not, the reconnect handler will handle it later _, _ = metrics.checkNetwork() - // Init each service's metrics - for _, service := range metrics.serviceMetrics { - service.InitMetrics(metrics.network) - } + metrics.initMetrics() return metrics, nil } @@ -148,6 +147,13 @@ func (m *Metrics) setNewClient() error { return nil } +func (m *Metrics) initMetrics() { + // Init each service's metrics + for _, service := range m.serviceMetrics { + service.InitMetrics(m.network) + } +} + func (m *Metrics) createDBClient() error { var err error @@ -327,10 +333,7 @@ func (m *Metrics) OpenWebsocket() error { if changed { m.registry = prometheus.NewRegistry() m.dynamicPromHandler.updateHandler(promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})) - // Init each service's metrics - for _, service := range m.serviceMetrics { - service.InitMetrics(m.network) - } + m.initMetrics() } ctx, cancel := context.WithCancel(context.Background()) @@ -439,9 +442,9 @@ func (m *Metrics) reconnectHandler() { log.Info("Network Changed, resetting all metrics") m.registry = prometheus.NewRegistry() m.dynamicPromHandler.updateHandler(promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})) - // Init each service's metrics + m.initMetrics() + // Init each service's data for _, service := range m.serviceMetrics { - service.InitMetrics(m.network) service.InitialData() } } else {