From eb613c643c957d419f9d0b73fa6a31ecfc5211bc Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:43:17 +0800 Subject: [PATCH] refactor: remove dynamic labels from metrics (#629) --- telemetry/label.go | 9 -------- telemetry/metrics.go | 12 ++++------- x/crosschain/keeper/bridge_call_in.go | 14 +++++-------- x/crosschain/keeper/bridge_call_out.go | 26 ++++++++++-------------- x/crosschain/keeper/msg_server.go | 6 +++--- x/crosschain/keeper/oracle.go | 6 +++--- x/crosschain/keeper/outgoing_pool.go | 19 ++++++++--------- x/crosschain/keeper/send_to_fx.go | 19 +++++++++++++++++ x/crosschain/legacy/precompile_events.go | 12 ----------- x/crosschain/types/metrics.go | 15 -------------- x/migrate/keeper/msg_server.go | 10 ++------- 11 files changed, 55 insertions(+), 93 deletions(-) delete mode 100644 telemetry/label.go delete mode 100644 x/crosschain/types/metrics.go diff --git a/telemetry/label.go b/telemetry/label.go deleted file mode 100644 index 7699c8ad2..000000000 --- a/telemetry/label.go +++ /dev/null @@ -1,9 +0,0 @@ -package telemetry - -const ( - LabelSender = "sender" - LabelReceiver = "receiver" - LabelTo = "to" - LabelSuccess = "success" - LabelToken = "token" -) diff --git a/telemetry/metrics.go b/telemetry/metrics.go index a54313f97..2ffb002a3 100644 --- a/telemetry/metrics.go +++ b/telemetry/metrics.go @@ -12,21 +12,17 @@ import ( var maxFloat32, _ = big.NewInt(0).SetString(fmt.Sprintf("%.0f", math.MaxFloat32), 10) -func SetGaugeLabelsWithToken(keys []string, token string, amount *big.Int, labels ...metrics.Label) { +func SetGaugeLabelsWithDenom(keys []string, denom string, amount *big.Int, labels ...metrics.Label) { if amount.Cmp(maxFloat32) == 1 { return } amountFloat32, _ := new(big.Float).SetInt(amount).Float32() - telemetry.SetGaugeWithLabels(append(keys, token), amountFloat32, - append(labels, telemetry.NewLabel(LabelToken, token))) -} - -func SetGaugeLabelsWithCoin(keys []string, coin sdk.Coin, labels ...metrics.Label) { - SetGaugeLabelsWithToken(keys, coin.Denom, coin.Amount.BigInt(), labels...) + telemetry.SetGaugeWithLabels(append(keys, denom), amountFloat32, + append(labels, telemetry.NewLabel("denom", denom))) } func SetGaugeLabelsWithCoins(keys []string, coins sdk.Coins, labels ...metrics.Label) { for _, coin := range coins { - SetGaugeLabelsWithCoin(keys, coin, labels...) + SetGaugeLabelsWithDenom(keys, coin.Denom, coin.Amount.BigInt(), labels...) } } diff --git a/x/crosschain/keeper/bridge_call_in.go b/x/crosschain/keeper/bridge_call_in.go index b0e376c1b..e88764972 100644 --- a/x/crosschain/keeper/bridge_call_in.go +++ b/x/crosschain/keeper/bridge_call_in.go @@ -38,15 +38,11 @@ func (k Keeper) BridgeCallHandler(ctx sdk.Context, msg *types.MsgBridgeCallClaim if !ctx.IsCheckTx() { telemetry.IncrCounterWithLabels( - []string{types.ModuleName, types.MetricsKeyBridgeCallIn}, + []string{types.ModuleName, "bridge_call_in"}, float32(1), []metrics.Label{ - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), - telemetry.NewLabel(fxtelemetry.LabelSender, msg.GetSender()), - telemetry.NewLabel(fxtelemetry.LabelTo, msg.GetTo()), - telemetry.NewLabel(fxtelemetry.LabelSuccess, strconv.FormatBool(err == nil)), - telemetry.NewLabel(types.MetricsLabelRefund, msg.GetRefund()), - telemetry.NewLabel(types.MetricsLabelTxOrigin, msg.GetTxOrigin()), + telemetry.NewLabel("module", k.moduleName), + telemetry.NewLabel("success", strconv.FormatBool(err == nil)), }, ) } @@ -93,9 +89,9 @@ func (k Keeper) BridgeCallTransferAndCallEvm(ctx sdk.Context, sender, refundAddr if !ctx.IsCheckTx() { fxtelemetry.SetGaugeLabelsWithCoins( - []string{types.ModuleName, types.MetricsKeyBridgeCallIn}, + []string{types.ModuleName, "bridge_call_in_amount"}, coins, - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), + telemetry.NewLabel("module", k.moduleName), ) } diff --git a/x/crosschain/keeper/bridge_call_out.go b/x/crosschain/keeper/bridge_call_out.go index 16caa475d..256470c2f 100644 --- a/x/crosschain/keeper/bridge_call_out.go +++ b/x/crosschain/keeper/bridge_call_out.go @@ -62,25 +62,21 @@ func (k Keeper) AddOutgoingBridgeCallWithoutBuild(ctx sdk.Context, outCall *type )) if !ctx.IsCheckTx() { - tokenLabels := make([]metrics.Label, 0, len(outCall.Tokens)) for _, t := range outCall.Tokens { - fxtelemetry.SetGaugeLabelsWithToken( - []string{types.ModuleName, types.MetricsKeyBridgeCallOut}, + fxtelemetry.SetGaugeLabelsWithDenom( + []string{types.ModuleName, "bridge_call_out_amount"}, t.Contract, t.Amount.BigInt(), - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), + telemetry.NewLabel("module", k.moduleName), + ) + telemetry.IncrCounterWithLabels( + []string{types.ModuleName, "bridge_call_out"}, + float32(1), + []metrics.Label{ + telemetry.NewLabel("module", k.moduleName), + telemetry.NewLabel("contract", t.Contract), + }, ) - tokenLabels = append(tokenLabels, telemetry.NewLabel(fxtelemetry.LabelToken, t.Contract)) } - telemetry.IncrCounterWithLabels( - []string{types.ModuleName, types.MetricsKeyBridgeCallOut}, - float32(1), - append([]metrics.Label{ - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), - telemetry.NewLabel(fxtelemetry.LabelSender, outCall.Sender), - telemetry.NewLabel(types.MetricsLabelRefund, outCall.Refund), - telemetry.NewLabel(fxtelemetry.LabelTo, outCall.To), - }, tokenLabels...), - ) } return outCall.Nonce diff --git a/x/crosschain/keeper/msg_server.go b/x/crosschain/keeper/msg_server.go index 97fee2da5..d51c92351 100644 --- a/x/crosschain/keeper/msg_server.go +++ b/x/crosschain/keeper/msg_server.go @@ -165,11 +165,11 @@ func (s MsgServer) AddDelegate(c context.Context, msg *types.MsgAddDelegate) (*t oracle.StartHeight = ctx.BlockHeight() if !ctx.IsCheckTx() { telemetry.SetGaugeWithLabels( - []string{types.ModuleName, types.MetricsKeyOracleStatus}, + []string{types.ModuleName, "oracle_status"}, float32(0), []metrics.Label{ - telemetry.NewLabel(types.MetricsLabelModule, s.moduleName), - telemetry.NewLabel(types.MetricsLabelAddress, oracle.OracleAddress), + telemetry.NewLabel("module", s.moduleName), + telemetry.NewLabel("address", oracle.OracleAddress), }, ) } diff --git a/x/crosschain/keeper/oracle.go b/x/crosschain/keeper/oracle.go index 38fae1ce2..4a8358c8b 100644 --- a/x/crosschain/keeper/oracle.go +++ b/x/crosschain/keeper/oracle.go @@ -201,11 +201,11 @@ func (k Keeper) SlashOracle(ctx sdk.Context, oracleAddrStr string) { } if !ctx.IsCheckTx() { telemetry.SetGaugeWithLabels( - []string{types.ModuleName, types.MetricsKeyOracleStatus}, + []string{types.ModuleName, "oracle_status"}, float32(1), []metrics.Label{ - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), - telemetry.NewLabel(types.MetricsLabelAddress, oracle.OracleAddress), + telemetry.NewLabel("module", k.moduleName), + telemetry.NewLabel("address", oracle.OracleAddress), }, ) } diff --git a/x/crosschain/keeper/outgoing_pool.go b/x/crosschain/keeper/outgoing_pool.go index 8637ab481..12a4ac758 100644 --- a/x/crosschain/keeper/outgoing_pool.go +++ b/x/crosschain/keeper/outgoing_pool.go @@ -61,20 +61,17 @@ func (k Keeper) addToOutgoingPool(ctx sdk.Context, sender sdk.AccAddress, receiv )) if !ctx.IsCheckTx() { - fxtelemetry.SetGaugeLabelsWithCoin( - []string{types.ModuleName, types.MetricsKeyOutgoingTx}, - amount, - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), + fxtelemetry.SetGaugeLabelsWithDenom( + []string{types.ModuleName, "outgoing_tx_amount"}, + amount.Denom, amount.Amount.BigInt(), + telemetry.NewLabel("module", k.moduleName), ) - - metrics.IncrCounterWithLabels( - []string{types.ModuleName, types.MetricsKeyOutgoingTx}, + telemetry.IncrCounterWithLabels( + []string{types.ModuleName, "outgoing_tx"}, 1, []metrics.Label{ - telemetry.NewLabel(types.MetricsLabelModule, k.moduleName), - telemetry.NewLabel(fxtelemetry.LabelSender, sender.String()), - telemetry.NewLabel(fxtelemetry.LabelReceiver, receiver), - telemetry.NewLabel(fxtelemetry.LabelToken, amount.Denom), + telemetry.NewLabel("module", k.moduleName), + telemetry.NewLabel("denom", amount.Denom), }, ) } diff --git a/x/crosschain/keeper/send_to_fx.go b/x/crosschain/keeper/send_to_fx.go index b5e0e11c9..280ff0729 100644 --- a/x/crosschain/keeper/send_to_fx.go +++ b/x/crosschain/keeper/send_to_fx.go @@ -8,6 +8,8 @@ import ( errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" + "github.com/armon/go-metrics" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" @@ -15,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/functionx/fx-core/v7/contract" + fxtelemetry "github.com/functionx/fx-core/v7/telemetry" fxtypes "github.com/functionx/fx-core/v7/types" "github.com/functionx/fx-core/v7/x/crosschain/types" ) @@ -26,6 +29,22 @@ func (k Keeper) SendToFxExecuted(ctx sdk.Context, claim *types.MsgSendToFxClaim) } coin := sdk.NewCoin(bridgeToken.Denom, claim.Amount) + if !ctx.IsCheckTx() { + defer func() { + telemetry.IncrCounterWithLabels( + []string{types.ModuleName, "send_to_fx"}, + float32(1), + []metrics.Label{ + telemetry.NewLabel("module", k.moduleName), + }, + ) + fxtelemetry.SetGaugeLabelsWithDenom( + []string{types.ModuleName, "send_to_fx_amount"}, + coin.Denom, coin.Amount.BigInt(), + telemetry.NewLabel("module", k.moduleName), + ) + }() + } receiveAddr, err := sdk.AccAddressFromBech32(claim.Receiver) if err != nil { return errorsmod.Wrap(types.ErrInvalid, "receiver address") diff --git a/x/crosschain/legacy/precompile_events.go b/x/crosschain/legacy/precompile_events.go index 60f398561..c1744eed6 100644 --- a/x/crosschain/legacy/precompile_events.go +++ b/x/crosschain/legacy/precompile_events.go @@ -3,8 +3,6 @@ package legacy import ( "math/big" - "github.com/armon/go-metrics" - "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" ) @@ -22,16 +20,6 @@ func Fip20CrossChainEvents(ctx sdk.Context, from, token common.Address, recipien sdk.NewAttribute(AttributeKeyTokenAddress, token.String()), sdk.NewAttribute(AttributeKeyDenom, denom), )) - - telemetry.IncrCounterWithLabels( - []string{"relay_transfer_cross_chain"}, - 1, - []metrics.Label{ - telemetry.NewLabel("erc20", token.String()), - telemetry.NewLabel("denom", denom), - telemetry.NewLabel("target", target), - }, - ) } // CrossChainEvents diff --git a/x/crosschain/types/metrics.go b/x/crosschain/types/metrics.go deleted file mode 100644 index d0c0d09c4..000000000 --- a/x/crosschain/types/metrics.go +++ /dev/null @@ -1,15 +0,0 @@ -package types - -const ( - MetricsKeyBridgeCallIn = "bridge_call_in" - MetricsKeyBridgeCallOut = "bridge_call_out" - MetricsKeyOutgoingTx = "outgoing_tx" - MetricsKeyOracleStatus = "oracle_status" -) - -const ( - MetricsLabelModule = "module" - MetricsLabelRefund = "refund" - MetricsLabelTxOrigin = "tx_origin" - MetricsLabelAddress = "address" -) diff --git a/x/migrate/keeper/msg_server.go b/x/migrate/keeper/msg_server.go index 7c5d7ccff..83178a958 100644 --- a/x/migrate/keeper/msg_server.go +++ b/x/migrate/keeper/msg_server.go @@ -4,7 +4,6 @@ import ( "context" errorsmod "cosmossdk.io/errors" - "github.com/armon/go-metrics" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" @@ -55,13 +54,8 @@ func (k Keeper) MigrateAccount(goCtx context.Context, msg *types.MsgMigrateAccou k.SetMigrateRecord(ctx, fromAddress, toAddress) defer func() { - telemetry.IncrCounterWithLabels( - []string{types.ModuleName, msg.Type()}, - 1, - []metrics.Label{ - telemetry.NewLabel("from", msg.From), - telemetry.NewLabel("to", msg.To), - }, + telemetry.IncrCounter(1, + types.ModuleName, msg.Type(), ) }()