Skip to content

Commit

Permalink
state: add vochain_validator metric using NewGaugeVec
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Oct 30, 2023
1 parent 4ac0289 commit 0ad125c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions vochain/state/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0ad125c

Please sign in to comment.