Skip to content

Commit

Permalink
Add per module latency metric for Begin, Mid, EndBlock (#234)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
Add more visibility over:
1. Per module begin block, total begin block latency
2. Per module mid block, total mid block latency
3. Per module end block, total end block latency
## Testing performed to validate your change
Verified in rpc node

Co-authored-by: Yiming Zang <[email protected]>
  • Loading branch information
yzang2019 and Yiming Zang authored May 2, 2023
1 parent 148625c commit 9848cbf
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import (
"encoding/json"
"fmt"
"sort"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -497,10 +499,13 @@ func (m Manager) RunMigrations(ctx sdk.Context, cfg Configurator, fromVM Version
func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
ctx = ctx.WithEventManager(sdk.NewEventManager())

defer telemetry.MeasureSince(time.Now(), "module", "total_begin_block")
for _, moduleName := range m.OrderBeginBlockers {
module, ok := m.Modules[moduleName].(BeginBlockAppModule)
if ok {
moduleStartTime := time.Now()
module.BeginBlock(ctx, req)
telemetry.ModuleMeasureSince(moduleName, moduleStartTime, "module", "begin_block")
}
}

Expand All @@ -515,12 +520,15 @@ func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.R
func (m *Manager) MidBlock(ctx sdk.Context, height int64) []abci.Event {
ctx = ctx.WithEventManager(sdk.NewEventManager())

defer telemetry.MeasureSince(time.Now(), "module", "total_mid_block")
for _, moduleName := range m.OrderMidBlockers {
module, ok := m.Modules[moduleName].(MidBlockAppModule)
if !ok {
continue
}
moduleStartTime := time.Now()
module.MidBlock(ctx, height)
telemetry.ModuleMeasureSince(moduleName, moduleStartTime, "module", "mid_block")
}

return ctx.EventManager().ABCIEvents()
Expand All @@ -532,14 +540,15 @@ func (m *Manager) MidBlock(ctx sdk.Context, height int64) []abci.Event {
func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
ctx = ctx.WithEventManager(sdk.NewEventManager())
validatorUpdates := []abci.ValidatorUpdate{}

defer telemetry.MeasureSince(time.Now(), "module", "total_end_block")
for _, moduleName := range m.OrderEndBlockers {
module, ok := m.Modules[moduleName].(EndBlockAppModule)
if !ok {
continue
}
moduleStartTime := time.Now()
moduleValUpdates := module.EndBlock(ctx, req)

telemetry.ModuleMeasureSince(moduleName, moduleStartTime, "module", "end_block")
// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
if len(moduleValUpdates) > 0 {
Expand All @@ -549,6 +558,7 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo

validatorUpdates = moduleValUpdates
}

}

return abci.ResponseEndBlock{
Expand Down

0 comments on commit 9848cbf

Please sign in to comment.