From 307833aa2a1e663592cccc96d491ac8d7296d02f Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 17 Jul 2020 11:34:37 -0400 Subject: [PATCH] Fix measure since (#6759) Co-authored-by: Alessio Treglia --- baseapp/abci.go | 13 +++++++------ store/cachekv/store.go | 9 +++++---- store/gaskv/store.go | 9 +++++---- store/iavl/store.go | 13 +++++++------ telemetry/wrapper.go | 8 ++++---- x/crisis/abci.go | 4 +++- x/distribution/abci.go | 4 +++- x/evidence/abci.go | 3 ++- x/gov/abci.go | 3 ++- x/mint/abci.go | 4 +++- x/slashing/abci.go | 4 +++- x/staking/abci.go | 6 ++++-- x/upgrade/abci.go | 3 ++- 13 files changed, 50 insertions(+), 33 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index ea0fad47181f..4c58a70e4376 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -6,6 +6,7 @@ import ( "sort" "strings" "syscall" + "time" abci "github.com/tendermint/tendermint/abci/types" @@ -103,7 +104,7 @@ func (app *BaseApp) FilterPeerByID(info string) abci.ResponseQuery { // BeginBlock implements the ABCI application interface. func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { - defer telemetry.MeasureSince("abci", "begin_block") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "begin_block") if app.cms.TracingEnabled() { app.cms.SetTracingContext(sdk.TraceContext( @@ -148,7 +149,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg // EndBlock implements the ABCI interface. func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) { - defer telemetry.MeasureSince("abci", "end_block") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "end_block") if app.deliverState.ms.TracingEnabled() { app.deliverState.ms = app.deliverState.ms.SetTracingContext(nil).(sdk.CacheMultiStore) @@ -168,7 +169,7 @@ func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBloc // will contain releveant error information. Regardless of tx execution outcome, // the ResponseCheckTx will contain relevant gas execution context. func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { - defer telemetry.MeasureSince("abci", "check_tx") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "check_tx") tx, err := app.txDecoder(req.Tx) if err != nil { @@ -208,7 +209,7 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { // Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant // gas execution context. func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx { - defer telemetry.MeasureSince("abci", "deliver_tx") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "deliver_tx") tx, err := app.txDecoder(req.Tx) if err != nil { @@ -248,7 +249,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx // against that height and gracefully halt if it matches the latest committed // height. func (app *BaseApp) Commit() (res abci.ResponseCommit) { - defer telemetry.MeasureSince("abci", "commit") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "commit") header := app.deliverState.ctx.BlockHeader() @@ -316,7 +317,7 @@ func (app *BaseApp) halt() { // Query implements the ABCI interface. It delegates to CommitMultiStore if it // implements Queryable. func (app *BaseApp) Query(req abci.RequestQuery) abci.ResponseQuery { - defer telemetry.MeasureSince("abci", "query") + defer telemetry.MeasureSince(time.Now().UTC(), "abci", "query") // handle gRPC routes first rather than calling splitPath because '/' characters // are used as part of gRPC paths diff --git a/store/cachekv/store.go b/store/cachekv/store.go index 3c5ec5772311..3262e1616455 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -6,6 +6,7 @@ import ( "io" "sort" "sync" + "time" tmkv "github.com/tendermint/tendermint/libs/kv" dbm "github.com/tendermint/tm-db" @@ -52,7 +53,7 @@ func (store *Store) GetStoreType() types.StoreType { func (store *Store) Get(key []byte) (value []byte) { store.mtx.Lock() defer store.mtx.Unlock() - defer telemetry.MeasureSince("store", "cachekv", "get") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "cachekv", "get") types.AssertValidKey(key) @@ -71,7 +72,7 @@ func (store *Store) Get(key []byte) (value []byte) { func (store *Store) Set(key []byte, value []byte) { store.mtx.Lock() defer store.mtx.Unlock() - defer telemetry.MeasureSince("store", "cachekv", "set") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "cachekv", "set") types.AssertValidKey(key) types.AssertValidValue(value) @@ -89,7 +90,7 @@ func (store *Store) Has(key []byte) bool { func (store *Store) Delete(key []byte) { store.mtx.Lock() defer store.mtx.Unlock() - defer telemetry.MeasureSince("store", "cachekv", "delete") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "cachekv", "delete") types.AssertValidKey(key) store.setCacheValue(key, nil, true, true) @@ -99,7 +100,7 @@ func (store *Store) Delete(key []byte) { func (store *Store) Write() { store.mtx.Lock() defer store.mtx.Unlock() - defer telemetry.MeasureSince("store", "cachekv", "write") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "cachekv", "write") // We need a copy of all of the keys. // Not the best, but probably not a bottleneck depending. diff --git a/store/gaskv/store.go b/store/gaskv/store.go index 22bd182f55a8..7d014b161962 100644 --- a/store/gaskv/store.go +++ b/store/gaskv/store.go @@ -2,6 +2,7 @@ package gaskv import ( "io" + "time" "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -35,7 +36,7 @@ func (gs *Store) GetStoreType() types.StoreType { // Implements KVStore. func (gs *Store) Get(key []byte) (value []byte) { - defer telemetry.MeasureSince("store", "gaskv", "get") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "gaskv", "get") gs.gasMeter.ConsumeGas(gs.gasConfig.ReadCostFlat, types.GasReadCostFlatDesc) value = gs.parent.Get(key) @@ -48,7 +49,7 @@ func (gs *Store) Get(key []byte) (value []byte) { // Implements KVStore. func (gs *Store) Set(key []byte, value []byte) { - defer telemetry.MeasureSince("store", "gaskv", "set") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "gaskv", "set") types.AssertValidValue(value) gs.gasMeter.ConsumeGas(gs.gasConfig.WriteCostFlat, types.GasWriteCostFlatDesc) @@ -59,14 +60,14 @@ func (gs *Store) Set(key []byte, value []byte) { // Implements KVStore. func (gs *Store) Has(key []byte) bool { - defer telemetry.MeasureSince("store", "gaskv", "has") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "gaskv", "has") gs.gasMeter.ConsumeGas(gs.gasConfig.HasCost, types.GasHasDesc) return gs.parent.Has(key) } // Implements KVStore. func (gs *Store) Delete(key []byte) { - defer telemetry.MeasureSince("store", "gaskv", "delete") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "gaskv", "delete") // charge gas to prevent certain attack vectors even though space is being freed gs.gasMeter.ConsumeGas(gs.gasConfig.DeleteCost, types.GasDeleteDesc) gs.parent.Delete(key) diff --git a/store/iavl/store.go b/store/iavl/store.go index 85d9a5b3fcc0..bd6bbd431519 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "sync" + "time" ics23iavl "github.com/confio/ics23-iavl" ics23 "github.com/confio/ics23/go" @@ -96,7 +97,7 @@ func (st *Store) GetImmutable(version int64) (*Store, error) { // Commit commits the current store state and returns a CommitID with the new // version and hash. func (st *Store) Commit() types.CommitID { - defer telemetry.MeasureSince("store", "iavl", "commit") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "commit") hash, version, err := st.tree.SaveVersion() if err != nil { @@ -145,27 +146,27 @@ func (st *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.Ca // Implements types.KVStore. func (st *Store) Set(key, value []byte) { - defer telemetry.MeasureSince("store", "iavl", "set") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "set") types.AssertValidValue(value) st.tree.Set(key, value) } // Implements types.KVStore. func (st *Store) Get(key []byte) []byte { - defer telemetry.MeasureSince("store", "iavl", "get") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "get") _, value := st.tree.Get(key) return value } // Implements types.KVStore. func (st *Store) Has(key []byte) (exists bool) { - defer telemetry.MeasureSince("store", "iavl", "has") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "has") return st.tree.Has(key) } // Implements types.KVStore. func (st *Store) Delete(key []byte) { - defer telemetry.MeasureSince("store", "iavl", "delete") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "delete") st.tree.Remove(key) } @@ -226,7 +227,7 @@ func getHeight(tree Tree, req abci.RequestQuery) int64 { // if you care to have the latest data to see a tx results, you must // explicitly set the height you want to see func (st *Store) Query(req abci.RequestQuery) (res abci.ResponseQuery) { - defer telemetry.MeasureSince("store", "iavl", "query") + defer telemetry.MeasureSince(time.Now().UTC(), "store", "iavl", "query") if len(req.Data) == 0 { return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrTxDecode, "query cannot be zero length")) diff --git a/telemetry/wrapper.go b/telemetry/wrapper.go index db66cf585483..925ef4dd2d52 100644 --- a/telemetry/wrapper.go +++ b/telemetry/wrapper.go @@ -20,10 +20,10 @@ func NewLabel(name, value string) metrics.Label { // ModuleMeasureSince provides a short hand method for emitting a time measure // 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, keys ...string) { +func ModuleMeasureSince(module string, start time.Time, keys ...string) { metrics.MeasureSinceWithLabels( keys, - time.Now().UTC(), + start, append([]metrics.Label{NewLabel(MetricLabelNameModule, module)}, globalLabels...), ) } @@ -65,6 +65,6 @@ 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(keys ...string) { - metrics.MeasureSinceWithLabels(keys, time.Now().UTC(), globalLabels) +func MeasureSince(start time.Time, keys ...string) { + metrics.MeasureSinceWithLabels(keys, start, globalLabels) } diff --git a/x/crisis/abci.go b/x/crisis/abci.go index 2844b3c1f5c0..42eed9513f79 100644 --- a/x/crisis/abci.go +++ b/x/crisis/abci.go @@ -1,6 +1,8 @@ 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" @@ -9,7 +11,7 @@ import ( // check all registered invariants func EndBlocker(ctx sdk.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyEndBlocker) if k.InvCheckPeriod() == 0 || ctx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 { // skip running the invariant check diff --git a/x/distribution/abci.go b/x/distribution/abci.go index c81dc6b303a7..7386871675e1 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -1,6 +1,8 @@ package distribution import ( + "time" + abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -12,7 +14,7 @@ import ( // 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, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyBeginBlocker) // determine the total power signing the block var previousTotalPower, sumPreviousPrecommitPower int64 diff --git a/x/evidence/abci.go b/x/evidence/abci.go index da5bd22e4195..39c4e81e5587 100644 --- a/x/evidence/abci.go +++ b/x/evidence/abci.go @@ -2,6 +2,7 @@ package evidence import ( "fmt" + "time" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,7 +17,7 @@ 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, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyBeginBlocker) for _, tmEvidence := range req.ByzantineValidators { switch tmEvidence.Type { diff --git a/x/gov/abci.go b/x/gov/abci.go index 77dfd663dafe..2984941e20bc 100644 --- a/x/gov/abci.go +++ b/x/gov/abci.go @@ -2,6 +2,7 @@ package gov import ( "fmt" + "time" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" @@ -11,7 +12,7 @@ import ( // EndBlocker called every block, process inflation, update validator set. func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyEndBlocker) logger := keeper.Logger(ctx) diff --git a/x/mint/abci.go b/x/mint/abci.go index fa34ec161345..affa0ebd3bfc 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -1,6 +1,8 @@ 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" @@ -9,7 +11,7 @@ import ( // BeginBlocker mints new tokens for the previous block. func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyBeginBlocker) // fetch stored minter & params minter := k.GetMinter(ctx) diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 77a774b491ff..a04cd3c18682 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -1,6 +1,8 @@ package slashing import ( + "time" + abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -12,7 +14,7 @@ import ( // 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, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), 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/abci.go b/x/staking/abci.go index cebaa8cb2bce..9a289acda9f2 100644 --- a/x/staking/abci.go +++ b/x/staking/abci.go @@ -1,6 +1,8 @@ package staking import ( + "time" + abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/telemetry" @@ -12,14 +14,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 BeginBlocker(ctx sdk.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), 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, telemetry.MetricKeyEndBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyEndBlocker) return k.BlockValidatorUpdates(ctx) } diff --git a/x/upgrade/abci.go b/x/upgrade/abci.go index 0da6d5155a40..38e5082429f1 100644 --- a/x/upgrade/abci.go +++ b/x/upgrade/abci.go @@ -2,6 +2,7 @@ package upgrade import ( "fmt" + "time" abci "github.com/tendermint/tendermint/abci/types" @@ -20,7 +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 BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) { - defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.MetricKeyBeginBlocker) + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now().UTC(), telemetry.MetricKeyBeginBlocker) plan, found := k.GetUpgradePlan(ctx) if !found {