Skip to content

Commit

Permalink
state: add vochain_validator_* metrics (power, score, proposals, votes)
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Oct 30, 2023
1 parent 4ac0289 commit 222a057
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions vochain/state/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,72 @@ 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 metricValidatorPower = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "vochain",
Name: "validator_power",
Help: "validator stats",
}, []string{"address"})

var metricValidatorScore = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "vochain",
Name: "validator_score",
Help: "validator stats",
}, []string{"address"})

var metricValidatorProposals = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "vochain",
Name: "validator_proposals",
Help: "validator stats",
}, []string{"address"})

var metricValidatorVotes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "vochain",
Name: "validator_votes",
Help: "validator stats",
}, []string{"address"})

func init() {
metrics.Register(metricValidatorPower)
metrics.Register(metricValidatorScore)
metrics.Register(metricValidatorProposals)
metrics.Register(metricValidatorVotes)
}

func setGaugeWithLabel(gv *prometheus.GaugeVec, label string, value float64) {
gauge, err := gv.GetMetricWithLabelValues(label)
if err != nil {
log.Warn(err)
return
}
gauge.Set(value)
}

func metricsUpdateValidator(validator *models.Validator) {
label := fmt.Sprintf("%x", validator.GetAddress())
setGaugeWithLabel(metricValidatorPower, label, float64(validator.GetPower()))
setGaugeWithLabel(metricValidatorScore, label, float64(validator.GetScore()))
setGaugeWithLabel(metricValidatorProposals, label, float64(validator.GetProposals()))
setGaugeWithLabel(metricValidatorVotes, label, float64(validator.GetVotes()))
}

func metricsDeleteValidator(label string) {
metricValidatorPower.DeleteLabelValues(label)
metricValidatorScore.DeleteLabelValues(label)
metricValidatorProposals.DeleteLabelValues(label)
metricValidatorVotes.DeleteLabelValues(label)
}

// AddValidator adds a tendemint validator. If it exists, it will be updated.
func (v *State) AddValidator(validator *models.Validator) error {
defer metricsUpdateValidator(validator)
v.tx.Lock()
defer v.tx.Unlock()
validatorBytes, err := proto.Marshal(validator)
Expand All @@ -24,6 +83,7 @@ func (v *State) AddValidator(validator *models.Validator) error {

// RemoveValidator removes a tendermint validator identified by its address
func (v *State) RemoveValidator(address []byte) error {
defer metricsDeleteValidator(fmt.Sprintf("%x", address))
v.tx.Lock()
defer v.tx.Unlock()
validators, err := v.tx.SubTree(StateTreeCfg(TreeValidators))
Expand Down

0 comments on commit 222a057

Please sign in to comment.