From da3ef844b4e7d98f84e8b7a717155322771c84a5 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Tue, 12 Nov 2024 11:51:24 -0600 Subject: [PATCH] Add chia_exporter_build_info metric with version info (#197) * Add function for calling init metrics * Add chia_exporter_build_info metric with version --- cmd/root.go | 10 +++++++++- cmd/serve.go | 2 +- internal/metrics/metrics.go | 31 ++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 13 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 b487838..cd796d8 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -76,17 +76,21 @@ type Metrics struct { // All the serviceMetrics interfaces that are registered serviceMetrics map[chiaService]serviceMetrics + + 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) @@ -128,10 +132,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 +149,17 @@ func (m *Metrics) setNewClient() error { return nil } +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) + } +} + func (m *Metrics) createDBClient() error { var err error @@ -327,10 +339,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 +448,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 {