Skip to content

Commit

Permalink
Revert "Revert "[CLOB-1014] [CLOB-1004] Unify metrics library, un-mod… (
Browse files Browse the repository at this point in the history
#994)

* Revert "Revert "[CLOB-1014] [CLOB-1004] Unify metrics library, un-modularize … (#967)"

This reverts commit 86e50c4.

* remove jonfung/ build and push
  • Loading branch information
jonfung-dydx authored Jan 19, 2024
1 parent 55c6d00 commit 4dcc07c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 123 deletions.
1 change: 0 additions & 1 deletion .github/workflows/protocol-build-and-push-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Protocol Build & Push Image to AWS ECR
on: # yamllint disable-line rule:truthy
push:
branches:
- jonfung/**
- main
- 'release/[a-z]+/v0.[0-9]+.x' # e.g. release/protocol/v0.1.x
- 'release/[a-z]+/v[0-9]+.x' # e.g. release/protocol/v1.x
Expand Down
21 changes: 21 additions & 0 deletions protocol/lib/metrics/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,24 @@ func ModuleMeasureSince(module string, key string, start time.Time) {
key,
)
}

// ModuleMeasureSinceWithLabels provides a short hand method for emitting a time measure
// metric for a module with labels. Global labels are not included in this metric.
// Please try to use `AddSampleWithLabels` instead.
// TODO(CLOB-1022) Roll our own calculations for timing on top of AddSample instead
// of using MeasureSince.
func ModuleMeasureSinceWithLabels(
module string,
keys []string,
start time.Time,
labels []gometrics.Label,
) {
gometrics.MeasureSinceWithLabels(
keys,
start.UTC(),
append(
[]gometrics.Label{telemetry.NewLabel(telemetry.MetricLabelNameModule, module)},
labels...,
),
)
}
20 changes: 0 additions & 20 deletions protocol/lib/metrics/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package metrics
import (
"math/big"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -68,25 +67,6 @@ func GetMetricValueFromBigInt(i *big.Int) float32 {
return r
}

// ModuleMeasureSinceWithLabels provides a short hand method for emitting a time measure
// metric for a module with a given set of keys and labels.
// NOTE: global labels are not included in this metric.
func ModuleMeasureSinceWithLabels(
module string,
keys []string,
start time.Time,
labels []gometrics.Label,
) {
gometrics.MeasureSinceWithLabels(
keys,
start.UTC(),
append(
[]gometrics.Label{telemetry.NewLabel(telemetry.MetricLabelNameModule, module)},
labels...,
),
)
}

// GetCallbackMetricFromCtx determines the callback metric based on the context. Note that DeliverTx is implied
// if the context is not CheckTx or ReCheckTx. This function is unable to account for other callbacks like
// PrepareCheckState or EndBlocker.
Expand Down
12 changes: 5 additions & 7 deletions protocol/x/clob/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
gometrics "github.com/hashicorp/go-metrics"
)

// BeginBlocker executes all ABCI BeginBlock logic respective to the clob module.
Expand Down Expand Up @@ -73,10 +72,10 @@ func EndBlocker(
),
),
)
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.Expired, metrics.StatefulOrderRemoved, metrics.Count},
metrics.IncrCounterWithLabels(
metrics.ClobExpiredStatefulOrders,
1,
orderId.GetOrderIdLabels(),
orderId.GetOrderIdLabels()...,
)
}

Expand Down Expand Up @@ -118,10 +117,9 @@ func EndBlocker(
keeper.PruneRateLimits(ctx)

// Emit relevant metrics at the end of every block.
telemetry.SetGaugeWithLabels(
[]string{metrics.InsuranceFundBalance},
metrics.SetGauge(
metrics.InsuranceFundBalance,
metrics.GetMetricValueFromBigInt(keeper.GetInsuranceFundBalance(ctx)),
[]gometrics.Label{},
)
}

Expand Down
82 changes: 40 additions & 42 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import (
"math/big"
"time"

indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"

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"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
gometrics "github.com/hashicorp/go-metrics"
)

// MaybeDeleverageSubaccount is the main entry point to deleverage a subaccount. It attempts to find positions
Expand Down Expand Up @@ -53,11 +51,9 @@ func (k Keeper) MaybeDeleverageSubaccount(
// Early return to skip deleveraging if the subaccount doesn't have negative equity or a position in a final
// settlement market.
if !shouldDeleverageAtBankruptcyPrice && !shouldDeleverageAtOraclePrice {
telemetry.IncrCounter(
metrics.IncrCounter(
metrics.ClobPrepareCheckStateCannotDeleverageSubaccount,
1,
types.ModuleName,
metrics.PrepareCheckState,
metrics.CannotDeleverageSubaccount,
)
return new(big.Int), nil
}
Expand All @@ -81,7 +77,7 @@ func (k Keeper) MaybeDeleverageSubaccount(
shouldDeleverageAtOraclePrice,
)

labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
metrics.GetLabelForBoolValue(metrics.IsLong, deltaQuantums.Sign() == -1),
}
Expand All @@ -93,22 +89,27 @@ func (k Keeper) MaybeDeleverageSubaccount(
labels = append(labels, metrics.GetLabelForStringValue(metrics.Status, metrics.PartiallyFilled))
}
// Record the status of the deleveraging operation.
telemetry.IncrCounterWithLabels([]string{types.ModuleName, metrics.DeleverageSubaccount}, 1, labels)
metrics.IncrCounterWithLabels(
metrics.ClobDeleverageSubaccount,
1,
labels...,
)

if quoteQuantums, err := k.perpetualsKeeper.GetNetNotional(
ctx,
perpetualId,
new(big.Int).Abs(deltaQuantums),
); err == nil {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, metrics.DeleverageSubaccount, metrics.TotalQuoteQuantums},
metrics.IncrCounterWithLabels(
metrics.ClobDeleverageSubaccountTotalQuoteQuantums,
metrics.GetMetricValueFromBigInt(quoteQuantums),
labels,
labels...,
)
gometrics.AddSampleWithLabels(
[]string{types.ModuleName, metrics.DeleverageSubaccount, metrics.TotalQuoteQuantums, metrics.Distribution},

metrics.AddSampleWithLabels(
metrics.ClobDeleverageSubaccountTotalQuoteQuantumsDistribution,
metrics.GetMetricValueFromBigInt(quoteQuantums),
labels,
labels...,
)
}

Expand All @@ -117,10 +118,11 @@ func (k Keeper) MaybeDeleverageSubaccount(
new(big.Float).SetInt(new(big.Int).Abs(quantumsDeleveraged)),
new(big.Float).SetInt(new(big.Int).Abs(deltaQuantums)),
).Float32()
gometrics.AddSampleWithLabels(
[]string{metrics.Deleveraging, metrics.PercentFilled, metrics.Distribution},

metrics.AddSampleWithLabels(
metrics.DeleveragingPercentFilledDistribution,
percentFilled,
labels,
labels...,
)

return quantumsDeleveraged, err
Expand Down Expand Up @@ -288,11 +290,10 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
fills []types.MatchPerpetualDeleveraging_Fill,
deltaQuantumsRemaining *big.Int,
) {
defer telemetry.ModuleMeasureSince(
defer metrics.ModuleMeasureSince(
types.ModuleName,
metrics.ClobOffsettingSubaccountPerpetualPosition,
time.Now(),
types.ModuleName,
metrics.OffsettingSubaccountPerpetualPosition,
)

numSubaccountsIterated := uint32(0)
Expand Down Expand Up @@ -428,29 +429,25 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
}
}

labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
}
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NumSubaccountsIterated, metrics.Count,
},

metrics.AddSampleWithLabels(
metrics.ClobDeleveragingNumSubaccountsIteratedCount,
float32(numSubaccountsIterated),
labels,
labels...,
)
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NonOverlappingBankruptcyPrices, metrics.Count,
},

metrics.AddSampleWithLabels(
metrics.ClobDeleveragingNonOverlappingBankrupcyPricesCount,
float32(numSubaccountsWithNonOverlappingBankruptcyPrices),
labels,
labels...,
)
gometrics.AddSampleWithLabels(
[]string{
types.ModuleName, metrics.Deleveraging, metrics.NoOpenPositionOnOppositeSide, metrics.Count,
},
metrics.AddSampleWithLabels(
metrics.ClobDeleveragingNoOpenPositionOnOppositeSideCount,
float32(numSubaccountsWithNoOpenPositionOnOppositeSide),
labels,
labels...,
)
return fills, deltaQuantumsRemaining
}
Expand Down Expand Up @@ -584,15 +581,16 @@ func (k Keeper) ProcessDeleveraging(
perpetualId,
new(big.Int).Abs(deltaBaseQuantums),
); err == nil {
labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
metrics.GetLabelForBoolValue(metrics.CheckTx, ctx.IsCheckTx()),
metrics.GetLabelForBoolValue(metrics.IsLong, deltaBaseQuantums.Sign() == -1),
}
gometrics.AddSampleWithLabels(
[]string{types.ModuleName, metrics.DeleverageSubaccount, metrics.Filled, metrics.QuoteQuantums},

metrics.AddSampleWithLabels(
metrics.ClobDeleverageSubaccountFilledQuoteQuantums,
metrics.GetMetricValueFromBigInt(deleveragedQuoteQuantums),
labels,
labels...,
)
}

Expand Down
54 changes: 25 additions & 29 deletions protocol/x/clob/keeper/liquidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"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"
gometrics "github.com/hashicorp/go-metrics"
)

// subaccountToDeleverage is a struct containing a subaccount ID and perpetual ID to deleverage.
Expand All @@ -42,12 +41,8 @@ func (k Keeper) LiquidateSubaccountsAgainstOrderbook(
) {
lib.AssertCheckTxMode(ctx)

gometrics.AddSample(
[]string{
metrics.Liquidations,
metrics.LiquidatableSubaccountIds,
metrics.Count,
},
metrics.AddSample(
metrics.LiquidationsLiquidatableSubaccountIdsCount,
float32(len(subaccountIds)),
)

Expand Down Expand Up @@ -282,7 +277,7 @@ func (k Keeper) PlacePerpetualLiquidation(
perpetualId,
)

labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
}
if liquidationOrder.IsBuy() {
Expand All @@ -296,10 +291,11 @@ func (k Keeper) PlacePerpetualLiquidation(
new(big.Float).SetUint64(orderSizeOptimisticallyFilledFromMatchingQuantums.ToUint64()),
new(big.Float).SetUint64(liquidationOrder.GetBaseQuantums().ToUint64()),
).Float32()
gometrics.AddSampleWithLabels(
[]string{metrics.Liquidations, metrics.PercentFilled, metrics.Distribution},

metrics.AddSampleWithLabels(
metrics.LiquidationsPercentFilledDistribution,
percentFilled,
labels,
labels...,
)

if orderSizeOptimisticallyFilledFromMatchingQuantums == 0 {
Expand All @@ -322,15 +318,16 @@ func (k Keeper) PlacePerpetualLiquidation(
perpetualId,
liquidationOrder.GetBaseQuantums().ToBigInt(),
); err == nil {
telemetry.IncrCounterWithLabels(
[]string{metrics.Liquidations, metrics.PlacePerpetualLiquidation, metrics.QuoteQuantums},
metrics.IncrCounterWithLabels(
metrics.LiquidationsPlacePerpetualLiquidationQuoteQuantums,
metrics.GetMetricValueFromBigInt(totalQuoteQuantums),
labels,
labels...,
)
gometrics.AddSampleWithLabels(
[]string{metrics.Liquidations, metrics.PlacePerpetualLiquidation, metrics.QuoteQuantums, metrics.Distribution},

metrics.AddSampleWithLabels(
metrics.LiquidationsPlacePerpetualLiquidationQuoteQuantumsDistribution,
metrics.GetMetricValueFromBigInt(totalQuoteQuantums),
labels,
labels...,
)
}

Expand Down Expand Up @@ -590,19 +587,18 @@ func (k Keeper) GetFillablePrice(
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
callback = metrics.DeliverTx
}
telemetry.IncrCounterWithLabels(
[]string{metrics.Liquidations, metrics.LiquidationMatchNegativeTNC},

metrics.IncrCounterWithLabels(
metrics.LiquidationsLiquidationMatchNegativeTNC,
1,
[]gometrics.Label{
metrics.GetLabelForIntValue(
metrics.PerpetualId,
int(perpetualId),
),
metrics.GetLabelForStringValue(
metrics.Callback,
callback,
),
},
metrics.GetLabelForIntValue(
metrics.PerpetualId,
int(perpetualId),
),
metrics.GetLabelForStringValue(
metrics.Callback,
callback,
),
)

ctx.Logger().Info(
Expand Down
Loading

0 comments on commit 4dcc07c

Please sign in to comment.