Skip to content

Commit

Permalink
feat: bridgecall metrics (#600)
Browse files Browse the repository at this point in the history
Co-authored-by: nulnut <151493716+nulnut@users.noreply.github.com>
  • Loading branch information
zakir-code and nulnut committed Jul 25, 2024
1 parent 2ad7522 commit 69126e6
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
9 changes: 9 additions & 0 deletions telemetry/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package telemetry

const (
LabelSender = "sender"
LabelReceiver = "receiver"
LabelTo = "to"
LabelSuccess = "success"
LabelToken = "token"
)
32 changes: 32 additions & 0 deletions telemetry/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package telemetry

import (
"fmt"
"math"
"math/big"

"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var maxFloat32, _ = big.NewInt(0).SetString(fmt.Sprintf("%.0f", math.MaxFloat32), 10)

func SetGaugeLabelsWithToken(keys []string, token 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...)
}

func SetGaugeLabelsWithCoins(keys []string, coins sdk.Coins, labels ...metrics.Label) {
for _, coin := range coins {
SetGaugeLabelsWithCoin(keys, coin, labels...)
}
}
28 changes: 28 additions & 0 deletions x/crosschain/keeper/bridge_call_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"bytes"
"fmt"
"math/big"
"strconv"

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"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"

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"
erc20types "github.com/functionx/fx-core/v7/x/erc20/types"
Expand All @@ -32,6 +36,21 @@ func (k Keeper) BridgeCallHandler(ctx sdk.Context, msg *types.MsgBridgeCallClaim
commit()
}

if !ctx.IsCheckTx() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, types.MetricsKeyBridgeCallIn},
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()),
},
)
}

if err != nil && len(tokens) > 0 {
// new outgoing bridge call to refund
outCallNonce, err := k.AddOutgoingBridgeCall(ctx, refundAddr, refundAddr, erc20Token, common.Address{}, nil, nil, msg.EventNonce)
Expand Down Expand Up @@ -71,6 +90,15 @@ func (k Keeper) BridgeCallTransferAndCallEvm(ctx sdk.Context, sender, refundAddr
if err != nil {
return err
}

if !ctx.IsCheckTx() {
fxtelemetry.SetGaugeLabelsWithCoins(
[]string{types.ModuleName, types.MetricsKeyBridgeCallIn},
coins,
telemetry.NewLabel(types.MetricsLabelModule, k.moduleName),
)
}

if err = k.bridgeCallTransferTokens(ctx, receiverTokenAddr.Bytes(), receiverTokenAddr.Bytes(), coins); err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions x/crosschain/keeper/bridge_call_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"strconv"

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"
gogotypes "github.com/gogo/protobuf/types"

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"
erc20types "github.com/functionx/fx-core/v7/x/erc20/types"
Expand Down Expand Up @@ -58,6 +61,28 @@ func (k Keeper) AddOutgoingBridgeCallWithoutBuild(ctx sdk.Context, outCall *type
sdk.NewAttribute(types.AttributeKeyBridgeCallNonce, fmt.Sprint(outCall.Nonce)),
))

if !ctx.IsCheckTx() {
tokenLabels := make([]metrics.Label, 0, len(outCall.Tokens))
for _, t := range outCall.Tokens {
fxtelemetry.SetGaugeLabelsWithToken(
[]string{types.ModuleName, types.MetricsKeyBridgeCallOut},
t.Contract, t.Amount.BigInt(),
telemetry.NewLabel(types.MetricsLabelModule, k.moduleName),
)
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
}

Expand Down
22 changes: 22 additions & 0 deletions x/crosschain/keeper/outgoing_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ 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"

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"
)
Expand Down Expand Up @@ -50,6 +53,25 @@ func (k Keeper) AddToOutgoingPool(ctx sdk.Context, sender sdk.AccAddress, receiv
sdk.NewAttribute(types.AttributeKeyOutgoingTxID, fmt.Sprint(nextTxID)),
))

if !ctx.IsCheckTx() {
fxtelemetry.SetGaugeLabelsWithCoin(
[]string{types.ModuleName, types.MetricsKeyOutgoingTx},
amount,
telemetry.NewLabel(types.MetricsLabelModule, k.moduleName),
)

metrics.IncrCounterWithLabels(
[]string{types.ModuleName, types.MetricsKeyOutgoingTx},
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),
},
)
}

return nextTxID, nil
}

Expand Down
13 changes: 13 additions & 0 deletions x/crosschain/types/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

const (
MetricsKeyBridgeCallIn = "bridge_call_in"
MetricsKeyBridgeCallOut = "bridge_call_out"
MetricsKeyOutgoingTx = "outgoing_tx"
)

const (
MetricsLabelModule = "module"
MetricsLabelRefund = "refund"
MetricsLabelTxOrigin = "tx_origin"
)

0 comments on commit 69126e6

Please sign in to comment.