Skip to content

Commit

Permalink
feat: add denom option & rename token-exponent to denom-exponent (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Oct 9, 2023
1 parent 60a9645 commit e14650e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ GLOBAL OPTIONS:
--node value [ --node value ] rpc node endpoint to connect to (specify multiple for high availability) (default: "http://localhost:26657")
--no-gov disable calls to gov module (useful for consumer chains) (default: false)
--no-staking disable calls to staking module (useful for consumer chains) (default: false)
--denom value denom used in metrics label (eg. atom or uatom)
--denom-exponent value denom exponent (eg. 6 for atom, 1 for uatom) (default: 0)
--validator value [ --validator value ] validator address(es) to track (use :my-label to add a custom label in metrics & ouput)
--x-gov value version of the gov module to use (v1|v1beta1) (default: "v1beta1")
--help, -h show help
--version, -v print the version
```
Expand Down
9 changes: 6 additions & 3 deletions pkg/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ var Flags = []cli.Flag{
Name: "no-staking",
Usage: "disable calls to staking module (useful for consumer chains)",
},
&cli.StringFlag{
Name: "denom",
Usage: "denom used in metrics label (eg. atom or uatom)",
},
&cli.UintFlag{
Name: "token-exponent",
Usage: "token exponent (ie. 6 for uatom)",
Value: 6,
Name: "denom-exponent",
Usage: "denom exponent (eg. 6 for atom, 1 for uatom)",
},
&cli.StringSliceFlag{
Name: "validator",
Expand Down
6 changes: 4 additions & 2 deletions pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func RunFunc(cCtx *cli.Context) error {
nodes = cCtx.StringSlice("node")
noGov = cCtx.Bool("no-gov")
noStaking = cCtx.Bool("no-staking")
tokenExpon = cCtx.Uint("token-exponent")
denom = cCtx.String("denom")
denomExpon = cCtx.Uint("denom-exponent")
validators = cCtx.StringSlice("validator")
xGov = cCtx.String("x-gov")
)
Expand Down Expand Up @@ -97,7 +98,8 @@ func RunFunc(cCtx *cli.Context) error {
//
if !noStaking {
validatorsWatcher := watcher.NewValidatorsWatcher(trackedValidators, metrics, pool, watcher.ValidatorsWatcherOptions{
TokenExponent: tokenExpon,
Denom: denom,
DenomExponent: denomExpon,
})
errg.Go(func() error {
return validatorsWatcher.Start(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func New(namespace string) *Metrics {
Name: "seat_price",
Help: "Min seat price to be in the active set (ie. bonded tokens of the latest validator)",
},
[]string{"chain_id"},
[]string{"chain_id", "denom"},
),
Rank: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -113,7 +113,7 @@ func New(namespace string) *Metrics {
Name: "tokens",
Help: "Number of staked tokens per validator",
},
[]string{"chain_id", "address", "name"},
[]string{"chain_id", "address", "name", "denom"},
),
IsBonded: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down
17 changes: 9 additions & 8 deletions pkg/watcher/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type ValidatorsWatcher struct {
}

type ValidatorsWatcherOptions struct {
TokenExponent uint
Denom string
DenomExponent uint
}

func NewValidatorsWatcher(validators []TrackedValidator, metrics *metrics.Metrics, pool *rpc.Pool, opts ValidatorsWatcherOptions) *ValidatorsWatcher {
Expand Down Expand Up @@ -77,18 +78,18 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin
// Sort validators by tokens & status (bonded, unbonded, jailed)
sort.Sort(RankedValidators(validators))

tokenExponent := w.opts.TokenExponent
if tokenExponent == 0 {
tokenExponent = 1
denomExponent := w.opts.DenomExponent
if denomExponent == 0 {
denomExponent = 1
}

seatPrice := decimal.Zero
for _, val := range validators {
tokens := decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(tokenExponent))
tokens := decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(denomExponent))
if val.Status == staking.Bonded && (seatPrice.IsZero() || seatPrice.GreaterThan(tokens)) {
seatPrice = tokens
}
w.metrics.SeatPrice.WithLabelValues(chainID).Set(seatPrice.InexactFloat64())
w.metrics.SeatPrice.WithLabelValues(chainID, w.opts.Denom).Set(seatPrice.InexactFloat64())
}

for _, tracked := range w.validators {
Expand All @@ -103,11 +104,11 @@ func (w *ValidatorsWatcher) handleValidators(chainID string, validators []stakin
rank = i + 1
isBonded = val.Status == staking.Bonded
isJailed = val.Jailed
tokens = decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(tokenExponent))
tokens = decimal.NewFromBigInt(val.Tokens.BigInt(), -int32(denomExponent))
)

w.metrics.Rank.WithLabelValues(chainID, address, name).Set(float64(rank))
w.metrics.Tokens.WithLabelValues(chainID, address, name).Set(tokens.InexactFloat64())
w.metrics.Tokens.WithLabelValues(chainID, address, name, w.opts.Denom).Set(tokens.InexactFloat64())
w.metrics.IsBonded.WithLabelValues(chainID, address, name).Set(metrics.BoolToFloat64(isBonded))
w.metrics.IsJailed.WithLabelValues(chainID, address, name).Set(metrics.BoolToFloat64(isJailed))
break
Expand Down
5 changes: 3 additions & 2 deletions pkg/watcher/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestValidatorsWatcher(t *testing.T) {
metrics.New("cosmos_validator_watcher"),
nil,
ValidatorsWatcherOptions{
TokenExponent: 6,
Denom: "denom",
DenomExponent: 6,
},
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func TestValidatorsWatcher(t *testing.T) {

validatorsWatcher.handleValidators(chainID, validators)

assert.Equal(t, float64(42), testutil.ToFloat64(validatorsWatcher.metrics.Tokens.WithLabelValues(chainID, kilnAddress, kilnName)))
assert.Equal(t, float64(42), testutil.ToFloat64(validatorsWatcher.metrics.Tokens.WithLabelValues(chainID, kilnAddress, kilnName, "denom")))
assert.Equal(t, float64(2), testutil.ToFloat64(validatorsWatcher.metrics.Rank.WithLabelValues(chainID, kilnAddress, kilnName)))
assert.Equal(t, float64(1), testutil.ToFloat64(validatorsWatcher.metrics.IsBonded.WithLabelValues(chainID, kilnAddress, kilnName)))
assert.Equal(t, float64(0), testutil.ToFloat64(validatorsWatcher.metrics.IsJailed.WithLabelValues(chainID, kilnAddress, kilnName)))
Expand Down

0 comments on commit e14650e

Please sign in to comment.