Skip to content

Commit

Permalink
More telemetry improvements for liquidation and deleveraging (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy04 committed Oct 19, 2023
1 parent e48f473 commit 79b327c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 52 deletions.
8 changes: 4 additions & 4 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,21 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
labels := []gometrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
}
telemetry.SetGaugeWithLabels(
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NumSubaccountsIterated, metrics.Count,
},
float32(numSubaccountsIterated),
labels,
)
telemetry.SetGaugeWithLabels(
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NonOverlappingBankruptcyPrices, metrics.Count,
},
float32(numSubaccountsWithNonOverlappingBankruptcyPrices),
labels,
)
telemetry.SetGaugeWithLabels(
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NoOpenPositionOnOppositeSide, metrics.Count,
},
Expand Down Expand Up @@ -428,7 +428,7 @@ func (k Keeper) ProcessDeleveraging(
metrics.GetLabelForBoolValue(metrics.CheckTx, ctx.IsCheckTx()),
metrics.GetLabelForBoolValue(metrics.IsLong, deltaQuantums.Sign() == -1),
}
telemetry.IncrCounterWithLabels(
gometrics.AddSampleWithLabels(
[]string{types.ModuleName, metrics.DeleverageSubaccount, metrics.Filled, metrics.QuoteQuantums},
metrics.GetMetricValueFromBigInt(deleveragedQuoteQuantums),
labels,
Expand Down
11 changes: 6 additions & 5 deletions protocol/x/clob/keeper/liquidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func (k Keeper) LiquidateSubaccountsAgainstOrderbook(
) error {
lib.AssertCheckTxMode(ctx)

telemetry.ModuleSetGauge(
types.ModuleName,
gometrics.AddSample(
[]string{
metrics.Liquidations,
metrics.LiquidatableSubaccountIds,
metrics.Count,
},
float32(len(subaccountIds)),
metrics.Liquidations,
metrics.LiquidatableSubaccountIds,
metrics.Count,
)

// Early return if there are 0 subaccounts to liquidate.
Expand Down
32 changes: 0 additions & 32 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

errorsmod "cosmossdk.io/errors"

gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
Expand Down Expand Up @@ -551,22 +550,6 @@ func (k Keeper) PersistMatchLiquidationToState(
return err
}

notionalQuoteQuantums, err := k.perpetualsKeeper.GetNetNotional(
ctx,
matchLiquidation.PerpetualId,
new(big.Int).SetUint64(matchLiquidation.TotalSize),
)
if err != nil {
return err
}
absNotionalQuoteQuantums := new(big.Int).Abs(notionalQuoteQuantums)

telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.LiquidationOrderNotionalQuoteQuantums, metrics.DeliverTx},
metrics.GetMetricValueFromBigInt(absNotionalQuoteQuantums),
matchLiquidation.GetMetricLabels(),
)

for _, fill := range matchLiquidation.GetFills() {
// Fetch the maker order from either short term orders or state.
makerOrder, err := k.FetchOrderFromOrderId(ctx, fill.MakerOrderId, ordersMap)
Expand Down Expand Up @@ -696,21 +679,6 @@ func (k Keeper) PersistMatchDeleveragingToState(
err,
)
}

if quoteQuantums, err := k.perpetualsKeeper.GetNetNotional(
ctx,
perpetualId,
new(big.Int).SetUint64(fill.FillAmount),
); err == nil {
labels := []gometrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
}
telemetry.IncrCounterWithLabels(
[]string{metrics.ProcessOperations, metrics.Deleveraging, metrics.QuoteQuantums},
metrics.GetMetricValueFromBigInt(quoteQuantums),
labels,
)
}
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions protocol/x/clob/keeper/process_single_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
errorsmod "cosmossdk.io/errors"

gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/indexer/off_chain_updates"
"github.com/dydxprotocol/v4-chain/protocol/lib"
Expand Down Expand Up @@ -258,13 +257,13 @@ func (k Keeper) ProcessSingleMatch(
}

// Stat quote quantums liquidated.
telemetry.IncrCounterWithLabels(
gometrics.AddSampleWithLabels(
[]string{metrics.Liquidations, metrics.PlacePerpetualLiquidation, metrics.Filled, metrics.QuoteQuantums},
metrics.GetMetricValueFromBigInt(notionalLiquidatedQuoteQuantums),
labels,
)
// Stat insurance fund delta.
telemetry.IncrCounterWithLabels(
gometrics.AddSampleWithLabels(
[]string{metrics.Liquidations, metrics.InsuranceFundDelta},
metrics.GetMetricValueFromBigInt(new(big.Int).Abs(takerInsuranceFundDelta)),
append(labels, metrics.GetLabelForBoolValue(metrics.Positive, takerInsuranceFundDelta.Sign() == 1)),
Expand Down
16 changes: 8 additions & 8 deletions protocol/x/clob/types/stat_internal_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
Expand Down Expand Up @@ -160,16 +159,17 @@ func (stats *OperationsStats) EmitStats(abciCallback string) {
},
}

labels := []gometrics.Label{
metrics.GetLabelForStringValue(
metrics.Callback,
abciCallback,
),
}
for _, stat := range statsList {
telemetry.SetGaugeWithLabels(
gometrics.AddSampleWithLabels(
stat.keys,
stat.value,
[]gometrics.Label{
metrics.GetLabelForStringValue(
metrics.Callback,
abciCallback,
),
},
labels,
)
}
}
Expand Down

0 comments on commit 79b327c

Please sign in to comment.