Skip to content

Commit

Permalink
feat: split one metric to three
Browse files Browse the repository at this point in the history
this splits one total metric so that it will produce three metrics, src,dst and total
  • Loading branch information
jlehtimaki committed Oct 27, 2023
1 parent 54ffe5d commit fac6c90
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 31 deletions.
90 changes: 68 additions & 22 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
)

const (
successStatus = "success"
errorStatus = "error"
clientExpiryMetricName = "cosmos_ibc_client_expiry"
walletBalanceMetricName = "cosmos_wallet_balance"
channelStuckPacketsMetricName = "cosmos_ibc_stuck_packets"
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"
)

var (
Expand All @@ -32,11 +34,31 @@ var (
channelStuckPacketsMetricName,
"Returns stuck packets for a channel.",
[]string{
"channel_id",
"src_chain_id",
"dst_chain_id",
"status",
},
nil,
)
channelSrcStuckPackets = prometheus.NewDesc(
channelSrcStuckPacketsMetricName,
"Returns source stuck packets for a channel.",
[]string{
"channel_id",
"src_chain_id",
"dst_chain_id",
"status",
},
nil,
)
channelDstStuckPackets = prometheus.NewDesc(
channelDstStuckPacketsMetricName,
"Returns destination stuck packets for a channel.",
[]string{
"channel_id",
"target_chain_id",
"source_stuck_packets",
"destination_stuck_packets",
"src_chain_id",
"dst_chain_id",
"status",
},
nil,
Expand Down Expand Up @@ -89,26 +111,50 @@ func (cc IBCCollector) Collect(ch chan<- prometheus.Metric) {
log.Error(err.Error())
}

sp, err := ibc.GetChannelInfo(path, cc.RPCs)
stuckPackets, err := ibc.GetChannelInfo(path, cc.RPCs)
if err != nil {
status = errorStatus

log.Error(err.Error())
}

ch <- prometheus.MustNewConstMetric(
channelStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Source+sp.StuckPackets.Destination),
[]string{
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
path.Channels[0].Chain1.ChannelID,
(*cc.RPCs)[path.Chain2.ChainName].ChainID,
fmt.Sprintf("%d", sp.StuckPackets.Source),
fmt.Sprintf("%d", sp.StuckPackets.Destination),
status,
}...,
)
for _, sp := range stuckPackets.Channels {
ch <- prometheus.MustNewConstMetric(
channelStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Total),
[]string{
sp.Name,
(*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.Name,
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
(*cc.RPCs)[path.Chain2.ChainName].ChainID,
status,
}...,
)

ch <- prometheus.MustNewConstMetric(
channelDstStuckPackets,
prometheus.GaugeValue,
float64(sp.StuckPackets.Destination),
[]string{
sp.Name,
(*cc.RPCs)[path.Chain1.ChainName].ChainID,
(*cc.RPCs)[path.Chain2.ChainName].ChainID,
status,
}...,
)
}

ch <- prometheus.MustNewConstMetric(
clientExpiry,
Expand Down
32 changes: 23 additions & 9 deletions pkg/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ type ClientsInfo struct {
ChainBClientExpiration time.Time
}

type ChannelInfo struct {
type ChannelsInfo struct {
Channels []Channel
}

type Channel struct {
Name string
StuckPackets struct {
Source int
Destination int
Total int
}
}

Expand Down Expand Up @@ -80,9 +86,9 @@ func GetClientsInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ClientsI
return clientsInfo, nil
}

func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelInfo, error) {
func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelsInfo, error) {
ctx := context.Background()
channelInfo := ChannelInfo{}
channelInfo := ChannelsInfo{}

cdA := chain.Info{
ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID,
Expand All @@ -92,7 +98,7 @@ func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelI

chainA, err := chain.PrepChain(cdA)
if err != nil {
return ChannelInfo{}, fmt.Errorf("Error: %w for %v", err, cdA)
return ChannelsInfo{}, fmt.Errorf("Error: %w for %v", err, cdA)
}

cdB := chain.Info{
Expand All @@ -103,12 +109,16 @@ func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelI

chainB, err := chain.PrepChain(cdB)
if err != nil {
return ChannelInfo{}, fmt.Errorf("Error: %w for %v", err, cdB)
return ChannelsInfo{}, fmt.Errorf("Error: %w for %v", err, cdB)
}

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

var channel Channel

channel.Name = c.Chain1.ChannelID

switch c.Ordering {
case "none":
order = chantypes.NONE
Expand All @@ -118,7 +128,7 @@ func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelI
order = chantypes.ORDERED
}

channel := chantypes.IdentifiedChannel{
ch := chantypes.IdentifiedChannel{
State: 3,
Ordering: order,
Counterparty: chantypes.Counterparty{
Expand All @@ -129,9 +139,13 @@ func GetChannelInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ChannelI
ChannelId: c.Chain2.ChannelID,
}

unrelayedSequences := relayer.UnrelayedSequences(ctx, chainA, chainB, &channel)
channelInfo.StuckPackets.Source += len(unrelayedSequences.Src)
channelInfo.StuckPackets.Destination += len(unrelayedSequences.Dst)
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)
}

return channelInfo, nil
Expand Down

0 comments on commit fac6c90

Please sign in to comment.