Skip to content

Commit

Permalink
Fix measure since (cosmos#6759)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessio Treglia <[email protected]>
  • Loading branch information
alexanderbez and Alessio Treglia authored Jul 17, 2020
1 parent c37f683 commit 5c86ecd
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 33 deletions.
13 changes: 7 additions & 6 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"syscall"
"time"

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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"sort"
"sync"
"time"

tmkv "github.com/tendermint/tendermint/libs/kv"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions store/gaskv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gaskv

import (
"io"
"time"

"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"sync"
"time"

ics23iavl "github.com/confio/ics23-iavl"
ics23 "github.com/confio/ics23/go"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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"))
Expand Down
8 changes: 4 additions & 4 deletions telemetry/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
)
}
Expand Down Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion x/crisis/abci.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion x/distribution/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package distribution

import (
"time"

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

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x/evidence/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evidence

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion x/slashing/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slashing

import (
"time"

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

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions x/staking/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package staking

import (
"time"

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

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion x/upgrade/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upgrade

import (
"fmt"
"time"

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

Expand All @@ -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 {
Expand Down

0 comments on commit 5c86ecd

Please sign in to comment.