Skip to content

[CLOB-1014] [CLOB-1004] Unify metrics library, un-modularize metrics keys #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion protocol/lib/metrics/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ const (

// Liquidation.
ConstructLiquidationOrder = "construct_liquidation_order"
InsuranceFundBalance = "insurance_fund_balance"
InsuranceFundDelta = "insurance_fund_delta"
Liquidations = "liquidations"
MaybeGetLiquidationOrder = "maybe_get_liquidation_order"
Expand Down
92 changes: 92 additions & 0 deletions protocol/lib/metrics/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package metrics

import (
"time"

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

// This file provides a main entrypoint for logging in the v4 protocol.
// TODO(CLOB-1013) Drop both metrics libraries above for a library
// that supports float64 (i.e hashicorp go-metrics)

type Label = gometrics.Label

// IncrCounterWithLabels provides a wrapper functionality for emitting a counter
// metric with global labels (if any) along with the provided labels.
func IncrCounterWithLabels(key string, val float32, labels ...Label) {
telemetry.IncrCounterWithLabels([]string{key}, val, labels)
}

// IncrCounter provides a wrapper functionality for emitting a counter
// metric with global labels (if any).
func IncrCounter(key string, val float32) {
telemetry.IncrCounterWithLabels([]string{key}, val, []gometrics.Label{})
}

// SetGaugeWithLabels provides a wrapper functionality for emitting a gauge
// metric with global labels (if any) along with the provided labels.
func SetGaugeWithLabels(key string, val float32, labels ...gometrics.Label) {
telemetry.SetGaugeWithLabels([]string{key}, val, labels)
}

// SetGauge provides a wrapper functionality for emitting a gauge
// metric with global labels (if any).
func SetGauge(key string, val float32) {
telemetry.SetGaugeWithLabels([]string{key}, val, []gometrics.Label{})
}

// AddSampleWithLabels provides a wrapper functionality for emitting a sample
// metric with the provided labels.
func AddSampleWithLabels(key string, val float32, labels ...gometrics.Label) {
gometrics.AddSampleWithLabels(
[]string{key},
val,
labels,
)
}

// AddSample provides a wrapper functionality for emitting a sample
// metric.
func AddSample(key string, val float32) {
gometrics.AddSampleWithLabels(
[]string{key},
val,
[]gometrics.Label{},
)
}

// ModuleMeasureSince provides a wrapper functionality for emitting a time measure
// metric with global labels (if any).
// Please try to use `AddSample` instead.
// TODO(CLOB-1022) Roll our own calculations for timing on top of AddSample instead
// of using MeasureSince.
func ModuleMeasureSince(module string, key string, start time.Time) {
telemetry.ModuleMeasureSince(
module,
start,
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a bit confusing to me, since AddSampleWithLabels is not time related. Are you saying that its preferred to pass the calculating timing into AddSampleWithLabels instead of using this?

Also, AddSampleWithLabels also uses gometrics, so wouldn't that have missing global labels too?

Copy link
Contributor Author

@jonfung-dydx jonfung-dydx Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm i'm saying it's preferred to manually roll your own timing calculation instead of using this function.

Thinking about it more I can modify this later to add our own MeasureSince method that uses our entrypoint method under the hood.

IMO We should not be relying on specific metric logging library functionality such as this. It should be easy to swap out metrics for our system under the hood.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add TODO to code 😄

module string,
keys []string,
start time.Time,
labels []gometrics.Label,
) {
gometrics.MeasureSinceWithLabels(
keys,
start.UTC(),
append(
[]gometrics.Label{telemetry.NewLabel(telemetry.MetricLabelNameModule, module)},
labels...,
),
)
}
42 changes: 42 additions & 0 deletions protocol/lib/metrics/metric_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// nolint:lll
package metrics

// Metrics Keys Guidelines
// 1. Be wary of length
// 2. Prefix by module
// 3. Suffix keys with a unit of measurement
// 4. Delimit with '_'
// 5. Information such as callback type should be added as tags, not in key names.
// Example: clob_place_order_count, clob_msg_place_order_latency_ms, clob_operations_queue_length
// clob_expired_stateful_orders_count, clob_processed_orders_ms_total

// Clob Metrics Keys
const (
// Stats
ClobExpiredStatefulOrders = "clob_expired_stateful_order_removed"
ClobPrepareCheckStateCannotDeleverageSubaccount = "clob_prepare_check_state_cannot_deleverage_subaccount"
ClobDeleverageSubaccountTotalQuoteQuantums = "clob_deleverage_subaccount_total_quote_quantums"
ClobDeleverageSubaccount = "clob_deleverage_subaccount"
LiquidationsPlacePerpetualLiquidationQuoteQuantums = "liquidations_place_perpetual_liquidation_quote_quantums"
LiquidationsLiquidationMatchNegativeTNC = "liquidations_liquidation_match_negative_tnc"
ClobMevErrorCount = "clob_mev_error_count"

// Gauges
InsuranceFundBalance = "insurance_fund_balance"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR summary mentions the removal of outdated metrics code and dependencies, including the global variable "InsuranceFundBalance". However, this constant is still present in the code. Please verify if this is intentional or an oversight.

- InsuranceFundBalance = "insurance_fund_balance"

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
InsuranceFundBalance = "insurance_fund_balance"

ClobMev = "clob_mev"

// Samples
ClobDeleverageSubaccountTotalQuoteQuantumsDistribution = "clob_deleverage_subaccount_total_quote_quantums_distribution"
DeleveragingPercentFilledDistribution = "deleveraging_percent_filled_distribution"
ClobDeleveragingNumSubaccountsIteratedCount = "clob_deleveraging_num_subaccounts_iterated_count"
ClobDeleveragingNonOverlappingBankrupcyPricesCount = "clob_deleveraging_non_overlapping_bankruptcy_prices_count"
ClobDeleveragingNoOpenPositionOnOppositeSideCount = "clob_deleveraging_no_open_position_on_opposite_side_count"
ClobDeleverageSubaccountFilledQuoteQuantums = "clob_deleverage_subaccount_filled_quote_quantums"
LiquidationsLiquidatableSubaccountIdsCount = "liquidations_liquidatable_subaccount_ids_count"
LiquidationsPercentFilledDistribution = "liquidations_percent_filled_distribution"
LiquidationsPlacePerpetualLiquidationQuoteQuantumsDistribution = "liquidations_place_perpetual_liquidation_quote_quantums_distribution"

// Measure Since
ClobOffsettingSubaccountPerpetualPosition = "clob_offsetting_subaccount_perpetual_position"
MevLatency = "mev_latency"
)
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 @@ -4,7 +4,6 @@ import (
"fmt"
"time"

gometrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
liquidationtypes "github.com/dydxprotocol/v4-chain/protocol/daemons/server/types/liquidations"
Expand Down Expand Up @@ -63,10 +62,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 @@ -108,10 +107,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
83 changes: 41 additions & 42 deletions protocol/x/clob/keeper/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package keeper
import (
"errors"
"fmt"
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
"math/big"
"time"

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

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/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
Expand Down Expand Up @@ -41,11 +40,9 @@ func (k Keeper) MaybeDeleverageSubaccount(

// Early return to skip deleveraging if the subaccount can't be deleveraged.
if !canPerformDeleveraging {
telemetry.IncrCounter(
metrics.IncrCounter(
metrics.ClobPrepareCheckStateCannotDeleverageSubaccount,
1,
types.ModuleName,
metrics.PrepareCheckState,
metrics.CannotDeleverageSubaccount,
)
return new(big.Int), nil
}
Expand All @@ -67,7 +64,7 @@ func (k Keeper) MaybeDeleverageSubaccount(
deltaQuantums := new(big.Int).Neg(position.GetBigQuantums())
quantumsDeleveraged, err = k.MemClob.DeleverageSubaccount(ctx, subaccountId, perpetualId, deltaQuantums)

labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
metrics.GetLabelForBoolValue(metrics.IsLong, deltaQuantums.Sign() == -1),
}
Expand All @@ -79,22 +76,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 @@ -103,10 +105,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 @@ -195,11 +198,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 @@ -280,29 +282,25 @@ func (k Keeper) OffsetSubaccountPerpetualPosition(
k.GetPseudoRand(ctx),
)

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 @@ -423,15 +421,16 @@ func (k Keeper) ProcessDeleveraging(
perpetualId,
new(big.Int).Abs(deltaQuantums),
); err == nil {
labels := []gometrics.Label{
labels := []metrics.Label{
metrics.GetLabelForIntValue(metrics.PerpetualId, int(perpetualId)),
metrics.GetLabelForBoolValue(metrics.CheckTx, ctx.IsCheckTx()),
metrics.GetLabelForBoolValue(metrics.IsLong, deltaQuantums.Sign() == -1),
}
gometrics.AddSampleWithLabels(
[]string{types.ModuleName, metrics.DeleverageSubaccount, metrics.Filled, metrics.QuoteQuantums},

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

Expand Down
Loading