Skip to content

Commit

Permalink
nit(transfer): further simplify telemetry calls. (#6761)
Browse files Browse the repository at this point in the history
* nit(transfer): further simplify telemetry calls.

* use Cian's suggestion.
  • Loading branch information
DimitrisJim authored Jul 4, 2024
1 parent cd20ccf commit b99dccc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
22 changes: 20 additions & 2 deletions modules/apps/transfer/internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package telemetry

import (
"fmt"

"github.com/hashicorp/go-metrics"

sdkmath "cosmossdk.io/math"
Expand All @@ -11,7 +13,12 @@ import (
coretypes "github.com/cosmos/ibc-go/v8/modules/core/types"
)

func ReportTransferTelemetry(tokens types.Tokens, labels []metrics.Label) {
func ReportTransfer(sourcePort, sourceChannel, destinationPort, destinationChannel string, tokens types.Tokens) {
labels := []metrics.Label{
telemetry.NewLabel(coretypes.LabelDestinationPort, destinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, destinationChannel),
}

for _, token := range tokens {
amount, ok := sdkmath.NewIntFromString(token.Amount)
if ok && amount.IsInt64() {
Expand All @@ -21,6 +28,8 @@ func ReportTransferTelemetry(tokens types.Tokens, labels []metrics.Label) {
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, token.Denom.Path())},
)
}

labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, fmt.Sprintf("%t", !token.Denom.HasPrefix(sourcePort, sourceChannel))))
}

telemetry.IncrCounterWithLabels(
Expand All @@ -30,7 +39,16 @@ func ReportTransferTelemetry(tokens types.Tokens, labels []metrics.Label) {
)
}

func ReportOnRecvPacketTelemetry(transferAmount sdkmath.Int, denomPath string, labels []metrics.Label) {
func ReportOnRecvPacket(sourcePort, sourceChannel string, token types.Token) {
labels := []metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, sourcePort),
telemetry.NewLabel(coretypes.LabelSourceChannel, sourceChannel),
telemetry.NewLabel(coretypes.LabelSource, fmt.Sprintf("%t", token.Denom.HasPrefix(sourcePort, 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"},
Expand Down
30 changes: 4 additions & 26 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"fmt"
"strings"

"github.com/hashicorp/go-metrics"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/cosmos/ibc-go/v8/modules/apps/transfer/internal/events"
internaltelemetry "github.com/cosmos/ibc-go/v8/modules/apps/transfer/internal/telemetry"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/internal/telemetry"
internaltypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/internal/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
coretypes "github.com/cosmos/ibc-go/v8/modules/core/types"
)

// sendTransfer handles transfer sending logic. There are 2 possible cases:
Expand Down Expand Up @@ -93,11 +89,6 @@ func (k Keeper) sendTransfer(
destinationPort := channel.Counterparty.PortId
destinationChannel := channel.Counterparty.ChannelId

labels := []metrics.Label{
telemetry.NewLabel(coretypes.LabelDestinationPort, destinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, destinationChannel),
}

// begin createOutgoingPacket logic
// See spec for this logic: https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transfer#packet-relay
channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
Expand All @@ -120,8 +111,6 @@ func (k Keeper) sendTransfer(
// if the denom is prefixed by the port and channel on which we are sending
// the token, then we must be returning the token back to the chain they originated from
if token.Denom.HasPrefix(sourcePort, sourceChannel) {
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "false"))

// transfer the coins to the module account and burn them
if err := k.bankKeeper.SendCoinsFromAccountToModule(
ctx, sender, types.ModuleName, sdk.NewCoins(coin),
Expand All @@ -138,8 +127,6 @@ func (k Keeper) sendTransfer(
panic(fmt.Errorf("cannot burn coins after a successful send to a module account: %v", err))
}
} else {
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "true"))

// obtain the escrow address for the source channel end
escrowAddress := types.GetEscrowAddress(sourcePort, sourceChannel)
if err := k.escrowCoin(ctx, sender, escrowAddress, coin); err != nil {
Expand All @@ -162,7 +149,7 @@ func (k Keeper) sendTransfer(

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

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

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

receivedCoins := make(sdk.Coins, 0, len(data.Tokens))
for _, token := range data.Tokens {
labels := []metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, packet.GetSourcePort()),
telemetry.NewLabel(coretypes.LabelSourceChannel, packet.GetSourceChannel()),
}

// parse the transfer amount
transferAmount, ok := sdkmath.NewIntFromString(token.Amount)
if !ok {
Expand All @@ -224,9 +206,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return err
}

denomPath := token.Denom.Path()
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "true"))
defer internaltelemetry.ReportOnRecvPacketTelemetry(transferAmount, denomPath, labels)
defer telemetry.ReportOnRecvPacket(packet.GetSourcePort(), packet.GetSourceChannel(), token)

// Appending token. The new denom has been computed
receivedCoins = append(receivedCoins, coin)
Expand Down Expand Up @@ -268,9 +248,7 @@ 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())
}

denomPath := token.Denom.Path()
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "false"))
defer internaltelemetry.ReportOnRecvPacketTelemetry(transferAmount, denomPath, labels)
defer telemetry.ReportOnRecvPacket(packet.GetSourcePort(), packet.GetSourceChannel(), token)

receivedCoins = append(receivedCoins, voucher)
}
Expand Down

0 comments on commit b99dccc

Please sign in to comment.