From 8ca8513f2e5d865832efc94a9b558dc01206a7d5 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 11:34:47 -0600 Subject: [PATCH 1/2] 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 { From 2b30f602db4ed7e7ce8f73e780842acc9c2d6960 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 11:43:47 -0600 Subject: [PATCH 2/2] Add chia_exporter_build_info metric with version --- cmd/root.go | 10 +++++++++- cmd/serve.go | 2 +- internal/metrics/metrics.go | 10 ++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 693308a..1298989 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,7 +20,15 @@ var ( var rootCmd = &cobra.Command{ Use: "chia-exporter", Short: "Prometheus metric exporter for Chia Blockchain", - Version: fmt.Sprintf("%s (%s)", gitVersion, buildTime), + Version: versionString(true), +} + +func versionString(includeTime bool) string { + if includeTime { + return fmt.Sprintf("%s (%s)", gitVersion, buildTime) + } + + return gitVersion } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/serve.go b/cmd/serve.go index 114b3e7..f8fb984 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -28,7 +28,7 @@ var serveCmd = &cobra.Command{ // We reload the metrics client every time to get a fresh chia config, in case certs or other config changed // that enable a successful connection at that point for { - m, err = metrics.NewMetrics(uint16(viper.GetInt("metrics-port")), level) + m, err = metrics.NewMetrics(uint16(viper.GetInt("metrics-port")), level, versionString(false)) if err != nil { log.Fatalln(err.Error()) } diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index bf5624a..cd796d8 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -77,18 +77,20 @@ type Metrics struct { // All the serviceMetrics interfaces that are registered serviceMetrics map[chiaService]serviceMetrics - buildInfoMetric *prometheus.Gauge + version string + buildInfoMetric *prometheus.GaugeVec } // NewMetrics returns a new instance of metrics // All metrics are registered here -func NewMetrics(port uint16, logLevel log.Level) (*Metrics, error) { +func NewMetrics(port uint16, logLevel log.Level, version string) (*Metrics, error) { var err error metrics := &Metrics{ metricsPort: port, registry: prometheus.NewRegistry(), serviceMetrics: map[chiaService]serviceMetrics{}, + version: version, } log.SetLevel(logLevel) @@ -148,6 +150,10 @@ func (m *Metrics) setNewClient() error { } func (m *Metrics) initMetrics() { + // Initialize global metrics + m.buildInfoMetric = m.newGaugeVec(chiaService("exporter"), "build_info", "Build info for chia exporter", []string{"version"}) + m.buildInfoMetric.WithLabelValues(m.version).Set(1) + // Init each service's metrics for _, service := range m.serviceMetrics { service.InitMetrics(m.network)