From 0ad125c07309d40d31acb781b4815860b5b3490c Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Mon, 30 Oct 2023 13:13:27 +0100 Subject: [PATCH] state: add vochain_validator metric using NewGaugeVec --- vochain/state/validators.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/vochain/state/validators.go b/vochain/state/validators.go index bd544a797..42a7be65a 100644 --- a/vochain/state/validators.go +++ b/vochain/state/validators.go @@ -6,13 +6,37 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/prometheus/client_golang/prometheus" + "go.vocdoni.io/dvote/log" + "go.vocdoni.io/dvote/metrics" "go.vocdoni.io/dvote/tree/arbo" "go.vocdoni.io/proto/build/go/models" "google.golang.org/protobuf/proto" ) +var promValidator = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: "vochain", + Name: "validator", + Help: "validator stats", +}, []string{"address"}) + +func init() { + metrics.Register(promValidator) +} + +func updateValidatorMetrics(validator *models.Validator) { + log.Warnf("validator %s (%x): %d", validator.GetName(), validator.GetAddress(), validator.GetPower()) + gauge, err := promValidator.GetMetricWithLabelValues(fmt.Sprintf("%x", validator.GetAddress())) + if err != nil { + log.Warn(err) + return + } + gauge.Set(float64(validator.GetPower())) +} + // AddValidator adds a tendemint validator. If it exists, it will be updated. func (v *State) AddValidator(validator *models.Validator) error { + defer updateValidatorMetrics(validator) v.tx.Lock() defer v.tx.Unlock() validatorBytes, err := proto.Marshal(validator)