Skip to content

Commit

Permalink
feat: add wallet metrics (#14)
Browse files Browse the repository at this point in the history
this will add wallet metrics that can be done with WALLET_ADDRESSES env
variable
  • Loading branch information
jlehtimaki authored Jun 5, 2024
1 parent 772db57 commit 7380145
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/warden-exporter/warden-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ func main() {
authCollector := collector.AuthCollector{
Cfg: cfg,
}
walletCollector := collector.WalletBalanceCollector{
Cfg: cfg,
}

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

if cfg.ValidatorMetrics {
Expand Down
76 changes: 76 additions & 0 deletions pkg/collector/wallet_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package collector

import (
"context"
"fmt"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"

"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"
)

const (
walletBalanceMetricName = "cosmos_wallet_balance"
)

//nolint:gochecknoglobals // this is needed as it's used in multiple places
var walletBalance = prometheus.NewDesc(
walletBalanceMetricName,
"Returns the wallet balance of account",
[]string{
"chain_id",
"account",
"denom",
"status",
},
nil,
)

type WalletBalanceCollector struct {
Cfg config.Config
}

func (w WalletBalanceCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- walletBalance
}

func (w WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) {
var balance uint64
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(w.Cfg.Timeout)*time.Second,
)

defer cancel()

status := successStatus

client, err := grpc.NewClient(w.Cfg)
if err != nil {
log.Error(fmt.Sprintf("error getting wallet balance metrics: %s", err))
}

addresses := strings.Split(w.Cfg.WalletAddresses, ",")
for _, addr := range addresses {
balance, err = client.Balance(ctx, addr, w.Cfg.Denom)
if err != nil {
log.Error(err.Error())
status = errorStatus
}
ch <- prometheus.MustNewConstMetric(
walletBalance,
prometheus.GaugeValue,
float64(balance),
[]string{
w.Cfg.ChainID,
addr,
w.Cfg.Denom,
status,
}...,
)
}
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Config struct {
ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1"`
WardenMetrics bool `env:"WARDEN_METRICS" envDefault:"true"`
ValidatorMetrics bool `env:"VALIDATOR_METRICS" envDefault:"true"`
WalletAddresses string `env:"WALLET_ADDRESSES" envDefault:""`
Denom string `env:"DENOM" envDefault:"uward"`
}

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

import (
"context"

bank "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func (c Client) Balance(ctx context.Context, address, denom string) (uint64, error) {
client := bank.NewQueryClient(c.conn)

req := bank.QueryBalanceRequest{Address: address, Denom: denom}

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

return balance.Balance.Amount.Uint64(), nil
}

0 comments on commit 7380145

Please sign in to comment.