Skip to content

Commit

Permalink
Add chia_exporter_build_info metric with version
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender committed Nov 12, 2024
1 parent 8ca8513 commit 2b30f60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 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
10 changes: 8 additions & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2b30f60

Please sign in to comment.