Skip to content

Commit

Permalink
Log Success/Error count for clob msg handlers (#529)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jakob-dydx authored Oct 10, 2023
1 parent c220484 commit f50431e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 13 deletions.
6 changes: 6 additions & 0 deletions protocol/lib/metrics/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down
26 changes: 26 additions & 0 deletions protocol/lib/metrics/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions protocol/x/clob/keeper/msg_server_cancel_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions protocol/x/clob/keeper/msg_server_create_clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions protocol/x/clob/keeper/msg_server_place_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions protocol/x/clob/keeper/msg_server_proposed_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions protocol/x/clob/keeper/msg_server_update_clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down

0 comments on commit f50431e

Please sign in to comment.