From f50431e3a43a804fc9d4337d779bc054deffd7d9 Mon Sep 17 00:00:00 2001 From: Jakob Herlitz <125316911+jakob-dydx@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:06:56 -0700 Subject: [PATCH] Log Success/Error count for clob msg handlers (#529) * create helper function to stat success and error metrics for clob msg handlers * format * take in labels in helper and pass from PlaceOrder and CancelOrder * import metrics --- protocol/lib/metrics/constants.go | 6 +++++ protocol/lib/metrics/util.go | 26 +++++++++++++++++++ .../x/clob/keeper/msg_server_cancel_orders.go | 13 +++++----- .../keeper/msg_server_create_clob_pair.go | 2 ++ .../x/clob/keeper/msg_server_place_order.go | 14 +++++----- .../keeper/msg_server_proposed_operations.go | 2 ++ ...g_server_update_block_rate_limit_config.go | 2 ++ .../keeper/msg_server_update_clob_pair.go | 2 ++ ..._server_update_equity_tier_limit_config.go | 7 +++++ .../msg_server_update_liquidations_config.go | 2 ++ 10 files changed, 63 insertions(+), 13 deletions(-) diff --git a/protocol/lib/metrics/constants.go b/protocol/lib/metrics/constants.go index 4f924b8cf9..cd59441af5 100644 --- a/protocol/lib/metrics/constants.go +++ b/protocol/lib/metrics/constants.go @@ -103,6 +103,7 @@ const ( ConditionalOrderTriggered = "conditional_order_triggered" ConditionalOrderUntriggered = "conditional_order_untriggered" ConvertToUpdates = "convert_to_updates" + CreateClobPair = "create_clob_pair" Expired = "expired" GetFillQuoteQuantums = "get_fill_quote_quantums" Hydrate = "hydrate" @@ -136,6 +137,7 @@ const ( PlaceStatefulOrder = "place_stateful_order" ProcessMatches = "process_matches" ProcessOperations = "process_operations" + ProposedOperations = "proposed_operations" Proposer = "proposer" RateLimit = "rate_limit" ReduceOnly = "reduce_only" @@ -164,6 +166,10 @@ const ( UnfilledLiquidationOrders = "unfilled_liquidation_orders" UnknownPlaceOrders = "unknown_place_orders" UnverifiedStatefulOrderRemoval = "unverified_stateful_order_removal" + UpdateBlockRateLimitConfiguration = "update_block_rate_limit_configuration" + UpdateClobPair = "update_clob_pair" + UpdateEquityTierLimitConfiguration = "update_equity_tier_limit_configuration" + UpdateLiquidationsConfig = "update_liquidations_config" ValidateMatches = "validate_matches" ValidateOrder = "validate_order" diff --git a/protocol/lib/metrics/util.go b/protocol/lib/metrics/util.go index 4824573ee7..27f0ebced4 100644 --- a/protocol/lib/metrics/util.go +++ b/protocol/lib/metrics/util.go @@ -20,6 +20,32 @@ func IncrCountMetricWithLabels(module string, metric string, labels ...gometrics ) } +// IncrSuccessOrErrorCounter increments either the success or error counter for a given handler +// based on whether the given error is nil or not. This function is intended to be called in a +// defer block at the top of any function which returns an error. +func IncrSuccessOrErrorCounter(err error, module string, handler string, callback string, labels ...gometrics.Label) { + successOrError := Success + if err != nil { + successOrError = Error + } + + telemetry.IncrCounterWithLabels( + []string{ + module, + handler, + successOrError, + Count, + }, + 1, + append( + []gometrics.Label{ + GetLabelForStringValue(Callback, callback), + }, + labels..., + ), + ) +} + // NewBinaryStringLabel returns a metrics label with a value of "yes" or "no" depending on the condition. func NewBinaryStringLabel(metricName string, condition bool) gometrics.Label { labelValue := No diff --git a/protocol/x/clob/keeper/msg_server_cancel_orders.go b/protocol/x/clob/keeper/msg_server_cancel_orders.go index 760acd7b8a..a7bb9115ba 100644 --- a/protocol/x/clob/keeper/msg_server_cancel_orders.go +++ b/protocol/x/clob/keeper/msg_server_cancel_orders.go @@ -25,6 +25,13 @@ func (k msgServer) CancelOrder( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter( + err, + types.ModuleName, + metrics.CancelOrder, + metrics.DeliverTx, + msg.OrderId.GetOrderIdLabels()..., + ) if err != nil { // Gracefully handle the case where the order was already removed from state. This can happen if an Order // Removal Operation was included in the same block as the MsgCancelOrder. By the time we try to cancel @@ -92,11 +99,5 @@ func (k msgServer) CancelOrder( ), ) - telemetry.IncrCounterWithLabels( - []string{types.ModuleName, metrics.StatefulCancellationMsgHandlerSuccess, metrics.Count}, - 1, - msg.OrderId.GetOrderIdLabels(), - ) - return &types.MsgCancelOrderResponse{}, nil } diff --git a/protocol/x/clob/keeper/msg_server_create_clob_pair.go b/protocol/x/clob/keeper/msg_server_create_clob_pair.go index 45cc222982..12e61338e8 100644 --- a/protocol/x/clob/keeper/msg_server_create_clob_pair.go +++ b/protocol/x/clob/keeper/msg_server_create_clob_pair.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" ) @@ -20,6 +21,7 @@ func (k msgServer) CreateClobPair( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter(err, types.ModuleName, metrics.CreateClobPair, metrics.DeliverTx) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "CreateClobPair", msg) } diff --git a/protocol/x/clob/keeper/msg_server_place_order.go b/protocol/x/clob/keeper/msg_server_place_order.go index b544fa7c9b..479f01f5f3 100644 --- a/protocol/x/clob/keeper/msg_server_place_order.go +++ b/protocol/x/clob/keeper/msg_server_place_order.go @@ -5,7 +5,6 @@ import ( errorsmod "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events" "github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager" @@ -25,6 +24,13 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) ( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter( + err, + types.ModuleName, + metrics.PlaceOrder, + metrics.DeliverTx, + msg.Order.GetOrderLabels()..., + ) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "PlaceOrder", msg) } @@ -99,11 +105,5 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) ( processProposerMatchesEvents, ) - telemetry.IncrCounterWithLabels( - []string{types.ModuleName, metrics.StatefulOrderMsgHandlerSuccess, metrics.Count}, - 1, - order.GetOrderLabels(), - ) - return &types.MsgPlaceOrderResponse{}, nil } diff --git a/protocol/x/clob/keeper/msg_server_proposed_operations.go b/protocol/x/clob/keeper/msg_server_proposed_operations.go index f96e8ec3f9..fc077039fa 100644 --- a/protocol/x/clob/keeper/msg_server_proposed_operations.go +++ b/protocol/x/clob/keeper/msg_server_proposed_operations.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) @@ -15,6 +16,7 @@ func (k msgServer) ProposedOperations( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter(err, types.ModuleName, metrics.ProposedOperations, metrics.DeliverTx) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "ProposedOperations", msg) } diff --git a/protocol/x/clob/keeper/msg_server_update_block_rate_limit_config.go b/protocol/x/clob/keeper/msg_server_update_block_rate_limit_config.go index 2d8f98acef..bdf10ee340 100644 --- a/protocol/x/clob/keeper/msg_server_update_block_rate_limit_config.go +++ b/protocol/x/clob/keeper/msg_server_update_block_rate_limit_config.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) @@ -19,6 +20,7 @@ func (k msgServer) UpdateBlockRateLimitConfiguration( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter(err, types.ModuleName, metrics.UpdateBlockRateLimitConfiguration, metrics.DeliverTx) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "UpdateBlockRateLimitConfiguration", msg) } diff --git a/protocol/x/clob/keeper/msg_server_update_clob_pair.go b/protocol/x/clob/keeper/msg_server_update_clob_pair.go index bc407336b1..2d03f5f0d8 100644 --- a/protocol/x/clob/keeper/msg_server_update_clob_pair.go +++ b/protocol/x/clob/keeper/msg_server_update_clob_pair.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) @@ -18,6 +19,7 @@ func (k msgServer) UpdateClobPair( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter(err, types.ModuleName, metrics.UpdateClobPair, metrics.DeliverTx) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "UpdateClobPair", msg) } diff --git a/protocol/x/clob/keeper/msg_server_update_equity_tier_limit_config.go b/protocol/x/clob/keeper/msg_server_update_equity_tier_limit_config.go index c26cac172f..86cf1f1c6e 100644 --- a/protocol/x/clob/keeper/msg_server_update_equity_tier_limit_config.go +++ b/protocol/x/clob/keeper/msg_server_update_equity_tier_limit_config.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) @@ -19,6 +20,12 @@ func (k msgServer) UpdateEquityTierLimitConfiguration( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter( + err, + types.ModuleName, + metrics.UpdateEquityTierLimitConfiguration, + metrics.DeliverTx, + ) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "UpdateEquityTierLimitConfiguration", msg) } diff --git a/protocol/x/clob/keeper/msg_server_update_liquidations_config.go b/protocol/x/clob/keeper/msg_server_update_liquidations_config.go index 8b7a683fc6..2345d4f3fa 100644 --- a/protocol/x/clob/keeper/msg_server_update_liquidations_config.go +++ b/protocol/x/clob/keeper/msg_server_update_liquidations_config.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" errorlib "github.com/dydxprotocol/v4-chain/protocol/lib/error" + "github.com/dydxprotocol/v4-chain/protocol/lib/metrics" "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" ) @@ -19,6 +20,7 @@ func (k msgServer) UpdateLiquidationsConfig( ctx := sdk.UnwrapSDKContext(goCtx) defer func() { + metrics.IncrSuccessOrErrorCounter(err, types.ModuleName, metrics.UpdateLiquidationsConfig, metrics.DeliverTx) if err != nil { errorlib.LogDeliverTxError(k.Keeper.Logger(ctx), err, ctx.BlockHeight(), "UpdateLiquidationsConfig", msg) }