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 Jun 12, 2023
1 parent 8322ee4 commit 59b9aeb
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 51 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"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -481,7 +483,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 @@ -502,7 +506,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
7 changes: 1 addition & 6 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package capability
import (
"encoding/json"
"fmt"
"math/rand"
"time"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"math/rand"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/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 @@ -143,8 +140,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)
}

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/tendermint/tendermint/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, sumPreviousPrecommitPower int64
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
Expand Down
5 changes: 0 additions & 5 deletions x/evidence/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package evidence

import (
"fmt"
"time"

abci "github.com/tendermint/tendermint/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 +12,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
5 changes: 0 additions & 5 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ 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"
Expand All @@ -13,8 +10,6 @@ import (

// 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)

logger := keeper.Logger(ctx)

// delete dead proposals from store and returns theirs deposits. A proposal is dead when it's inactive and didn't get enough deposit on time to get into voting phase.
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/tendermint/tendermint/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
8 changes: 0 additions & 8 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/tendermint/tendermint/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)

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)

return k.BlockValidatorUpdates(ctx)
}
5 changes: 0 additions & 5 deletions x/upgrade/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package upgrade

import (
"fmt"
"time"

abci "github.com/tendermint/tendermint/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 @@ -21,8 +18,6 @@ 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 BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

plan, found := k.GetUpgradePlan(ctx)

if !found {
Expand Down

0 comments on commit 59b9aeb

Please sign in to comment.