Skip to content

Commit

Permalink
Add chia_exporter_build_info metric with version info (#197)
Browse files Browse the repository at this point in the history
* Add function for calling init metrics

* Add chia_exporter_build_info metric with version
  • Loading branch information
cmmarslender authored Nov 12, 2024
1 parent 2f6189b commit da3ef84
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
31 changes: 20 additions & 11 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit da3ef84

Please sign in to comment.