From f378a1aa93203cc1aa4b27496340e8f50d1f9316 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Tue, 26 Mar 2024 12:34:03 +0100 Subject: [PATCH 01/13] Conditionally emit metrics based on enablement --- telemetry/metrics.go | 16 ++++++++++++++++ telemetry/wrapper.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/telemetry/metrics.go b/telemetry/metrics.go index 502fdb611209..66ce9914d92d 100644 --- a/telemetry/metrics.go +++ b/telemetry/metrics.go @@ -14,6 +14,21 @@ import ( "github.com/prometheus/common/expfmt" ) +// globalTelemetryEnabled is a private variable that stores the telemetry enabled state. +// It is set on initialization and does not change for the lifetime of the program. +var globalTelemetryEnabled bool + +// initTelemetry sets the global variable based on the configuration. +// It is called only once, at startup, to set the telemetry enabled state. +func initTelemetry(enabled bool) { + globalTelemetryEnabled = enabled +} + +// isTelemetryEnabled provides controlled access to check if telemetry is enabled. +func isTelemetryEnabled() bool { + return globalTelemetryEnabled +} + // globalLabels defines the set of global labels that will be applied to all // metrics emitted using the telemetry package function wrappers. var globalLabels = []metrics.Label{} @@ -95,6 +110,7 @@ type GatherResponse struct { // New creates a new instance of Metrics func New(cfg Config) (_ *Metrics, rerr error) { + initTelemetry(cfg.Enabled) if !cfg.Enabled { return nil, nil } diff --git a/telemetry/wrapper.go b/telemetry/wrapper.go index 4cd96b78f715..d97d0d132ccc 100644 --- a/telemetry/wrapper.go +++ b/telemetry/wrapper.go @@ -24,6 +24,10 @@ func NewLabel(name, value string) metrics.Label { // metric for a module with a given set of keys. If any global labels are defined, // they will be added to the module label. func ModuleMeasureSince(module string, start time.Time, keys ...string) { + if !isTelemetryEnabled() { + return + } + metrics.MeasureSinceWithLabels( keys, start.UTC(), @@ -35,6 +39,10 @@ func ModuleMeasureSince(module string, start time.Time, keys ...string) { // module with a given set of keys. If any global labels are defined, they will // be added to the module label. func ModuleSetGauge(module string, val float32, keys ...string) { + if !isTelemetryEnabled() { + return + } + metrics.SetGaugeWithLabels( keys, val, @@ -45,29 +53,49 @@ func ModuleSetGauge(module string, val float32, keys ...string) { // IncrCounter provides a wrapper functionality for emitting a counter metric with // global labels (if any). func IncrCounter(val float32, keys ...string) { + if !isTelemetryEnabled() { + return + } + metrics.IncrCounterWithLabels(keys, val, globalLabels) } // IncrCounterWithLabels provides a wrapper functionality for emitting a counter // metric with global labels (if any) along with the provided labels. func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) { + if !isTelemetryEnabled() { + return + } + metrics.IncrCounterWithLabels(keys, val, append(labels, globalLabels...)) } // SetGauge provides a wrapper functionality for emitting a gauge metric with // global labels (if any). func SetGauge(val float32, keys ...string) { + if !isTelemetryEnabled() { + return + } + metrics.SetGaugeWithLabels(keys, val, globalLabels) } // SetGaugeWithLabels provides a wrapper functionality for emitting a gauge // metric with global labels (if any) along with the provided labels. func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) { + if !isTelemetryEnabled() { + return + } + metrics.SetGaugeWithLabels(keys, val, append(labels, globalLabels...)) } // MeasureSince provides a wrapper functionality for emitting a a time measure // metric with global labels (if any). func MeasureSince(start time.Time, keys ...string) { + if !isTelemetryEnabled() { + return + } + metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels) } From ee6fac92ac80c8f8014d9ea587bf23e340806c38 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Tue, 26 Mar 2024 12:49:53 +0100 Subject: [PATCH 02/13] Add telemetry.Now() and refactor the methods that utilize it --- baseapp/abci.go | 2 +- telemetry/wrapper.go | 9 +++++++++ x/circuit/module.go | 4 +--- x/crisis/abci.go | 4 +--- x/crisis/module.go | 4 +--- x/distribution/keeper/abci.go | 4 +--- x/evidence/keeper/abci.go | 6 ++---- x/gov/keeper/abci.go | 2 +- x/mint/keeper/abci.go | 4 +--- x/slashing/abci.go | 4 +--- x/staking/keeper/abci.go | 6 ++---- x/upgrade/keeper/abci.go | 8 +++----- 12 files changed, 24 insertions(+), 33 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index e54824f41e0f..d4a0ae9df0be 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -177,7 +177,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.RequestQuery) (resp *abci telemetry.IncrCounter(1, "query", "count") telemetry.IncrCounter(1, "query", req.Path) - defer telemetry.MeasureSince(time.Now(), req.Path) + defer telemetry.MeasureSince(telemetry.Now(), req.Path) if req.Path == QueryPathBroadcastTx { return sdkerrors.QueryResult(errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "can't route a broadcast tx message"), app.trace), nil diff --git a/telemetry/wrapper.go b/telemetry/wrapper.go index d97d0d132ccc..94802271cc59 100644 --- a/telemetry/wrapper.go +++ b/telemetry/wrapper.go @@ -99,3 +99,12 @@ func MeasureSince(start time.Time, keys ...string) { metrics.MeasureSinceWithLabels(keys, start.UTC(), globalLabels) } + +// Now return the current time if telemetry is enabled or a zero time if it's not +func Now() time.Time { + if !isTelemetryEnabled() { + return time.Time{} + } + + return time.Now() +} diff --git a/x/circuit/module.go b/x/circuit/module.go index 5f14b29e4c8c..b087dd8779a1 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "time" - gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" @@ -93,7 +91,7 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error { // InitGenesis performs genesis initialization for the circuit module. func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error { - start := time.Now() + start := telemetry.Now() var genesisState types.GenesisState if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { return err diff --git a/x/crisis/abci.go b/x/crisis/abci.go index 8ce275ecd799..aaa3f424cd80 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -2,8 +2,6 @@ package crisis import ( "context" - "time" - "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/crisis/keeper" @@ -12,7 +10,7 @@ import ( // check all registered invariants func EndBlocker(ctx context.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) sdkCtx := sdk.UnwrapSDKContext(ctx) if k.InvCheckPeriod() == 0 || sdkCtx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { diff --git a/x/crisis/module.go b/x/crisis/module.go index 807a276aca93..49f99d976091 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "time" - "github.com/spf13/cobra" "google.golang.org/grpc" @@ -118,7 +116,7 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error { // InitGenesis performs genesis initialization for the crisis module. func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error { - start := time.Now() + start := telemetry.Now() var genesisState types.GenesisState if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { return err diff --git a/x/distribution/keeper/abci.go b/x/distribution/keeper/abci.go index 544b62e1e69e..60d1aea984a9 100644 --- a/x/distribution/keeper/abci.go +++ b/x/distribution/keeper/abci.go @@ -1,8 +1,6 @@ package keeper import ( - "time" - "cosmossdk.io/x/distribution/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -13,7 +11,7 @@ import ( // and distribute rewards for the previous block. // TODO: use context.Context after including the comet service func (k Keeper) BeginBlocker(ctx sdk.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // determine the total power signing the block var previousTotalPower int64 diff --git a/x/evidence/keeper/abci.go b/x/evidence/keeper/abci.go index 0ad33d81a73d..25d4b4439988 100644 --- a/x/evidence/keeper/abci.go +++ b/x/evidence/keeper/abci.go @@ -2,11 +2,9 @@ package keeper import ( "context" - "fmt" - "time" - "cosmossdk.io/core/comet" "cosmossdk.io/x/evidence/types" + "fmt" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" @@ -15,7 +13,7 @@ import ( // BeginBlocker iterates through and handles any newly discovered evidence of // misbehavior submitted by CometBFT. Currently, only equivocation is handled. func (k Keeper) BeginBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) bi := sdk.UnwrapSDKContext(ctx).CometInfo() diff --git a/x/gov/keeper/abci.go b/x/gov/keeper/abci.go index 4e4052102b31..4969d4abfbda 100644 --- a/x/gov/keeper/abci.go +++ b/x/gov/keeper/abci.go @@ -21,7 +21,7 @@ import ( // EndBlocker is called every block. func (k Keeper) EndBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) logger := k.Logger() // delete dead proposals from store and returns theirs deposits. diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index 8c3c1b3a4b01..33e02b4f5e52 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -2,8 +2,6 @@ package keeper import ( "context" - "time" - "cosmossdk.io/core/event" "cosmossdk.io/x/mint/types" @@ -13,7 +11,7 @@ import ( // BeginBlocker mints new tokens for the previous block. func (k Keeper) BeginBlocker(ctx context.Context, ic types.InflationCalculationFn) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params minter, err := k.Minter.Get(ctx) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 20613b40cfd6..14ba06824d9f 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -2,8 +2,6 @@ package slashing import ( "context" - "time" - "cosmossdk.io/x/slashing/keeper" "cosmossdk.io/x/slashing/types" @@ -14,7 +12,7 @@ import ( // BeginBlocker check for infraction evidence or downtime of validators // on every begin block func BeginBlocker(ctx context.Context, k keeper.Keeper) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // Iterate over all the validators which *should* have signed this block // store whether or not they have actually signed it and slash/unbond any diff --git a/x/staking/keeper/abci.go b/x/staking/keeper/abci.go index 174fd5c21af7..9f09b942ae77 100644 --- a/x/staking/keeper/abci.go +++ b/x/staking/keeper/abci.go @@ -2,8 +2,6 @@ package keeper import ( "context" - "time" - "cosmossdk.io/x/staking/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -13,12 +11,12 @@ import ( // BeginBlocker will persist the current header and validator set as a historical entry // and prune the oldest entry based on the HistoricalEntries parameter func (k *Keeper) BeginBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) return k.TrackHistoricalInfo(ctx) } // EndBlocker called at every block, update validator set func (k *Keeper) EndBlocker(ctx context.Context) ([]module.ValidatorUpdate, error) { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) return k.BlockValidatorUpdates(ctx) } diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index 6ef39c8062b0..5492dc056e0b 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -2,12 +2,10 @@ package keeper import ( "context" - "errors" - "fmt" - "time" - storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/upgrade/types" + "errors" + "fmt" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,7 +20,7 @@ import ( // a migration to be executed if needed upon this switch (migration defined in the new binary) // skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped func (k Keeper) PreBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) blockHeight := k.environment.HeaderService.GetHeaderInfo(ctx).Height plan, err := k.GetUpgradePlan(ctx) From 91f7b6b423b576fab160c81e3828478e8b17bd05 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Wed, 27 Mar 2024 11:47:38 +0100 Subject: [PATCH 03/13] fix time.now on defers --- baseapp/abci.go | 3 ++- x/crisis/abci.go | 3 ++- x/distribution/keeper/abci.go | 3 ++- x/evidence/keeper/abci.go | 3 ++- x/gov/keeper/abci.go | 3 ++- x/mint/keeper/abci.go | 3 ++- x/slashing/abci.go | 3 ++- x/staking/keeper/abci.go | 6 ++++-- x/upgrade/keeper/abci.go | 3 ++- 9 files changed, 20 insertions(+), 10 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index d4a0ae9df0be..ed80059e4d02 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -177,7 +177,8 @@ func (app *BaseApp) Query(_ context.Context, req *abci.RequestQuery) (resp *abci telemetry.IncrCounter(1, "query", "count") telemetry.IncrCounter(1, "query", req.Path) - defer telemetry.MeasureSince(telemetry.Now(), req.Path) + start := telemetry.Now() + defer telemetry.MeasureSince(start, req.Path) if req.Path == QueryPathBroadcastTx { return sdkerrors.QueryResult(errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "can't route a broadcast tx message"), app.trace), nil diff --git a/x/crisis/abci.go b/x/crisis/abci.go index aaa3f424cd80..77eb71387f5b 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -10,7 +10,8 @@ import ( // check all registered invariants func EndBlocker(ctx context.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker) sdkCtx := sdk.UnwrapSDKContext(ctx) if k.InvCheckPeriod() == 0 || sdkCtx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { diff --git a/x/distribution/keeper/abci.go b/x/distribution/keeper/abci.go index 60d1aea984a9..8511d2314390 100644 --- a/x/distribution/keeper/abci.go +++ b/x/distribution/keeper/abci.go @@ -11,7 +11,8 @@ import ( // and distribute rewards for the previous block. // TODO: use context.Context after including the comet service func (k Keeper) BeginBlocker(ctx sdk.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) // determine the total power signing the block var previousTotalPower int64 diff --git a/x/evidence/keeper/abci.go b/x/evidence/keeper/abci.go index 25d4b4439988..600bf9ce4878 100644 --- a/x/evidence/keeper/abci.go +++ b/x/evidence/keeper/abci.go @@ -13,7 +13,8 @@ import ( // BeginBlocker iterates through and handles any newly discovered evidence of // misbehavior submitted by CometBFT. Currently, only equivocation is handled. func (k Keeper) BeginBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) bi := sdk.UnwrapSDKContext(ctx).CometInfo() diff --git a/x/gov/keeper/abci.go b/x/gov/keeper/abci.go index 4969d4abfbda..6e4bb2f1b80b 100644 --- a/x/gov/keeper/abci.go +++ b/x/gov/keeper/abci.go @@ -21,7 +21,8 @@ import ( // EndBlocker is called every block. func (k Keeper) EndBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker) logger := k.Logger() // delete dead proposals from store and returns theirs deposits. diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index 33e02b4f5e52..29ea83bbc5ee 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -11,7 +11,8 @@ import ( // BeginBlocker mints new tokens for the previous block. func (k Keeper) BeginBlocker(ctx context.Context, ic types.InflationCalculationFn) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) // fetch stored minter & params minter, err := k.Minter.Get(ctx) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 14ba06824d9f..9514129d96e9 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -12,7 +12,8 @@ import ( // BeginBlocker check for infraction evidence or downtime of validators // on every begin block func BeginBlocker(ctx context.Context, k keeper.Keeper) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) // Iterate over all the validators which *should* have signed this block // store whether or not they have actually signed it and slash/unbond any diff --git a/x/staking/keeper/abci.go b/x/staking/keeper/abci.go index 9f09b942ae77..012d5286d663 100644 --- a/x/staking/keeper/abci.go +++ b/x/staking/keeper/abci.go @@ -11,12 +11,14 @@ import ( // BeginBlocker will persist the current header and validator set as a historical entry // and prune the oldest entry based on the HistoricalEntries parameter func (k *Keeper) BeginBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) return k.TrackHistoricalInfo(ctx) } // EndBlocker called at every block, update validator set func (k *Keeper) EndBlocker(ctx context.Context) ([]module.ValidatorUpdate, error) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker) return k.BlockValidatorUpdates(ctx) } diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index 5492dc056e0b..2fde7cb4a7bf 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -20,7 +20,8 @@ import ( // a migration to be executed if needed upon this switch (migration defined in the new binary) // skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped func (k Keeper) PreBlocker(ctx context.Context) error { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) + start := telemetry.Now() + defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) blockHeight := k.environment.HeaderService.GetHeaderInfo(ctx).Height plan, err := k.GetUpgradePlan(ctx) From de6950e1d47557cc51cd1ad5e79df9dc3a568238 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Thu, 28 Mar 2024 19:35:41 +0100 Subject: [PATCH 04/13] fix linter --- x/circuit/module.go | 1 + x/crisis/abci.go | 1 + x/crisis/module.go | 1 + x/evidence/keeper/abci.go | 3 ++- x/mint/keeper/abci.go | 1 + x/slashing/abci.go | 1 + x/staking/keeper/abci.go | 1 + x/upgrade/keeper/abci.go | 6 ++++-- 8 files changed, 12 insertions(+), 3 deletions(-) diff --git a/x/circuit/module.go b/x/circuit/module.go index b087dd8779a1..8f7e4b4e389b 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" diff --git a/x/crisis/abci.go b/x/crisis/abci.go index 77eb71387f5b..ac5523609f1c 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -2,6 +2,7 @@ package crisis import ( "context" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/crisis/keeper" diff --git a/x/crisis/module.go b/x/crisis/module.go index 49f99d976091..e658afc588f3 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/spf13/cobra" "google.golang.org/grpc" diff --git a/x/evidence/keeper/abci.go b/x/evidence/keeper/abci.go index 600bf9ce4878..837e9d8403fd 100644 --- a/x/evidence/keeper/abci.go +++ b/x/evidence/keeper/abci.go @@ -2,9 +2,10 @@ package keeper import ( "context" + "fmt" + "cosmossdk.io/core/comet" "cosmossdk.io/x/evidence/types" - "fmt" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index 29ea83bbc5ee..334290929519 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "cosmossdk.io/core/event" "cosmossdk.io/x/mint/types" diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 9514129d96e9..58f050b870ea 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -2,6 +2,7 @@ package slashing import ( "context" + "cosmossdk.io/x/slashing/keeper" "cosmossdk.io/x/slashing/types" diff --git a/x/staking/keeper/abci.go b/x/staking/keeper/abci.go index cf0a852b25c6..de7b66c9d1af 100644 --- a/x/staking/keeper/abci.go +++ b/x/staking/keeper/abci.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "cosmossdk.io/core/appmodule" "cosmossdk.io/x/staking/types" diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index 2fde7cb4a7bf..a3ac6eacc69e 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -2,11 +2,13 @@ package keeper import ( "context" - storetypes "cosmossdk.io/store/types" - "cosmossdk.io/x/upgrade/types" "errors" "fmt" + + storetypes "cosmossdk.io/store/types" + "cosmossdk.io/x/upgrade/types" + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" ) From 75dafb66468e07d7403c039d7932f27680ba284f Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 11:35:44 +0100 Subject: [PATCH 05/13] fix linter --- x/upgrade/keeper/abci.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index a3ac6eacc69e..682de34ad0e4 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -4,8 +4,6 @@ import ( "context" "errors" "fmt" - - storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/upgrade/types" From a27839df679a113979008ec0bf6cd87be652b076 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 12:05:12 +0100 Subject: [PATCH 06/13] minor fix --- x/upgrade/keeper/abci.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index 682de34ad0e4..f92a909f98b6 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/upgrade/types" From 42435f4fc140c9eb9983e809437f655325680fec Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 13:45:48 +0100 Subject: [PATCH 07/13] tests --- telemetry/metrics.go | 7 ++++--- telemetry/wrapper_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 telemetry/wrapper_test.go diff --git a/telemetry/metrics.go b/telemetry/metrics.go index 66ce9914d92d..433b7806683f 100644 --- a/telemetry/metrics.go +++ b/telemetry/metrics.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "sync/atomic" "time" "github.com/hashicorp/go-metrics" @@ -16,17 +17,17 @@ import ( // globalTelemetryEnabled is a private variable that stores the telemetry enabled state. // It is set on initialization and does not change for the lifetime of the program. -var globalTelemetryEnabled bool +var globalTelemetryEnabled atomic.Bool // initTelemetry sets the global variable based on the configuration. // It is called only once, at startup, to set the telemetry enabled state. func initTelemetry(enabled bool) { - globalTelemetryEnabled = enabled + globalTelemetryEnabled.Store(enabled) } // isTelemetryEnabled provides controlled access to check if telemetry is enabled. func isTelemetryEnabled() bool { - return globalTelemetryEnabled + return globalTelemetryEnabled.Load() } // globalLabels defines the set of global labels that will be applied to all diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go new file mode 100644 index 000000000000..56b97bf9f4e7 --- /dev/null +++ b/telemetry/wrapper_test.go @@ -0,0 +1,22 @@ +package telemetry + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestNow(t *testing.T) { + initTelemetry(true) + + currentTime := time.Now() + telemetryTime := Now() + + assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") + assert.WithinDuration(t, currentTime, telemetryTime, time.Second, "Now() should be close to current time") + + initTelemetry(false) + + telemetryTime = Now() + assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") +} From 0390f3eacdc5920b520bebf1afd2234b75872a9c Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 13:55:58 +0100 Subject: [PATCH 08/13] linter --- telemetry/wrapper_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go index 56b97bf9f4e7..7ba24226818a 100644 --- a/telemetry/wrapper_test.go +++ b/telemetry/wrapper_test.go @@ -1,9 +1,10 @@ package telemetry import ( - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestNow(t *testing.T) { From 88eb4bcf79d35ecdd051998381ec5b97f14bbf70 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 15:12:28 +0100 Subject: [PATCH 09/13] telemetry tests --- telemetry/wrapper_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go index 7ba24226818a..0c75bd8c5100 100644 --- a/telemetry/wrapper_test.go +++ b/telemetry/wrapper_test.go @@ -21,3 +21,15 @@ func TestNow(t *testing.T) { telemetryTime = Now() assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") } + +func TestIsTelemetryEnabled(t *testing.T) { + initTelemetry(true) + if !isTelemetryEnabled() { + t.Errorf("isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") + } + + initTelemetry(false) + if isTelemetryEnabled() { + t.Errorf("isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") + } +} From e49123d9876961f88c757189a7e2671f97688b11 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 15:22:23 +0100 Subject: [PATCH 10/13] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d72b6cd7494..cb5fdccccc7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,10 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i ### Improvements +* (telemetry) [#19903](https://github.com/cosmos/cosmos-sdk/pull/19903) Conditionally emit metrics based on enablement. + * **Introduction of `Now` Function**: Added a new function called `Now` to the telemetry package. It returns the current system time if telemetry is enabled, or a zero time if telemetry is not enabled. + * **Atomic Global Variable**: Implemented an atomic global variable to manage the state of telemetry's enablement. This ensures thread safety for the telemetry state. + * **Conditional Telemetry Emission**: All telemetry functions have been updated to emit metrics only when telemetry is enabled. They perform a check with `isTelemetryEnabled()` and return early if telemetry is disabled, minimizing unnecessary operations and overhead. * (types) [#19869](https://github.com/cosmos/cosmos-sdk/pull/19869) Removed `Any` type from `codec/types` and replaced it with an alias for `cosmos/gogoproto/types/any`. * (server) [#19854](https://github.com/cosmos/cosmos-sdk/pull/19854) Add customizability to start command. * Add `StartCmdOptions` in `server.AddCommands` instead of `servertypes.ModuleInitFlags`. To set custom flags set them in the `StartCmdOptions` struct on the `AddFlags` field. From a76e4f886c2c039c95f3710ec484d41a2dd84961 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Fri, 29 Mar 2024 15:46:39 +0100 Subject: [PATCH 11/13] enhanced telemetry tests --- telemetry/wrapper_test.go | 51 +++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go index 0c75bd8c5100..c05e668ec1fb 100644 --- a/telemetry/wrapper_test.go +++ b/telemetry/wrapper_test.go @@ -1,35 +1,54 @@ package telemetry import ( + "sync" "testing" "time" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" ) -func TestNow(t *testing.T) { - initTelemetry(true) +// TelemetrySuite is a struct that holds the setup for the telemetry tests. +// It includes a mutex to ensure that tests that depend on the global state +// do not run in parallel, which can cause race conditions and unpredictable results. +type TelemetrySuite struct { + suite.Suite + mu sync.Mutex +} - currentTime := time.Now() - telemetryTime := Now() +// SetupTest is called before each test to reset the global state to a known disabled state. +// This ensures each test starts with the telemetry disabled +func (suite *TelemetrySuite) SetupTest() { + initTelemetry(false) +} - assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") - assert.WithinDuration(t, currentTime, telemetryTime, time.Second, "Now() should be close to current time") +// TestNow tests the Now function when telemetry is enabled and disabled. +func (suite *TelemetrySuite) TestNow() { + suite.mu.Lock() + defer suite.mu.Unlock() - initTelemetry(false) + initTelemetry(true) + telemetryTime := Now() + suite.NotEqual(time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") + initTelemetry(false) telemetryTime = Now() - assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") + suite.Equal(time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") } -func TestIsTelemetryEnabled(t *testing.T) { +// TestIsTelemetryEnabled tests the isTelemetryEnabled function. +func (suite *TelemetrySuite) TestIsTelemetryEnabled() { + suite.mu.Lock() + defer suite.mu.Unlock() + initTelemetry(true) - if !isTelemetryEnabled() { - t.Errorf("isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") - } + suite.True(isTelemetryEnabled(), "isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") initTelemetry(false) - if isTelemetryEnabled() { - t.Errorf("isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") - } + suite.False(isTelemetryEnabled(), "isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") +} + +// TestTelemetrySuite initiates the test suite. +func TestTelemetrySuite(t *testing.T) { + suite.Run(t, new(TelemetrySuite)) } From af0ba45da918dc59715b8c34e3ac974c4ee39f9c Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Wed, 10 Apr 2024 16:55:46 +0200 Subject: [PATCH 12/13] Improvents on telemetry calls --- baseapp/abci.go | 3 +-- server/start.go | 4 --- telemetry/metrics.go | 17 ++++--------- telemetry/wrapper.go | 16 ++++++------ telemetry/wrapper_test.go | 48 ++++++++++++++++------------------- x/crisis/abci.go | 3 +-- x/crisis/module.go | 3 +-- x/distribution/keeper/abci.go | 3 +-- x/evidence/keeper/abci.go | 3 +-- x/gov/keeper/abci.go | 3 +-- x/mint/keeper/abci.go | 3 +-- x/slashing/abci.go | 3 +-- x/staking/keeper/abci.go | 3 +-- x/upgrade/keeper/abci.go | 3 +-- 14 files changed, 45 insertions(+), 70 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index ed80059e4d02..e54824f41e0f 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -177,8 +177,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.RequestQuery) (resp *abci telemetry.IncrCounter(1, "query", "count") telemetry.IncrCounter(1, "query", req.Path) - start := telemetry.Now() - defer telemetry.MeasureSince(start, req.Path) + defer telemetry.MeasureSince(time.Now(), req.Path) if req.Path == QueryPathBroadcastTx { return sdkerrors.QueryResult(errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "can't route a broadcast tx message"), app.trace), nil diff --git a/server/start.go b/server/start.go index a3cb688df669..6fdbe968db77 100644 --- a/server/start.go +++ b/server/start.go @@ -533,10 +533,6 @@ func startAPIServer( } func startTelemetry(cfg serverconfig.Config) (*telemetry.Metrics, error) { - if !cfg.Telemetry.Enabled { - return nil, nil - } - return telemetry.New(cfg.Telemetry) } diff --git a/telemetry/metrics.go b/telemetry/metrics.go index 433b7806683f..07d1020eb892 100644 --- a/telemetry/metrics.go +++ b/telemetry/metrics.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "net/http" - "sync/atomic" "time" "github.com/hashicorp/go-metrics" @@ -17,17 +16,11 @@ import ( // globalTelemetryEnabled is a private variable that stores the telemetry enabled state. // It is set on initialization and does not change for the lifetime of the program. -var globalTelemetryEnabled atomic.Bool +var globalTelemetryEnabled bool -// initTelemetry sets the global variable based on the configuration. -// It is called only once, at startup, to set the telemetry enabled state. -func initTelemetry(enabled bool) { - globalTelemetryEnabled.Store(enabled) -} - -// isTelemetryEnabled provides controlled access to check if telemetry is enabled. -func isTelemetryEnabled() bool { - return globalTelemetryEnabled.Load() +// IsTelemetryEnabled provides controlled access to check if telemetry is enabled. +func IsTelemetryEnabled() bool { + return globalTelemetryEnabled } // globalLabels defines the set of global labels that will be applied to all @@ -111,7 +104,7 @@ type GatherResponse struct { // New creates a new instance of Metrics func New(cfg Config) (_ *Metrics, rerr error) { - initTelemetry(cfg.Enabled) + globalTelemetryEnabled = cfg.Enabled if !cfg.Enabled { return nil, nil } diff --git a/telemetry/wrapper.go b/telemetry/wrapper.go index 94802271cc59..da11f1fa0450 100644 --- a/telemetry/wrapper.go +++ b/telemetry/wrapper.go @@ -24,7 +24,7 @@ func NewLabel(name, value string) metrics.Label { // metric for a module with a given set of keys. If any global labels are defined, // they will be added to the module label. func ModuleMeasureSince(module string, start time.Time, keys ...string) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -39,7 +39,7 @@ func ModuleMeasureSince(module string, start time.Time, keys ...string) { // module with a given set of keys. If any global labels are defined, they will // be added to the module label. func ModuleSetGauge(module string, val float32, keys ...string) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -53,7 +53,7 @@ func ModuleSetGauge(module string, val float32, keys ...string) { // IncrCounter provides a wrapper functionality for emitting a counter metric with // global labels (if any). func IncrCounter(val float32, keys ...string) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -63,7 +63,7 @@ func IncrCounter(val float32, keys ...string) { // IncrCounterWithLabels provides a wrapper functionality for emitting a counter // metric with global labels (if any) along with the provided labels. func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -73,7 +73,7 @@ func IncrCounterWithLabels(keys []string, val float32, labels []metrics.Label) { // SetGauge provides a wrapper functionality for emitting a gauge metric with // global labels (if any). func SetGauge(val float32, keys ...string) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -83,7 +83,7 @@ func SetGauge(val float32, keys ...string) { // SetGaugeWithLabels provides a wrapper functionality for emitting a gauge // metric with global labels (if any) along with the provided labels. func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -93,7 +93,7 @@ func SetGaugeWithLabels(keys []string, val float32, labels []metrics.Label) { // MeasureSince provides a wrapper functionality for emitting a a time measure // metric with global labels (if any). func MeasureSince(start time.Time, keys ...string) { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return } @@ -102,7 +102,7 @@ func MeasureSince(start time.Time, keys ...string) { // Now return the current time if telemetry is enabled or a zero time if it's not func Now() time.Time { - if !isTelemetryEnabled() { + if !IsTelemetryEnabled() { return time.Time{} } diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go index c05e668ec1fb..c7bea88e2c59 100644 --- a/telemetry/wrapper_test.go +++ b/telemetry/wrapper_test.go @@ -5,50 +5,46 @@ import ( "testing" "time" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" ) -// TelemetrySuite is a struct that holds the setup for the telemetry tests. -// It includes a mutex to ensure that tests that depend on the global state -// do not run in parallel, which can cause race conditions and unpredictable results. -type TelemetrySuite struct { - suite.Suite - mu sync.Mutex +var mu sync.Mutex + +func initTelemetry(v bool) { + globalTelemetryEnabled = v } -// SetupTest is called before each test to reset the global state to a known disabled state. -// This ensures each test starts with the telemetry disabled -func (suite *TelemetrySuite) SetupTest() { +// Reset the global state to a known disabled state before each test. +func setupTest(t *testing.T) { + mu.Lock() // Ensure no other test can modify global state at the same time. + defer mu.Unlock() initTelemetry(false) } // TestNow tests the Now function when telemetry is enabled and disabled. -func (suite *TelemetrySuite) TestNow() { - suite.mu.Lock() - defer suite.mu.Unlock() +func TestNow(t *testing.T) { + setupTest(t) // Locks the mutex to avoid race condition. initTelemetry(true) telemetryTime := Now() - suite.NotEqual(time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") + assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled") + + setupTest(t) // Reset the global state and lock the mutex again. initTelemetry(false) telemetryTime = Now() - suite.Equal(time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") + assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled") } -// TestIsTelemetryEnabled tests the isTelemetryEnabled function. -func (suite *TelemetrySuite) TestIsTelemetryEnabled() { - suite.mu.Lock() - defer suite.mu.Unlock() +// TestIsTelemetryEnabled tests the IsTelemetryEnabled function. +func TestIsTelemetryEnabled(t *testing.T) { + setupTest(t) // Locks the mutex to avoid race condition. initTelemetry(true) - suite.True(isTelemetryEnabled(), "isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") + assert.True(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return true when globalTelemetryEnabled is set to true") - initTelemetry(false) - suite.False(isTelemetryEnabled(), "isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") -} + setupTest(t) // Reset the global state and lock the mutex again. -// TestTelemetrySuite initiates the test suite. -func TestTelemetrySuite(t *testing.T) { - suite.Run(t, new(TelemetrySuite)) + initTelemetry(false) + assert.False(t, IsTelemetryEnabled(), "IsTelemetryEnabled() should return false when globalTelemetryEnabled is set to false") } diff --git a/x/crisis/abci.go b/x/crisis/abci.go index ac5523609f1c..2eacc641c4ad 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -11,8 +11,7 @@ import ( // check all registered invariants func EndBlocker(ctx context.Context, k keeper.Keeper) { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) sdkCtx := sdk.UnwrapSDKContext(ctx) if k.InvCheckPeriod() == 0 || sdkCtx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { diff --git a/x/crisis/module.go b/x/crisis/module.go index e658afc588f3..7da203a6a0f5 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -117,12 +117,11 @@ func (am AppModule) ValidateGenesis(bz json.RawMessage) error { // InitGenesis performs genesis initialization for the crisis module. func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error { - start := telemetry.Now() var genesisState types.GenesisState if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { return err } - telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal") + telemetry.MeasureSince(telemetry.Now(), "InitGenesis", "crisis", "unmarshal") am.keeper.InitGenesis(ctx, &genesisState) if !am.skipGenesisInvariants { diff --git a/x/distribution/keeper/abci.go b/x/distribution/keeper/abci.go index 8511d2314390..60d1aea984a9 100644 --- a/x/distribution/keeper/abci.go +++ b/x/distribution/keeper/abci.go @@ -11,8 +11,7 @@ import ( // and distribute rewards for the previous block. // TODO: use context.Context after including the comet service func (k Keeper) BeginBlocker(ctx sdk.Context) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // determine the total power signing the block var previousTotalPower int64 diff --git a/x/evidence/keeper/abci.go b/x/evidence/keeper/abci.go index 837e9d8403fd..5e5321e40f3e 100644 --- a/x/evidence/keeper/abci.go +++ b/x/evidence/keeper/abci.go @@ -14,8 +14,7 @@ import ( // BeginBlocker iterates through and handles any newly discovered evidence of // misbehavior submitted by CometBFT. Currently, only equivocation is handled. func (k Keeper) BeginBlocker(ctx context.Context) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) bi := sdk.UnwrapSDKContext(ctx).CometInfo() diff --git a/x/gov/keeper/abci.go b/x/gov/keeper/abci.go index 6e4bb2f1b80b..4969d4abfbda 100644 --- a/x/gov/keeper/abci.go +++ b/x/gov/keeper/abci.go @@ -21,8 +21,7 @@ import ( // EndBlocker is called every block. func (k Keeper) EndBlocker(ctx context.Context) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyEndBlocker) logger := k.Logger() // delete dead proposals from store and returns theirs deposits. diff --git a/x/mint/keeper/abci.go b/x/mint/keeper/abci.go index 334290929519..04677ad2f317 100644 --- a/x/mint/keeper/abci.go +++ b/x/mint/keeper/abci.go @@ -12,8 +12,7 @@ import ( // BeginBlocker mints new tokens for the previous block. func (k Keeper) BeginBlocker(ctx context.Context, ic types.InflationCalculationFn) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params minter, err := k.Minter.Get(ctx) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 58f050b870ea..72645213d82b 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -13,8 +13,7 @@ import ( // BeginBlocker check for infraction evidence or downtime of validators // on every begin block func BeginBlocker(ctx context.Context, k keeper.Keeper) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // Iterate over all the validators which *should* have signed this block // store whether or not they have actually signed it and slash/unbond any diff --git a/x/staking/keeper/abci.go b/x/staking/keeper/abci.go index de7b66c9d1af..59db9be890cb 100644 --- a/x/staking/keeper/abci.go +++ b/x/staking/keeper/abci.go @@ -12,8 +12,7 @@ import ( // BeginBlocker will persist the current header and validator set as a historical entry // and prune the oldest entry based on the HistoricalEntries parameter func (k *Keeper) BeginBlocker(ctx context.Context) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) return k.TrackHistoricalInfo(ctx) } diff --git a/x/upgrade/keeper/abci.go b/x/upgrade/keeper/abci.go index f92a909f98b6..bd2844dcbf23 100644 --- a/x/upgrade/keeper/abci.go +++ b/x/upgrade/keeper/abci.go @@ -21,8 +21,7 @@ import ( // a migration to be executed if needed upon this switch (migration defined in the new binary) // skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped func (k Keeper) PreBlocker(ctx context.Context) error { - start := telemetry.Now() - defer telemetry.ModuleMeasureSince(types.ModuleName, start, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) blockHeight := k.environment.HeaderService.GetHeaderInfo(ctx).Height plan, err := k.GetUpgradePlan(ctx) From e2642b618c0ccf103dfb057e98e639d4920a4e41 Mon Sep 17 00:00:00 2001 From: Lucas Francisco Lopez Date: Thu, 11 Apr 2024 22:19:12 +0200 Subject: [PATCH 13/13] linting fixes --- telemetry/wrapper_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/telemetry/wrapper_test.go b/telemetry/wrapper_test.go index c7bea88e2c59..5388839874bc 100644 --- a/telemetry/wrapper_test.go +++ b/telemetry/wrapper_test.go @@ -16,6 +16,7 @@ func initTelemetry(v bool) { // Reset the global state to a known disabled state before each test. func setupTest(t *testing.T) { + t.Helper() mu.Lock() // Ensure no other test can modify global state at the same time. defer mu.Unlock() initTelemetry(false)