Skip to content

Commit

Permalink
feat: Add labels for slashing events (#3)
Browse files Browse the repository at this point in the history
* feat: Add labels for slashing events

* chore: Update README.md

* Update pkg/grpc/grpc.go

Co-authored-by: Joonas Lehtimäki <[email protected]>

* fix: Remove white spaces

---------

Co-authored-by: Joonas Lehtimäki <[email protected]>
  • Loading branch information
kayano and jlehtimaki authored Jan 16, 2024
1 parent 213a436 commit 69e706f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Usage of validator-exporter:
"GRPC_TLS_ENABLED" envDefault:"true"
"GRPC_TIMEOUT_SECONDS" envDefault:"5"
"PREFIX" envDefault:"archway"
"CHAIN_NAME" envDefault:"archway"
"CHAIN_ID" envDefault:"constantine-3"
```
### Connecting to archway constantine testnet by default
```
Expand All @@ -34,7 +36,7 @@ GRPC_TLS_ENABLED=false GRPC_ADDR=localhost:9090 validator-exporter --log-level d
```
# HELP cosmos_validator_missed_blocks Returns missed blocks for a validator.
# TYPE cosmos_validator_missed_blocks gauge
cosmos_validator_missed_blocks{moniker="validator_1",valcons="archwayvalcons18le5pevj6sdynyksn77n9z9g8394l3xqk04s3z",valoper="archwayvaloper172zqrqtrwfplwhec44050dhuv66ekcmty4hnfv"} 0
cosmos_validator_missed_blocks{moniker="validator_2",valcons="archwayvalcons1z4q9zpe8l8puwv8aq4dqadkz4zm244pnu72qcd",valoper="archwayvaloper1370vgzkv5l3kylcylwekzjcdt2hjk2k8zrht6c"} 0
cosmos_validator_missed_blocks{moniker="validator_3",valcons="archwayvalcons1ep8hnygqw8gvsdfvyanhcfsmvlrvae4s9hljta",valoper="archwayvaloper1scxt3mgxmw3z2hpf8k4mlssz5qvljmtaplv6nz"} 2
cosmos_validator_missed_blocks{bond_status="bonded",chain_id="localnet",chain_name="archway",jailed="false",moniker="validator_1",tombstoned="false",valcons="archwayvalcons1gzfu5aqsqsljmgs5m3eyklwq9ufrpmlfhxhxjd",valoper="archwayvaloper172zqrqtrwfplwhec44050dhuv66ekcmty4hnfv"} 0
cosmos_validator_missed_blocks{bond_status="bonded",chain_id="localnet",chain_name="archway",jailed="false",moniker="validator_2",tombstoned="false",valcons="archwayvalcons1yf253kfa2g3qk2727nltndzll546wrrtdr5d3x",valoper="archwayvaloper1370vgzkv5l3kylcylwekzjcdt2hjk2k8zrht6c"} 0
cosmos_validator_missed_blocks{bond_status="bonded",chain_id="localnet",chain_name="archway",jailed="false",moniker="validator_3",tombstoned="false",valcons="archwayvalcons1ve4g3nth72p6jq92d5cfk9kq0j48jr0m5h7wk5",valoper="archwayvaloper1scxt3mgxmw3z2hpf8k4mlssz5qvljmtaplv6nz"} 2
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/caarlos0/env/v10 v10.0.0
github.com/cosmos/cosmos-sdk v0.50.2
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.60.0
)
Expand Down Expand Up @@ -123,7 +124,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"context"
"fmt"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -22,9 +23,14 @@ var missedBlocks = prometheus.NewDesc(
missedBlocksMetricName,
"Returns missed blocks for a validator.",
[]string{
"chain_name",
"chain_id",
"valcons",
"valoper",
"moniker",
"jailed",
"tombstoned",
"bond_status",
},
nil,
)
Expand Down Expand Up @@ -70,9 +76,14 @@ func (vc ValidatorsCollector) missedBlocksMetrics(vals []types.Validator) []prom
prometheus.GaugeValue,
float64(val.MissedBlocks),
[]string{
vc.Cfg.ChainName,
vc.Cfg.ChainID,
val.ConsAddress,
val.OperatorAddress,
val.Moniker,
strconv.FormatBool(val.Jailed),
strconv.FormatBool(val.Tombstoned),
val.BondStatus,
}...,
),
)
Expand Down
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.constantine.archway.tech:443"`
TLS bool `env:"GRPC_TLS_ENABLED" envDefault:"true"`
Timeout int `env:"GRPC_TIMEOUT_SECONDS" envDefault:"5"`
Prefix string `env:"PREFIX" envDefault:"archway"`
Addr string `env:"GRPC_ADDR" envDefault:"grpc.constantine.archway.tech:443"`
TLS bool `env:"GRPC_TLS_ENABLED" envDefault:"true"`
Timeout int `env:"GRPC_TIMEOUT_SECONDS" envDefault:"5"`
Prefix string `env:"PREFIX" envDefault:"archway"`
ChainName string `env:"CHAIN_NAME" envDefault:"archway"`
ChainID string `env:"CHAIN_ID" envDefault:"constantine-3"`
}

func (c Config) GRPCConn() (*grpc.ClientConn, error) {
Expand Down
15 changes: 14 additions & 1 deletion pkg/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

base "cosmossdk.io/api/cosmos/base/tendermint/v1beta1"

Expand All @@ -20,7 +21,10 @@ import (
log "github.com/archway-network/validator-exporter/pkg/logger"
)

const valConsStr = "valcons"
const (
valConsStr = "valcons"
bondStatusPrefix = "BOND_STATUS_"
)

var errEndpoint = errors.New("grpc error")

Expand Down Expand Up @@ -198,6 +202,9 @@ func SigningValidators(ctx context.Context, cfg config.Config) ([]types.Validato
OperatorAddress: valsMap[info.Address].OperatorAddress,
Moniker: valsMap[info.Address].Description.Moniker,
MissedBlocks: info.MissedBlocksCounter,
Jailed: valsMap[info.Address].IsJailed(),
Tombstoned: info.Tombstoned,
BondStatus: bondStatus(valsMap[info.Address].GetStatus()),
})
}

Expand Down Expand Up @@ -234,3 +241,9 @@ func LatestBlockHeight(ctx context.Context, cfg config.Config) (int64, error) {

return height, nil
}

func bondStatus(status staking.BondStatus) string {
statusWithoutPrefix, _ := strings.CutPrefix(status.String(), bondStatusPrefix)

return strings.ToLower(statusWithoutPrefix)
}
17 changes: 17 additions & 0 deletions pkg/grpc/grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package grpc

import (
"testing"

staking "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/assert"
)

func TestBondStatus(t *testing.T) {
status := staking.Bonded

exp := "bonded"
res := bondStatus(status)

assert.Equal(t, exp, res)
}
3 changes: 3 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ type Validator struct {
ConsAddress string
MissedBlocks int64
Moniker string
Jailed bool
Tombstoned bool
BondStatus string
}

0 comments on commit 69e706f

Please sign in to comment.