Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chia_exporter_build_info metric with version info #197

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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