From 2b30f602db4ed7e7ce8f73e780842acc9c2d6960 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 11:43:47 -0600 Subject: [PATCH] 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)