Skip to content

Commit

Permalink
[OTE-209] Emit metrics gated through execution mode (dydxprotocol#1157)
Browse files Browse the repository at this point in the history
Signed-off-by: Eric <eric.warehime@gmail.com>
  • Loading branch information
affanv14 authored and Eric-Warehime committed Mar 12, 2024
1 parent 47e365d commit 7f178fe
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
28 changes: 28 additions & 0 deletions protocol/lib/metrics/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

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

Expand All @@ -12,6 +13,7 @@ import (
// that supports float64 (i.e hashicorp go-metrics)

type Label = gometrics.Label
type TelemetryEmitWithLabelsFunc func(key string, val float32, labels ...gometrics.Label)

// IncrCounterWithLabels provides a wrapper functionality for emitting a counter
// metric with global labels (if any) along with the provided labels.
Expand Down Expand Up @@ -90,3 +92,29 @@ func ModuleMeasureSinceWithLabels(
),
)
}

func isAllowedExecutionMode(
ctx sdk.Context,
allowedModes []sdk.ExecMode,
) bool {
contextExecMode := ctx.ExecMode()
for _, mode := range allowedModes {
if contextExecMode == mode {
return true
}
}
return false
}

func EmitTelemetryWithLabelsForExecMode(
ctx sdk.Context,
allowedModes []sdk.ExecMode,
telemtryFuncWithLabels TelemetryEmitWithLabelsFunc,
key string,
val float32,
labels ...gometrics.Label,
) {
if isAllowedExecutionMode(ctx, allowedModes) {
telemtryFuncWithLabels(key, val, labels...)
}
}
70 changes: 70 additions & 0 deletions protocol/lib/metrics/lib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package metrics_test

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
gometrics "github.com/hashicorp/go-metrics"

"github.com/stretchr/testify/require"
)

func TestSetGaugeWithLabelsForExecMode(t *testing.T) {
t.Cleanup(gometrics.Shutdown)
context := sdk.Context{}
conf := gometrics.DefaultConfig("testService")
conf.EnableHostname = false
sink := gometrics.NewInmemSink(time.Hour, time.Hour)
_, err := gometrics.NewGlobal(conf, sink)
require.NoError(t, err)

context = context.WithExecMode(sdk.ExecModeFinalize)
metrics.EmitTelemetryWithLabelsForExecMode(
context,
[]sdk.ExecMode{sdk.ExecModeFinalize},
metrics.SetGaugeWithLabels,
"testKey1",
3.14,
gometrics.Label{
Name: "testLabel",
Value: "testLabelValue",
},
)

metrics.EmitTelemetryWithLabelsForExecMode(
context,
[]sdk.ExecMode{sdk.ExecModeSimulate},
metrics.SetGaugeWithLabels,
"testKey2",
3.14,
gometrics.Label{
Name: "testLabel",
Value: "testLabelValue",
},
)

FinalizeModeKeyFound := false
SimulateModeKeyFound := false
for _, metrics := range sink.Data() {
metrics.RLock()
defer metrics.RUnlock()

if metric, ok := metrics.Gauges["testService.testKey1;testLabel=testLabelValue"]; ok {
require.Equal(t,
[]gometrics.Label{{
Name: "testLabel",
Value: "testLabelValue",
}},
metric.Labels)
require.Equal(t, float32(3.14), metric.Value)
FinalizeModeKeyFound = true
}
if _, ok := metrics.Gauges["testService.testKey2;testLabel=testLabelValue"]; ok {
SimulateModeKeyFound = true
}
}
require.True(t, FinalizeModeKeyFound)
require.False(t, SimulateModeKeyFound)
}
1 change: 1 addition & 0 deletions protocol/lib/metrics/metric_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
ClobConditionalOrderTriggerPrice = "clob_conditional_order_trigger_price"
ClobConditionalOrderTriggered = "clob_conditional_order_triggered"
ClobSubaccountsRequiringDeleveragingCount = "clob_subaccounts_requiring_deleveraging_count"
SendingProcessDepositToSubaccount = "sending_process_deposit_to_subaccount"

// Samples
ClobDeleverageSubaccountTotalQuoteQuantumsDistribution = "clob_deleverage_subaccount_total_quote_quantums_distribution"
Expand Down
15 changes: 7 additions & 8 deletions protocol/x/sending/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ func (k Keeper) ProcessDepositToSubaccount(

// Emit gauge metric with labels if deposit to subaccount succeeds.
if err == nil {
telemetry.SetGaugeWithLabels(
[]string{
types.ModuleName,
metrics.ProcessDepositToSubaccount,
},
metrics.EmitTelemetryWithLabelsForExecMode(
ctx,
// sdk.ExecModeFinalize is used here to ensure metrics are only emitted in the Finalize ExecMode.
[]sdk.ExecMode{sdk.ExecModeFinalize},
metrics.SetGaugeWithLabels,
metrics.SendingProcessDepositToSubaccount,
float32(msgDepositToSubaccount.Quantums),
[]gometrics.Label{
metrics.GetLabelForIntValue(metrics.AssetId, int(msgDepositToSubaccount.AssetId)),
},
metrics.GetLabelForIntValue(metrics.AssetId, int(msgDepositToSubaccount.AssetId)),
)

// Add deposit event to Indexer block message.
Expand Down

0 comments on commit 7f178fe

Please sign in to comment.