Skip to content

Commit

Permalink
feat: add active validators gauge for consumers (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jun 27, 2024
1 parent d6594f6 commit 95d607b
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 31 deletions.
1 change: 1 addition & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func NewApp(configPath string, filesystem fs.FS, version string) *App {
generatorsPkg.NewPriceGenerator(),
generatorsPkg.NewConsumerInfoGenerator(appConfig.Chains),
generatorsPkg.NewConsumerNeedsToSignGenerator(appConfig.Chains),
generatorsPkg.NewValidatorActiveGenerator(appConfig.Chains, logger),
}

return &App{
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/consumer_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewConsumerInfoFetcher(
tracer trace.Tracer,
) *ConsumerInfoFetcher {
return &ConsumerInfoFetcher{
Logger: logger.With().Str("component", "validators_fetcher").Logger(),
Logger: logger.With().Str("component", "consumer_info_fetcher").Logger(),
Chains: chains,
RPCs: rpcs,
Tracer: tracer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/consumer_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewConsumerValidatorsFetcher(
tracer trace.Tracer,
) *ConsumerValidatorsFetcher {
return &ConsumerValidatorsFetcher{
Logger: logger.With().Str("component", "validators_fetcher").Logger(),
Logger: logger.With().Str("component", "consumer_validators_fetcher").Logger(),
Chains: chains,
RPCs: rpcs,
Tracer: tracer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/has_to_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewValidatorConsumersFetcher(
tracer trace.Tracer,
) *ValidatorConsumersFetcher {
return &ValidatorConsumersFetcher{
Logger: logger.With().Str("component", "node_info_fetcher").Logger(),
Logger: logger.With().Str("component", "validator_consumers_fetcher").Logger(),
Chains: chains,
RPCs: rpcs,
Tracer: tracer,
Expand Down
2 changes: 1 addition & 1 deletion pkg/fetchers/signing_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewSigningInfoFetcher(
tracer trace.Tracer,
) *SigningInfoFetcher {
return &SigningInfoFetcher{
Logger: logger.With().Str("component", "signing_infos").Logger(),
Logger: logger.With().Str("component", "signing_infos_fetcher").Logger(),
Chains: chains,
RPCs: rpcs,
Tracer: tracer,
Expand Down
14 changes: 0 additions & 14 deletions pkg/generators/single_validator_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ func (g *SingleValidatorInfoGenerator) Generate(state *statePkg.State) []prometh
[]string{"chain", "address"},
)

isActiveGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.MetricsPrefix + "active",
Help: "Whether a validator is active (1 if yes, 0 if no)",
},
[]string{"chain", "address"},
)

commissionGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.MetricsPrefix + "commission",
Expand Down Expand Up @@ -149,11 +141,6 @@ func (g *SingleValidatorInfoGenerator) Generate(state *statePkg.State) []prometh
"address": validatorAddr.Address,
}).Set(utils.BoolToFloat64(validator.Jailed))

isActiveGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.BoolToFloat64(validator.Active()))

commissionGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
Expand Down Expand Up @@ -185,7 +172,6 @@ func (g *SingleValidatorInfoGenerator) Generate(state *statePkg.State) []prometh
return []prometheus.Collector{
validatorInfoGauge,
isJailedGauge,
isActiveGauge,
commissionGauge,
commissionMaxGauge,
commissionMaxChangeGauge,
Expand Down
19 changes: 6 additions & 13 deletions pkg/generators/single_validator_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSingleValidatorInfoGeneratorNotFound(t *testing.T) {
})
generator := NewSingleValidatorInfoGenerator(chains, loggerPkg.GetNopLogger())
results := generator.Generate(state)
assert.Len(t, results, 7)
assert.Len(t, results, 6)

for _, metric := range results {
gauge, ok := metric.(*prometheus.GaugeVec)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestSingleValidatorInfoGeneratorActive(t *testing.T) {
})
generator := NewSingleValidatorInfoGenerator(chains, loggerPkg.GetNopLogger())
results := generator.Generate(state)
assert.Len(t, results, 7)
assert.Len(t, results, 6)

validatorInfoGauge, ok := results[0].(*prometheus.GaugeVec)
assert.True(t, ok)
Expand All @@ -140,35 +140,28 @@ func TestSingleValidatorInfoGeneratorActive(t *testing.T) {
"address": "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e",
})))

isActive, ok := results[2].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, 1, testutil.ToFloat64(isActive.With(prometheus.Labels{
"chain": "chain",
"address": "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e",
})), 0.01)

commissionGauge, ok := results[3].(*prometheus.GaugeVec)
commissionGauge, ok := results[2].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, 0.05, testutil.ToFloat64(commissionGauge.With(prometheus.Labels{
"chain": "chain",
"address": "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e",
})), 0.01)

commissionMaxGauge, ok := results[4].(*prometheus.GaugeVec)
commissionMaxGauge, ok := results[3].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, 0.2, testutil.ToFloat64(commissionMaxGauge.With(prometheus.Labels{
"chain": "chain",
"address": "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e",
})), 0.01)

commissionMaxChangeGauge, ok := results[5].(*prometheus.GaugeVec)
commissionMaxChangeGauge, ok := results[4].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, 0.01, testutil.ToFloat64(commissionMaxChangeGauge.With(prometheus.Labels{
"chain": "chain",
"address": "cosmosvaloper1xqz9pemz5e5zycaa89kys5aw6m8rhgsvw4328e",
})), 0.01)

delegationsGauge, ok := results[6].(*prometheus.GaugeVec)
delegationsGauge, ok := results[5].(*prometheus.GaugeVec)
assert.True(t, ok)
assert.InEpsilon(t, float64(2), testutil.ToFloat64(delegationsGauge.With(prometheus.Labels{
"chain": "chain",
Expand Down
117 changes: 117 additions & 0 deletions pkg/generators/validator_active.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package generators

import (
configPkg "main/pkg/config"
"main/pkg/constants"
fetchersPkg "main/pkg/fetchers"
statePkg "main/pkg/state"
"main/pkg/types"
"main/pkg/utils"

"github.com/rs/zerolog"

"github.com/prometheus/client_golang/prometheus"
)

type ValidatorActiveGenerator struct {
Chains []*configPkg.Chain
Logger zerolog.Logger
}

func NewValidatorActiveGenerator(
chains []*configPkg.Chain,
logger *zerolog.Logger,
) *ValidatorActiveGenerator {
return &ValidatorActiveGenerator{
Chains: chains,
Logger: logger.With().Str("component", "validator_active_generator").Logger(),
}
}

func (g *ValidatorActiveGenerator) Generate(state *statePkg.State) []prometheus.Collector {
dataRaw, ok := state.Get(constants.FetcherNameValidators)
if !ok {
return []prometheus.Collector{}
}

consumersDataRaw, ok := state.Get(constants.FetcherNameConsumerValidators)
if !ok {
return []prometheus.Collector{}
}

isActiveGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.MetricsPrefix + "active",
Help: "Whether a validator is active (1 if yes, 0 if no)",
},
[]string{"chain", "address"},
)

data, _ := dataRaw.(fetchersPkg.ValidatorsData)
consumersData, _ := consumersDataRaw.(fetchersPkg.ConsumerValidatorsData)

for _, chain := range g.Chains {
chainValidators, ok := data.Validators[chain.Name]
if !ok {
g.Logger.Warn().
Str("chain", chain.Name).
Msg("Could not find validators list")
continue
}

for _, validatorAddr := range chain.Validators {
compare := func(v types.Validator) bool {
equal, err := utils.CompareTwoBech32(v.OperatorAddress, validatorAddr.Address)
if err != nil {
g.Logger.Error().
Err(err).
Str("chain", chain.Name).
Str("validator", validatorAddr.Address).
Msg("Error comparing two validators' bech32 addresses")
return false
}

return equal
}

validator, ok := utils.Find(chainValidators.Validators, compare)

if !ok {
g.Logger.Warn().
Str("chain", chain.Name).
Str("validator", validatorAddr.Address).
Msg("Could not find validator")
continue
}

isActiveGauge.With(prometheus.Labels{
"chain": chain.Name,
"address": validatorAddr.Address,
}).Set(utils.BoolToFloat64(validator.Active()))

if validatorAddr.ConsensusAddress == "" {
continue
}

for _, consumer := range chain.ConsumerChains {
consumerValidators, ok := consumersData.Validators[consumer.Name]
if !ok {
continue
}

_, isActive := utils.Find(consumerValidators.Validators, func(v types.ConsumerValidator) bool {
return v.ProviderAddress == validatorAddr.ConsensusAddress
})

isActiveGauge.With(prometheus.Labels{
"chain": consumer.Name,
"address": validatorAddr.Address,
}).Set(utils.BoolToFloat64(isActive))
}
}
}

return []prometheus.Collector{
isActiveGauge,
}
}
Loading

0 comments on commit 95d607b

Please sign in to comment.