Skip to content

Commit

Permalink
feat: Add status label for cosmos_ibc_client_expiry metric (#14)
Browse files Browse the repository at this point in the history
* feat: Add status label for cosmos_ibc_client_expiry metric

* chore: Add rpc for quicksilver in example config
  • Loading branch information
kayano authored Sep 26, 2023
1 parent e19debf commit f0dffc0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ rpc:
url: https://juno-rpc.polkachu.com:443
- chainId: kaiyo-1
url: https://kujira-rpc.polkachu.com:443
- chainId: noble-1
url: https://noble-rpc.polkachu.com:443
- chainId: nois-1
url: https://nois-rpc.polkachu.com:443
- chainId: osmo-test-5
url: https://rpc.osmotest5.osmosis.zone:443
- chainId: osmosis-1
url: https://osmosis-rpc.stakely.io:443
- chainId: quicksilver-2
url: https://rpc.quicksilver.zone:443
- chainId: sandbox-01
url: https://rpc.sandbox-01.aksh.pw:443
- chainId: theta-testnet-001
url: https://rpc.sentry-01.theta-testnet.polypore.xyz:443
- chainId: umee-1
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
cosmossdk.io/math v1.0.1
github.com/caarlos0/env/v9 v9.0.0
github.com/cosmos/relayer/v2 v2.4.1
github.com/google/go-cmp v0.5.9
github.com/google/go-github/v55 v55.0.0
github.com/prometheus/client_golang v1.15.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -87,6 +86,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/s2a-go v0.1.4 // indirect
Expand Down
9 changes: 0 additions & 9 deletions pkg/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import (
"github.com/archway-network/relayer_exporter/pkg/chain"
)

const (
successStatus = "success"
errorStatus = "error"
)

type Account struct {
Address string `yaml:"address"`
Denom string `yaml:"denom"`
ChainID string `yaml:"chainId"`
Balance math.Int
Status string
}

func (a *Account) GetBalance(rpcs map[string]string) error {
Expand All @@ -34,13 +28,10 @@ func (a *Account) GetBalance(rpcs map[string]string) error {

coins, err := chain.ChainProvider.QueryBalanceWithAddress(ctx, a.Address)
if err != nil {
a.Status = errorStatus

return err
}

a.Balance = coins.AmountOf(a.Denom)
a.Status = successStatus

return nil
}
58 changes: 40 additions & 18 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
)

const (
successStatus = "success"
errorStatus = "error"
clientExpiryMetricName = "cosmos_ibc_client_expiry"
walletBalanceMetricName = "cosmos_wallet_balance"
)
Expand All @@ -21,7 +23,7 @@ var (
clientExpiry = prometheus.NewDesc(
clientExpiryMetricName,
"Returns light client expiry in unixtime.",
[]string{"host_chain_id", "client_id", "target_chain_id"}, nil,
[]string{"host_chain_id", "client_id", "target_chain_id", "status"}, nil,
)
walletBalance = prometheus.NewDesc(
walletBalanceMetricName,
Expand All @@ -47,24 +49,41 @@ func (cc IBCClientsCollector) Describe(ch chan<- *prometheus.Desc) {
func (cc IBCClientsCollector) Collect(ch chan<- prometheus.Metric) {
log.Debug("Start collecting", zap.String("metric", clientExpiryMetricName))

clients := ibc.GetClientsInfos(cc.Paths, cc.RPCs)

for _, c := range clients {
ch <- prometheus.MustNewConstMetric(
clientExpiry,
prometheus.GaugeValue,
float64(c.ChainAClientExpiration.Unix()),
[]string{c.ChainA.ChainID(), c.ChainA.ClientID(), c.ChainB.ChainID()}...,
)

ch <- prometheus.MustNewConstMetric(
clientExpiry,
prometheus.GaugeValue,
float64(c.ChainBClientExpiration.Unix()),
[]string{c.ChainB.ChainID(), c.ChainB.ClientID(), c.ChainA.ChainID()}...,
)
var wg sync.WaitGroup

for _, p := range cc.Paths {
wg.Add(1)

go func(path *relayer.IBCdata) {
defer wg.Done()

ci, err := ibc.GetClientsInfo(path, cc.RPCs)
status := successStatus

if err != nil {
status = errorStatus

log.Error(err.Error())
}

ch <- prometheus.MustNewConstMetric(
clientExpiry,
prometheus.GaugeValue,
float64(ci.ChainAClientExpiration.Unix()),
[]string{path.Chain1.ChainName, path.Chain1.ClientID, path.Chain2.ChainName, status}...,
)

ch <- prometheus.MustNewConstMetric(
clientExpiry,
prometheus.GaugeValue,
float64(ci.ChainBClientExpiration.Unix()),
[]string{path.Chain2.ChainName, path.Chain2.ClientID, path.Chain1.ChainName, status}...,
)
}(p)
}

wg.Wait()

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

Expand All @@ -84,9 +103,12 @@ func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) {
defer wg.Done()

balance := 0.0
status := successStatus

err := account.GetBalance(wb.RPCs)
if err != nil {
status = errorStatus

log.Error(err.Error(), zap.Any("account", account))
} else {
// Convert to a big float to get a float64 for metrics
Expand All @@ -97,7 +119,7 @@ func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) {
walletBalance,
prometheus.GaugeValue,
balance,
[]string{account.Address, account.ChainID, account.Denom, account.Status}...,
[]string{account.Address, account.ChainID, account.Denom, status}...,
)
}(a)
}
Expand Down
34 changes: 0 additions & 34 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"time"

"github.com/archway-network/relayer_exporter/pkg/chain"
log "github.com/archway-network/relayer_exporter/pkg/logger"
"github.com/cosmos/relayer/v2/relayer"
"github.com/google/go-cmp/cmp"
)

type ClientsInfo struct {
Expand All @@ -20,38 +18,6 @@ type ClientsInfo struct {
ChainBClientExpiration time.Time
}

func GetClientsInfos(ibcs []*relayer.IBCdata, rpcs map[string]string) []ClientsInfo {
num := len(ibcs)

out := make(chan ClientsInfo, num)
defer close(out)

for i := 0; i < num; i++ {
go func(i int) {
clientsInfo, err := GetClientsInfo(ibcs[i], rpcs)
if err != nil {
out <- ClientsInfo{}

log.Error(err.Error())

return
}
out <- clientsInfo
}(i)
}

clientsInfos := []ClientsInfo{}

for i := 0; i < num; i++ {
ci := <-out
if !cmp.Equal(ci, ClientsInfo{}) {
clientsInfos = append(clientsInfos, ci)
}
}

return clientsInfos
}

func GetClientsInfo(ibc *relayer.IBCdata, rpcs map[string]string) (ClientsInfo, error) {
clientsInfo := ClientsInfo{}

Expand Down

0 comments on commit f0dffc0

Please sign in to comment.