Skip to content

Commit

Permalink
Code Refactoring: Refactor telemetry to begin and end block (#316)
Browse files Browse the repository at this point in the history
* Remove hardcoded telemetry from modules

* Add telemetry to begin/endblock

* Remove test labels
  • Loading branch information
chungnicholas authored and yan-soon committed Sep 19, 2023
1 parent 315fc4a commit e173ed8
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 50 deletions.
6 changes: 6 additions & 0 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ package module
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/telemetry"
"sort"
"time"

"cosmossdk.io/core/appmodule"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -559,7 +561,9 @@ func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.R
for _, moduleName := range m.OrderBeginBlockers {
module, ok := m.Modules[moduleName].(BeginBlockAppModule)
if ok {
startTime := time.Now()
module.BeginBlock(ctx, req)
telemetry.ModuleMeasureSince(moduleName, startTime, telemetry.MetricKeyBeginBlocker)
}
}

Expand All @@ -580,7 +584,9 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo
if !ok {
continue
}
startTime := time.Now()
moduleValUpdates := module.EndBlock(ctx, req)
telemetry.ModuleMeasureSince(moduleName, startTime, telemetry.MetricKeyEndBlocker)

// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
Expand Down
4 changes: 0 additions & 4 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package capability
import (
"encoding/json"
"fmt"
"time"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand All @@ -19,7 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -148,8 +146,6 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
// BeginBlocker calls InitMemStore to assert that the memory store is initialized.
// It's safe to run multiple times.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

am.keeper.InitMemStore(ctx)

if am.sealKeeper && !am.keeper.IsSealed() {
Expand Down
6 changes: 0 additions & 6 deletions x/crisis/abci.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package crisis

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
)

// check all registered invariants
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

if k.InvCheckPeriod() == 0 || ctx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 {
// skip running the invariant check
return
Expand Down
6 changes: 0 additions & 6 deletions x/distribution/abci.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package distribution

import (
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

// BeginBlocker sets the proposer for determining distribution during endblock
// and distribute rewards for the previous block.
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// determine the total power signing the block
var previousTotalPower int64
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
Expand Down
4 changes: 0 additions & 4 deletions x/evidence/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package evidence

import (
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
Expand All @@ -15,8 +13,6 @@ import (
// BeginBlocker iterates through and handles any newly discovered evidence of
// misbehavior submitted by Tendermint. Currently, only equivocation is handled.
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

for _, tmEvidence := range req.ByzantineValidators {
switch tmEvidence.Type {
// It's still ongoing discussion how should we treat and slash attacks with
Expand Down
6 changes: 1 addition & 5 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package gov

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
logger := keeper.Logger(ctx)

// delete dead proposals from store and returns theirs deposits.
Expand Down
4 changes: 0 additions & 4 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mint

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
Expand All @@ -11,8 +9,6 @@ import (

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// fetch stored minter & params
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)
Expand Down
6 changes: 0 additions & 6 deletions x/slashing/abci.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package slashing

import (
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)

// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.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
// which have missed too many blocks in a row (downtime slashing)
Expand Down
12 changes: 2 additions & 10 deletions x/staking/abci.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package staking

import (
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// BeginBlocker will persist the current header and validator set as a historical entry
// and prune the oldest entry based on the HistoricalEntries parameter
func BeginBlocker(ctx sdk.Context, k *keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
k.TrackHistoricalInfo(ctx)
}

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k *keeper.Keeper) []abci.ValidatorUpdate {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
return k.BlockValidatorUpdates(ctx)
}
6 changes: 1 addition & 5 deletions x/upgrade/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package upgrade

import (
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
Expand All @@ -20,9 +18,7 @@ import (
// The purpose is to ensure the binary is switched EXACTLY at the desired block, and to allow
// 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 BeginBlocker(k *keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

func BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
plan, found := k.GetUpgradePlan(ctx)

if !k.DowngradeVerified() {
Expand Down

0 comments on commit e173ed8

Please sign in to comment.