Skip to content

Commit

Permalink
refactor(core)move telemetry into internal folder, move metric.go in …
Browse files Browse the repository at this point in the history
…separate folder for core/02-client.
  • Loading branch information
DimitrisJim committed Jul 4, 2024
1 parent 356e433 commit ecf03ed
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 113 deletions.
6 changes: 3 additions & 3 deletions modules/apps/transfer/internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"

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

func ReportTransferTelemetry(tokens types.Tokens, labels []metrics.Label) {
Expand All @@ -18,7 +18,7 @@ func ReportTransferTelemetry(tokens types.Tokens, labels []metrics.Label) {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "ibc", "transfer"},
float32(amount.Int64()),
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, token.Denom.Path())},
[]metrics.Label{telemetry.NewLabel(coremetrics.LabelDenom, token.Denom.Path())},
)
}
}
Expand All @@ -35,7 +35,7 @@ func ReportOnRecvPacketTelemetry(transferAmount sdkmath.Int, denomPath string, l
telemetry.SetGaugeWithLabels(
[]string{"ibc", types.ModuleName, "packet", "receive"},
float32(transferAmount.Int64()),
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, denomPath)},
[]metrics.Label{telemetry.NewLabel(coremetrics.LabelDenom, denomPath)},
)
}

Expand Down
18 changes: 9 additions & 9 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
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"
coremetrics "github.com/cosmos/ibc-go/v8/modules/core/metrics"
)

// sendTransfer handles transfer sending logic. There are 2 possible cases:
Expand Down Expand Up @@ -94,8 +94,8 @@ func (k Keeper) sendTransfer(
destinationChannel := channel.Counterparty.ChannelId

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

// begin createOutgoingPacket logic
Expand All @@ -120,7 +120,7 @@ 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"))
labels = append(labels, telemetry.NewLabel(coremetrics.LabelSource, "false"))

// transfer the coins to the module account and burn them
if err := k.bankKeeper.SendCoinsFromAccountToModule(
Expand All @@ -138,7 +138,7 @@ 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"))
labels = append(labels, telemetry.NewLabel(coremetrics.LabelSource, "true"))

// obtain the escrow address for the source channel end
escrowAddress := types.GetEscrowAddress(sourcePort, sourceChannel)
Expand Down Expand Up @@ -190,8 +190,8 @@ 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()),
telemetry.NewLabel(coremetrics.LabelSourcePort, packet.GetSourcePort()),
telemetry.NewLabel(coremetrics.LabelSourceChannel, packet.GetSourceChannel()),
}

// parse the transfer amount
Expand Down Expand Up @@ -225,7 +225,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
}

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

// Appending token. The new denom has been computed
Expand Down Expand Up @@ -272,7 +272,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
}

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

receivedCoins = append(receivedCoins, voucher)
Expand Down
49 changes: 6 additions & 43 deletions modules/core/02-client/keeper/client.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package keeper

import (
metrics "github.com/hashicorp/go-metrics"

errorsmod "cosmossdk.io/errors"

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

"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/cosmos/ibc-go/v8/modules/core/internal/telemetry"
)

// CreateClient generates a new client identifier and invokes the associated light client module in order to
Expand Down Expand Up @@ -48,11 +46,7 @@ func (k *Keeper) CreateClient(ctx sdk.Context, clientType string, clientState, c
initialHeight := clientModule.LatestHeight(ctx, clientID)
k.Logger(ctx).Info("client created at height", "client-id", clientID, "height", initialHeight.String())

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "create"},
1,
[]metrics.Label{telemetry.NewLabel(types.LabelClientType, clientType)},
)
defer telemetry.ReportCreateClient(clientType)

emitCreateClientEvent(ctx, clientID, clientType, initialHeight)

Expand Down Expand Up @@ -85,15 +79,7 @@ func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg export

k.Logger(ctx).Info("client frozen due to misbehaviour", "client-id", clientID)

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "misbehaviour"},
1,
[]metrics.Label{
telemetry.NewLabel(types.LabelClientType, clientType),
telemetry.NewLabel(types.LabelClientID, clientID),
telemetry.NewLabel(types.LabelMsgType, "update"),
},
)
defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID)

emitSubmitMisbehaviourEvent(ctx, clientID, clientType)

Expand All @@ -104,15 +90,7 @@ func (k *Keeper) UpdateClient(ctx sdk.Context, clientID string, clientMsg export

k.Logger(ctx).Info("client state updated", "client-id", clientID, "heights", consensusHeights)

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "update"},
1,
[]metrics.Label{
telemetry.NewLabel(types.LabelClientType, clientType),
telemetry.NewLabel(types.LabelClientID, clientID),
telemetry.NewLabel(types.LabelUpdateType, "msg"),
},
)
defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID)

// emitting events in the keeper emits for both begin block and handler client updates
emitUpdateClientEvent(ctx, clientID, clientType, consensusHeights, k.cdc, clientMsg)
Expand Down Expand Up @@ -148,14 +126,7 @@ func (k *Keeper) UpgradeClient(
latestHeight := clientModule.LatestHeight(ctx, clientID)
k.Logger(ctx).Info("client state upgraded", "client-id", clientID, "height", latestHeight.String())

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "upgrade"},
1,
[]metrics.Label{
telemetry.NewLabel(types.LabelClientType, clientType),
telemetry.NewLabel(types.LabelClientID, clientID),
},
)
defer telemetry.ReportUpgradeClient(clientType, clientID)

emitUpgradeClientEvent(ctx, clientID, clientType, latestHeight)

Expand Down Expand Up @@ -198,15 +169,7 @@ func (k *Keeper) RecoverClient(ctx sdk.Context, subjectClientID, substituteClien

k.Logger(ctx).Info("client recovered", "client-id", subjectClientID)

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "update"},
1,
[]metrics.Label{
telemetry.NewLabel(types.LabelClientType, clientType),
telemetry.NewLabel(types.LabelClientID, subjectClientID),
telemetry.NewLabel(types.LabelUpdateType, "recovery"),
},
)
defer telemetry.ReportRecoverClient(clientType, subjectClientID)

// emitting events in the keeper for recovering clients
emitRecoverClientEvent(ctx, subjectClientID, clientType)
Expand Down
9 changes: 0 additions & 9 deletions modules/core/02-client/types/metrics.go

This file was deleted.

58 changes: 58 additions & 0 deletions modules/core/internal/telemetry/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package telemetry

import (
metrics "github.com/hashicorp/go-metrics"

"github.com/cosmos/cosmos-sdk/telemetry"

ibcmetrics "github.com/cosmos/ibc-go/v8/modules/core/metrics"
)

func ReportCreateClient(clientType string) {
telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "create"},
1,
[]metrics.Label{telemetry.NewLabel(ibcmetrics.LabelClientType, clientType)},
)
}

func ReportUpdateClient(foundMisbehaviour bool, clientType, clientID string) {
labels := []metrics.Label{
telemetry.NewLabel(ibcmetrics.LabelClientType, clientType),
telemetry.NewLabel(ibcmetrics.LabelClientID, clientID),
}

var updateType string
if foundMisbehaviour {
labels = append(labels, telemetry.NewLabel(ibcmetrics.LabelMsgType, "update"))
updateType = "misbehaviour"
} else {
labels = append(labels, telemetry.NewLabel(ibcmetrics.LabelUpdateType, "msg"))
updateType = "update"
}

telemetry.IncrCounterWithLabels([]string{"ibc", "client", updateType}, 1, labels)
}

func ReportUpgradeClient(clientType, clientID string) {
telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "upgrade"},
1,
[]metrics.Label{
telemetry.NewLabel(ibcmetrics.LabelClientType, clientType),
telemetry.NewLabel(ibcmetrics.LabelClientID, clientID),
},
)
}

func ReportRecoverClient(clientType, subjectClientID string) {
telemetry.IncrCounterWithLabels(
[]string{"ibc", "client", "update"},
1,
[]metrics.Label{
telemetry.NewLabel(ibcmetrics.LabelClientType, clientType),
telemetry.NewLabel(ibcmetrics.LabelClientID, subjectClientID),
telemetry.NewLabel(ibcmetrics.LabelUpdateType, "recovery"),
},
)
}
44 changes: 44 additions & 0 deletions modules/core/internal/telemetry/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package telemetry

import (
metrics "github.com/hashicorp/go-metrics"

"github.com/cosmos/cosmos-sdk/telemetry"

"github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ibcmetrics "github.com/cosmos/ibc-go/v8/modules/core/metrics"
)

func ReportRecvPacket(packet types.Packet) {
telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ibc", types.EventTypeRecvPacket},
1,
addPacketLabels(packet),
)
}

func ReportTimeoutPacket(packet types.Packet, timeoutType string) {
labels := append(addPacketLabels(packet), telemetry.NewLabel(ibcmetrics.LabelTimeoutType, timeoutType))
telemetry.IncrCounterWithLabels(
[]string{"ibc", "timeout", "packet"},
1,
labels,
)
}

func ReportAcknowledgePacket(packet types.Packet) {
telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ibc", types.EventTypeAcknowledgePacket},
1,
addPacketLabels(packet),
)
}

func addPacketLabels(packet types.Packet) []metrics.Label {
return []metrics.Label{
telemetry.NewLabel(ibcmetrics.LabelSourcePort, packet.SourcePort),
telemetry.NewLabel(ibcmetrics.LabelSourceChannel, packet.SourceChannel),
telemetry.NewLabel(ibcmetrics.LabelDestinationPort, packet.DestinationPort),
telemetry.NewLabel(ibcmetrics.LabelDestinationChannel, packet.DestinationChannel),
}
}
50 changes: 5 additions & 45 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"context"
"errors"

metrics "github.com/hashicorp/go-metrics"

errorsmod "cosmossdk.io/errors"

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

clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
Expand All @@ -17,6 +14,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
"github.com/cosmos/ibc-go/v8/modules/core/internal/telemetry"
coretypes "github.com/cosmos/ibc-go/v8/modules/core/types"
)

Expand Down Expand Up @@ -510,16 +508,7 @@ func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPack
}
}

defer telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ibc", channeltypes.EventTypeRecvPacket},
1,
[]metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, msg.Packet.SourcePort),
telemetry.NewLabel(coretypes.LabelSourceChannel, msg.Packet.SourceChannel),
telemetry.NewLabel(coretypes.LabelDestinationPort, msg.Packet.DestinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, msg.Packet.DestinationChannel),
},
)
defer telemetry.ReportRecvPacket(msg.Packet)

ctx.Logger().Info("receive packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String())

Expand Down Expand Up @@ -581,17 +570,7 @@ func (k *Keeper) Timeout(goCtx context.Context, msg *channeltypes.MsgTimeout) (*
return nil, errorsmod.Wrap(err, "timeout packet callback failed")
}

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "timeout", "packet"},
1,
[]metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, msg.Packet.SourcePort),
telemetry.NewLabel(coretypes.LabelSourceChannel, msg.Packet.SourceChannel),
telemetry.NewLabel(coretypes.LabelDestinationPort, msg.Packet.DestinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, msg.Packet.DestinationChannel),
telemetry.NewLabel(coretypes.LabelTimeoutType, "height"),
},
)
defer telemetry.ReportTimeoutPacket(msg.Packet, "height")

ctx.Logger().Info("timeout packet callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String())

Expand Down Expand Up @@ -656,17 +635,7 @@ func (k *Keeper) TimeoutOnClose(goCtx context.Context, msg *channeltypes.MsgTime
return nil, errorsmod.Wrap(err, "timeout on close callback failed")
}

defer telemetry.IncrCounterWithLabels(
[]string{"ibc", "timeout", "packet"},
1,
[]metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, msg.Packet.SourcePort),
telemetry.NewLabel(coretypes.LabelSourceChannel, msg.Packet.SourceChannel),
telemetry.NewLabel(coretypes.LabelDestinationPort, msg.Packet.DestinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, msg.Packet.DestinationChannel),
telemetry.NewLabel(coretypes.LabelTimeoutType, "channel-closed"),
},
)
defer telemetry.ReportTimeoutPacket(msg.Packet, "channel-closed")

ctx.Logger().Info("timeout on close callback succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String())

Expand Down Expand Up @@ -723,16 +692,7 @@ func (k *Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAck
return nil, errorsmod.Wrap(err, "acknowledge packet callback failed")
}

defer telemetry.IncrCounterWithLabels(
[]string{"tx", "msg", "ibc", channeltypes.EventTypeAcknowledgePacket},
1,
[]metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, msg.Packet.SourcePort),
telemetry.NewLabel(coretypes.LabelSourceChannel, msg.Packet.SourceChannel),
telemetry.NewLabel(coretypes.LabelDestinationPort, msg.Packet.DestinationPort),
telemetry.NewLabel(coretypes.LabelDestinationChannel, msg.Packet.DestinationChannel),
},
)
defer telemetry.ReportAcknowledgePacket(msg.Packet)

ctx.Logger().Info("acknowledgement succeeded", "port-id", msg.Packet.SourcePort, "channel-id", msg.Packet.SourceChannel, "result", channeltypes.SUCCESS.String())

Expand Down
Loading

0 comments on commit ecf03ed

Please sign in to comment.