Skip to content

Commit

Permalink
Export wallet address for Prometheus metrics (#1206)
Browse files Browse the repository at this point in the history
* export relayer address for pro

* address in updateFeesSpent

* make error messages consistent

* log error rather than return

* handle 0 balance
  • Loading branch information
boojamya authored Jun 8, 2023
1 parent e95dd80 commit 0981e67
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
21 changes: 12 additions & 9 deletions relayer/chains/cosmos/cosmos_chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,25 @@ func (ccp *CosmosChainProcessor) CurrentRelayerBalance(ctx context.Context) {
}

// Get the balance for the chain provider's key
relayerWalletBalance, err := ccp.chainProvider.QueryBalance(ctx, ccp.chainProvider.Key())
relayerWalletBalances, err := ccp.chainProvider.QueryBalance(ctx, ccp.chainProvider.Key())
if err != nil {
ccp.log.Error(
"Failed to query relayer balance",
zap.Error(err),
)
}

address, err := ccp.chainProvider.Address()
if err != nil {
ccp.log.Error(
"Failed to get relayer bech32 wallet addresss",
zap.Error(err),
)
}
// Print the relevant gas prices
for _, gasDenom := range *ccp.parsedGasPrices {
for _, balance := range relayerWalletBalance {
if balance.Denom == gasDenom.Denom {
// Convert to a big float to get a float64 for metrics
f, _ := big.NewFloat(0.0).SetInt(balance.Amount.BigInt()).Float64()
ccp.metrics.SetWalletBalance(ccp.chainProvider.ChainId(), ccp.chainProvider.Key(), balance.Denom, f)
}
}
bal := relayerWalletBalances.AmountOf(gasDenom.Denom)
// Convert to a big float to get a float64 for metrics
f, _ := big.NewFloat(0.0).SetInt(bal.BigInt()).Float64()
ccp.metrics.SetWalletBalance(ccp.chainProvider.ChainId(), ccp.chainProvider.Key(), address, gasDenom.Denom, f)
}
}
14 changes: 10 additions & 4 deletions relayer/chains/cosmos/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,14 @@ func (cc *CosmosProvider) broadcastTx(
cc.LogFailedTx(rlyResp, err, msgs)
return err
}

cc.UpdateFeesSpent(cc.ChainId(), cc.Key(), fees)
address, err := cc.Address()
if err != nil {
cc.log.Error(
"failed to get relayer bech32 wallet addresss",
zap.Error(err),
)
}
cc.UpdateFeesSpent(cc.ChainId(), cc.Key(), address, fees)

// TODO: maybe we need to check if the node has tx indexing enabled?
// if not, we need to find a new way to block until inclusion in a block
Expand Down Expand Up @@ -1257,7 +1263,7 @@ func (cc *CosmosProvider) NewClientState(
}, nil
}

func (cc *CosmosProvider) UpdateFeesSpent(chain, key string, fees sdk.Coins) {
func (cc *CosmosProvider) UpdateFeesSpent(chain, key, address string, fees sdk.Coins) {
// Don't set the metrics in testing
if cc.metrics == nil {
return
Expand All @@ -1270,7 +1276,7 @@ func (cc *CosmosProvider) UpdateFeesSpent(chain, key string, fees sdk.Coins) {
for _, fee := range cc.TotalFees {
// Convert to a big float to get a float64 for metrics
f, _ := big.NewFloat(0.0).SetInt(fee.Amount.BigInt()).Float64()
cc.metrics.SetFeesSpent(chain, key, fee.GetDenom(), f)
cc.metrics.SetFeesSpent(chain, key, address, fee.GetDenom(), f)
}
}

Expand Down
10 changes: 5 additions & 5 deletions relayer/processor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ func (m *PrometheusMetrics) SetLatestHeight(chain string, height int64) {
m.LatestHeightGauge.WithLabelValues(chain).Set(float64(height))
}

func (m *PrometheusMetrics) SetWalletBalance(chain, key, denom string, balance float64) {
m.WalletBalance.WithLabelValues(chain, key, denom).Set(balance)
func (m *PrometheusMetrics) SetWalletBalance(chain, key, address, denom string, balance float64) {
m.WalletBalance.WithLabelValues(chain, key, address, denom).Set(balance)
}

func (m *PrometheusMetrics) SetFeesSpent(chain, key, denom string, amount float64) {
m.FeesSpent.WithLabelValues(chain, key, denom).Set(amount)
func (m *PrometheusMetrics) SetFeesSpent(chain, key, address, denom string, amount float64) {
m.FeesSpent.WithLabelValues(chain, key, address, denom).Set(amount)
}

func NewPrometheusMetrics() *PrometheusMetrics {
packetLabels := []string{"path", "chain", "channel", "port", "type"}
heightLabels := []string{"chain"}
walletLabels := []string{"chain", "key", "denom"}
walletLabels := []string{"chain", "key", "address", "denom"}
registry := prometheus.NewRegistry()
registerer := promauto.With(registry)
return &PrometheusMetrics{
Expand Down

0 comments on commit 0981e67

Please sign in to comment.