Skip to content

Commit

Permalink
nit: move telemetry out of if clauses. (#6799)
Browse files Browse the repository at this point in the history
* nit: move telemetry out of if clauses.

* chore: report telemetry after all error cases have been handled.
  • Loading branch information
DimitrisJim authored Jul 12, 2024
1 parent 086c647 commit 4c6120e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
37 changes: 24 additions & 13 deletions modules/apps/transfer/internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"

"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
coremetrics "github.com/cosmos/ibc-go/v8/modules/core/metrics"
)

Expand Down Expand Up @@ -39,22 +40,32 @@ func ReportTransfer(sourcePort, sourceChannel, destinationPort, destinationChann
)
}

func ReportOnRecvPacket(sourcePort, sourceChannel string, token types.Token) {
func ReportOnRecvPacket(packet channeltypes.Packet, tokens types.Tokens) {
labels := []metrics.Label{
telemetry.NewLabel(coremetrics.LabelSourcePort, sourcePort),
telemetry.NewLabel(coremetrics.LabelSourceChannel, sourceChannel),
telemetry.NewLabel(coremetrics.LabelSource, fmt.Sprintf("%t", token.Denom.HasPrefix(sourcePort, sourceChannel))),
telemetry.NewLabel(coremetrics.LabelSourcePort, packet.SourcePort),
telemetry.NewLabel(coremetrics.LabelSourceChannel, packet.SourceChannel),
}
// Transfer amount has already been parsed in caller.
transferAmount, _ := sdkmath.NewIntFromString(token.Amount)
denomPath := token.Denom.Path()

if transferAmount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"ibc", types.ModuleName, "packet", "receive"},
float32(transferAmount.Int64()),
[]metrics.Label{telemetry.NewLabel(coremetrics.LabelDenom, denomPath)},
)
for _, token := range tokens {
// Modify trace as Recv does.
if token.Denom.HasPrefix(packet.SourcePort, packet.SourceChannel) {
token.Denom.Trace = token.Denom.Trace[1:]
} else {
trace := []types.Hop{types.NewHop(packet.DestinationPort, packet.DestinationChannel)}
token.Denom.Trace = append(trace, token.Denom.Trace...)
}

// Transfer amount has already been parsed in caller.
transferAmount, ok := sdkmath.NewIntFromString(token.Amount)
if ok && transferAmount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"ibc", types.ModuleName, "packet", "receive"},
float32(transferAmount.Int64()),
[]metrics.Label{telemetry.NewLabel(coremetrics.LabelDenom, token.Denom.Path())},
)
}

labels = append(labels, telemetry.NewLabel(coremetrics.LabelSource, fmt.Sprintf("%t", token.Denom.HasPrefix(packet.SourcePort, packet.SourceChannel))))
}

telemetry.IncrCounterWithLabels(
Expand Down
8 changes: 3 additions & 5 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (k Keeper) sendTransfer(

events.EmitTransferEvent(ctx, sender.String(), receiver, tokens, memo, hops)

defer telemetry.ReportTransfer(sourcePort, sourceChannel, destinationPort, destinationChannel, tokens)
telemetry.ReportTransfer(sourcePort, sourceChannel, destinationPort, destinationChannel, tokens)

return sequence, nil
}
Expand Down Expand Up @@ -210,8 +210,6 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return err
}

defer telemetry.ReportOnRecvPacket(packet.GetSourcePort(), packet.GetSourceChannel(), token)

// Appending token. The new denom has been computed
receivedCoins = append(receivedCoins, coin)
} else {
Expand Down Expand Up @@ -252,8 +250,6 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return errorsmod.Wrapf(err, "failed to send coins to receiver %s", receiver.String())
}

defer telemetry.ReportOnRecvPacket(packet.GetSourcePort(), packet.GetSourceChannel(), token)

receivedCoins = append(receivedCoins, voucher)
}
}
Expand All @@ -265,6 +261,8 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
}
}

telemetry.ReportOnRecvPacket(packet, data.Tokens)

// The ibc_module.go module will return the proper ack.
return nil
}
Expand Down

0 comments on commit 4c6120e

Please sign in to comment.