Skip to content

Commit

Permalink
feat(validator): adding new metric for validator export (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlehtimaki authored May 3, 2024
1 parent b3b0479 commit f45ce70
Show file tree
Hide file tree
Showing 9 changed files with 495 additions and 148 deletions.
31 changes: 20 additions & 11 deletions cmd/warden-exporter/warden-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,28 @@ func main() {
log.Fatal(err.Error())
}

wardenCollector := collector.WardenCollector{
Cfg: cfg,
}
intentCollector := collector.IntentCollector{
Cfg: cfg,
}
authCollector := collector.AuthCollector{
Cfg: cfg,
if cfg.WardenMetrics {
wardenCollector := collector.WardenCollector{
Cfg: cfg,
}
intentCollector := collector.IntentCollector{
Cfg: cfg,
}
authCollector := collector.AuthCollector{
Cfg: cfg,
}

prometheus.MustRegister(wardenCollector)
prometheus.MustRegister(intentCollector)
prometheus.MustRegister(authCollector)
}

prometheus.MustRegister(wardenCollector)
prometheus.MustRegister(intentCollector)
prometheus.MustRegister(authCollector)
if cfg.ValidatorMetrics {
validatorCollector := collector.ValidatorsCollector{
Cfg: cfg,
}
prometheus.MustRegister(validatorCollector)
}

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/warden-protocol/warden-exporter
go 1.22

require (
cosmossdk.io/api v0.7.4
github.com/caarlos0/env/v10 v10.0.0
github.com/cosmos/cosmos-sdk v0.50.6
github.com/prometheus/client_golang v1.19.0
Expand All @@ -12,7 +13,6 @@ require (
)

require (
cosmossdk.io/api v0.7.4 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.11.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
Expand Down
92 changes: 92 additions & 0 deletions pkg/collector/validator_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package collector

import (
"context"
"fmt"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/warden-protocol/warden-exporter/pkg/config"
"github.com/warden-protocol/warden-exporter/pkg/grpc"
log "github.com/warden-protocol/warden-exporter/pkg/logger"
types "github.com/warden-protocol/warden-exporter/pkg/types"
)

const (
missedBlocksMetricName = "cosmos_validator_missed_blocks"
)

//nolint:gochecknoglobals // this is needed as it's used in multiple places
var missedBlocks = prometheus.NewDesc(
missedBlocksMetricName,
"Returns missed blocks for a validator.",
[]string{
"chain_id",
"valcons",
"valoper",
"moniker",
"jailed",
"tombstoned",
"bond_status",
},
nil,
)

type ValidatorsCollector struct {
Cfg config.Config
}

func (vc ValidatorsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- missedBlocks
}

func (vc ValidatorsCollector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(vc.Cfg.Timeout)*time.Second,
)

defer cancel()

vals, err := grpc.SigningValidators(ctx, vc.Cfg)
if err != nil {
log.Error(fmt.Sprintf("error getting signing validators: %s", err))
} else {
log.Debug("Start collecting", zap.String("metric", missedBlocksMetricName))

for _, m := range vc.missedBlocksMetrics(vals) {
ch <- m
}

log.Debug("Stop collecting", zap.String("metric", missedBlocksMetricName))
}
}

func (vc ValidatorsCollector) missedBlocksMetrics(vals []types.Validator) []prometheus.Metric {
metrics := []prometheus.Metric{}

for _, val := range vals {
metrics = append(
metrics,
prometheus.MustNewConstMetric(
missedBlocks,
prometheus.GaugeValue,
float64(val.MissedBlocks),
[]string{
vc.Cfg.ChainID,
val.ConsAddress,
val.OperatorAddress,
val.Moniker,
strconv.FormatBool(val.Jailed),
strconv.FormatBool(val.Tombstoned),
val.BondStatus,
}...,
),
)
}

return metrics
}
10 changes: 6 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ func configError(msg string) error {
}

type Config struct {
Addr string `env:"GRPC_ADDR" envDefault:"grpc.buenavista.wardenprotocol.org:443"`
TLS bool `env:"GRPC_TLS_ENABLED" envDefault:"true"`
Timeout int `env:"GRPC_TIMEOUT_SECONDS" envDefault:"5"`
ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1"`
Addr string `env:"GRPC_ADDR" envDefault:"grpc.buenavista.wardenprotocol.org:443"`
TLS bool `env:"GRPC_TLS_ENABLED" envDefault:"true"`
Timeout int `env:"GRPC_TIMEOUT_SECONDS" envDefault:"5"`
ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1"`
WardenMetrics bool `env:"WARDEN_METRICS" envDefault:"true"`
ValidatorMetrics bool `env:"VALIDATOR_METRICS" envDefault:"true"`
}

func (c Config) GRPCConn() (*grpc.ClientConn, error) {
Expand Down
132 changes: 0 additions & 132 deletions pkg/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package grpc

import (
"context"
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/types/query"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
intent "github.com/warden-protocol/wardenprotocol/warden/x/intent/types"
warden "github.com/warden-protocol/wardenprotocol/warden/x/warden/types/v1beta2"
"google.golang.org/grpc"

"github.com/warden-protocol/warden-exporter/pkg/config"
Expand Down Expand Up @@ -39,130 +34,3 @@ func NewClient(cfg config.Config) (Client, error) {

return client, nil
}

// spaces metric.
func (c Client) Spaces(ctx context.Context) (uint64, error) {
client := warden.NewQueryClient(c.conn)
req := warden.QuerySpacesRequest{Pagination: &query.PageRequest{
Limit: 1,
CountTotal: true,
}}

spacesRes, err := client.Spaces(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return spacesRes.Pagination.Total, nil
}

// keys metric.
func (c Client) Keys(ctx context.Context) (uint64, uint64, uint64, error) {
var (
// addressTypes []warden.AddressType
pendingKeys uint64
ecdsaKeys uint64
eddsaKeys uint64
key []byte
)

client := warden.NewQueryClient(c.conn)

for {
req := warden.QueryAllKeysRequest{Pagination: &query.PageRequest{Key: key}}

allKeys, err := client.AllKeys(ctx, &req)
if err != nil {
return 0, 0, 0, endpointError(err.Error())
}

for _, key := range allKeys.Keys {
if key.Key.Type == warden.KeyType_KEY_TYPE_ECDSA_SECP256K1 {
ecdsaKeys++
continue
}

if key.Key.Type == warden.KeyType_KEY_TYPE_EDDSA_ED25519 {
eddsaKeys++
continue
}

pendingKeys++
}

if allKeys.GetPagination() == nil {
break
}

key = allKeys.Pagination.GetNextKey()
if len(key) == 0 {
break
}
}

return ecdsaKeys, eddsaKeys, pendingKeys, nil
}

// Keychains metric.
func (c Client) Keychains(ctx context.Context) (uint64, error) {
var key []byte

client := warden.NewQueryClient(c.conn)

req := warden.QueryKeychainsRequest{Pagination: &query.PageRequest{Key: key}}

keyChains, err := client.Keychains(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return keyChains.Pagination.Total, nil
}

// Intents.
func (c Client) Intents(ctx context.Context) (uint64, error) {
var key []byte

client := intent.NewQueryClient(c.conn)

req := intent.QueryIntentsRequest{Pagination: &query.PageRequest{Key: key}}

intents, err := client.Intents(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return intents.Pagination.Total, nil
}

// Actions.
func (c Client) Actions(ctx context.Context) (uint64, error) {
var key []byte

client := intent.NewQueryClient(c.conn)

req := intent.QueryActionsRequest{Pagination: &query.PageRequest{Key: key}}

actions, err := client.Actions(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return actions.Pagination.Total, nil
}

// Accounts.
func (c Client) Accounts(ctx context.Context) (uint64, error) {
var key []byte

client := auth.NewQueryClient(c.conn)

req := auth.QueryAccountsRequest{Pagination: &query.PageRequest{Key: key}}

accounts, err := client.Accounts(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return accounts.Pagination.Total, nil
}
24 changes: 24 additions & 0 deletions pkg/grpc/grpcAuth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package grpc

import (
"context"

"github.com/cosmos/cosmos-sdk/types/query"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// Accounts.
func (c Client) Accounts(ctx context.Context) (uint64, error) {
var key []byte

client := auth.NewQueryClient(c.conn)

req := auth.QueryAccountsRequest{Pagination: &query.PageRequest{Key: key}}

accounts, err := client.Accounts(ctx, &req)
if err != nil {
return 0, endpointError(err.Error())
}

return accounts.Pagination.Total, nil
}
Loading

0 comments on commit f45ce70

Please sign in to comment.