Skip to content

Commit

Permalink
feat: change metric logic to only have one
Browse files Browse the repository at this point in the history
  • Loading branch information
jlehtimaki committed Oct 31, 2023
1 parent bceaf10 commit 321b778
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 76 deletions.
55 changes: 8 additions & 47 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import (
)

const (
successStatus = "success"
errorStatus = "error"
clientExpiryMetricName = "cosmos_ibc_client_expiry"
walletBalanceMetricName = "cosmos_wallet_balance"
channelStuckPacketsMetricName = "cosmos_ibc_stuck_packets_total"
channelSrcStuckPacketsMetricName = "cosmos_ibc_stuck_packets_src"
channelDstStuckPacketsMetricName = "cosmos_ibc_stuck_packets_dst"
successStatus = "success"
errorStatus = "error"
clientExpiryMetricName = "cosmos_ibc_client_expiry"
walletBalanceMetricName = "cosmos_wallet_balance"
channelStuckPacketsMetricName = "cosmos_ibc_stuck_packets"
)

var (
Expand All @@ -43,30 +41,6 @@ var (
},
nil,
)
channelSrcStuckPackets = prometheus.NewDesc(
channelSrcStuckPacketsMetricName,
"Returns source stuck packets for a channel.",
[]string{
"src_channel_id",
"dst_channel_id",
"src_chain_id",
"dst_chain_id",
"status",
},
nil,
)
channelDstStuckPackets = prometheus.NewDesc(
channelDstStuckPacketsMetricName,
"Returns destination stuck packets for a channel.",
[]string{
"src_channel_id",
"dst_channel_id",
"src_chain_id",
"dst_chain_id",
"status",
},
nil,
)
walletBalance = prometheus.NewDesc(
walletBalanceMetricName,
"Returns wallet balance for an address on a chain.",
Expand Down Expand Up @@ -141,19 +115,6 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
channelStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Total),
[]string{
sp.Source,
sp.Destination,
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
(*cc.RPCs)[path.Chain2.ChainName].ChainID,
status,
}...,
)

ch <- prometheus.MustNewConstMetric(
channelSrcStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Source),
[]string{
sp.Source,
Expand All @@ -165,14 +126,14 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
)

ch <- prometheus.MustNewConstMetric(
channelDstStuckPackets,
channelStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Destination),
[]string{
sp.Source,
sp.Destination,
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
sp.Source,
(*cc.RPCs)[path.Chain2.ChainName].ChainID,
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
status,
}...,
)
Expand Down
54 changes: 25 additions & 29 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ type ClientsInfo struct {
}

type ChannelsInfo struct {
ChainA *relayer.Chain
ChainB *relayer.Chain
Channels []Channel
}

type Channel struct {
Source string
Destination string
StuckPackets struct {
Source string
Destination string
SourcePort string
DestinationPort string
Ordering string
StuckPackets struct {
Source int
Destination int
Total int
}
}

Expand Down Expand Up @@ -95,8 +95,19 @@ func GetChannelsInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (Channel
ctx := context.Background()
channelInfo := ChannelsInfo{}

// Init channel data
for _, c := range ibc.Channels {
var channel Channel
channel.Source = c.Chain1.ChannelID
channel.Destination = c.Chain2.ChannelID
channel.SourcePort = c.Chain1.PortID
channel.DestinationPort = c.Chain2.PortID
channel.Ordering = c.Ordering
channelInfo.Channels = append(channelInfo.Channels, channel)
}

if (*rpcs)[ibc.Chain1.ChainName].ChainID == "" || (*rpcs)[ibc.Chain2.ChainName].ChainID == "" {
return ChannelsInfo{}, fmt.Errorf(
return channelInfo, fmt.Errorf(
"Error: RPC data is missing, cannot retrieve channel data: %v",
ibc.Channels,
)
Expand Down Expand Up @@ -128,24 +139,12 @@ func GetChannelsInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (Channel
if _, _, err := relayer.QueryLatestHeights(
ctx, chainA, chainB,
); err != nil {
for _, c := range ibc.Channels {
var channel Channel
channel.Source = c.Chain1.ChannelID
channel.Destination = c.Chain2.ChannelID
channelInfo.Channels = append(channelInfo.Channels, channel)
}

return channelInfo, fmt.Errorf("Error: %w for %v", err, cdA)
}

for _, c := range ibc.Channels {
for i, c := range channelInfo.Channels {
var order chantypes.Order

var channel Channel

channel.Source = c.Chain1.ChannelID
channel.Destination = c.Chain2.ChannelID

switch c.Ordering {
case "none":
order = chantypes.NONE
Expand All @@ -159,20 +158,17 @@ func GetChannelsInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (Channel
State: stateOpen,
Ordering: order,
Counterparty: chantypes.Counterparty{
PortId: c.Chain2.PortID,
ChannelId: c.Chain2.ChannelID,
PortId: c.DestinationPort,
ChannelId: c.Destination,
},
PortId: c.Chain1.PortID,
ChannelId: c.Chain2.ChannelID,
PortId: c.SourcePort,
ChannelId: c.Source,
}

unrelayedSequences := relayer.UnrelayedSequences(ctx, chainA, chainB, &ch)

channel.StuckPackets.Total += len(unrelayedSequences.Src) + len(unrelayedSequences.Dst)
channel.StuckPackets.Source += len(unrelayedSequences.Src)
channel.StuckPackets.Destination += len(unrelayedSequences.Dst)

channelInfo.Channels = append(channelInfo.Channels, channel)
channelInfo.Channels[i].StuckPackets.Source += len(unrelayedSequences.Src)
channelInfo.Channels[i].StuckPackets.Destination += len(unrelayedSequences.Dst)
}

return channelInfo, nil
Expand Down

0 comments on commit 321b778

Please sign in to comment.