Skip to content

Commit

Permalink
Add additional metrics to p2p and consensus
Browse files Browse the repository at this point in the history
Partially addresses cosmos/cosmos-sdk#2169.
  • Loading branch information
mslipper committed Sep 18, 2018
1 parent d419fff commit 9936e82
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ scripts/cutWALUntil/cutWALUntil
*.iml

libs/pubsub/query/fuzz_test/output
libs/db/test*
shunit2

.tendermint-lite
Expand All @@ -38,4 +39,4 @@ terraform.tfstate
terraform.tfstate.backup
terraform.tfstate.d

.vscode
.vscode
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ FEATURES:

IMPROVEMENTS:

- Added additional metrics to p2p and consensus

BUG FIXES:
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,15 @@ type TxIndexConfig struct {
// Comma-separated list of tags to index (by default the only tag is "tx.hash")
//
// You can also index transactions by height by adding "tx.height" tag here.
//
//
// It's recommended to index only a subset of tags due to possible memory
// bloat. This is, of course, depends on the indexer's DB and the volume of
// transactions.
IndexTags string `mapstructure:"index_tags"`

// When set to true, tells indexer to index all tags (predefined tags:
// "tx.hash", "tx.height" and all tags from DeliverTx responses).
//
// "tx.hash", "tx.height" and all tags from DeliverTx responses).
//
// Note this may be not desirable (see the comment above). IndexTags has a
// precedence over IndexAllTags (i.e. when given both, IndexTags will be
// indexed).
Expand Down
3 changes: 3 additions & 0 deletions config/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

const MetricsNamespace = "tendermint"
2 changes: 1 addition & 1 deletion consensus/byzantine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestByzantine(t *testing.T) {
err := eventBus.Subscribe(context.Background(), testSubscriber, types.EventQueryNewBlock, eventChans[i])
require.NoError(t, err)

conR := NewConsensusReactor(css[i], true) // so we dont start the consensus states
conR := NewConsensusReactor(css[i], true, NopMetrics()) // so we dont start the consensus states
conR.SetLogger(logger.With("validator", i))
conR.SetEventBus(eventBus)

Expand Down
65 changes: 49 additions & 16 deletions consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/discard"

prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
tmcfg "github.com/tendermint/tendermint/config"
)

const MetricsSubsystem = "consensus"

// Metrics contains metrics exposed by this package.
type Metrics struct {
// Height of the chain.
Expand Down Expand Up @@ -38,75 +41,103 @@ type Metrics struct {
BlockSizeBytes metrics.Gauge
// Total number of transactions.
TotalTxs metrics.Gauge
// The latest block height.
LatestBlockHeight metrics.Gauge
// Whether or not a node is synced. 0 if no, 1 if yes.
CatchingUp metrics.Gauge
}

// PrometheusMetrics returns Metrics build using Prometheus client library.
func PrometheusMetrics() *Metrics {
return &Metrics{
Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "height",
Help: "Height of the chain.",
}, []string{}),
Rounds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "rounds",
Help: "Number of rounds.",
}, []string{}),

Validators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "validators",
Help: "Number of validators.",
}, []string{}),
ValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "validators_power",
Help: "Total power of all validators.",
}, []string{}),
MissingValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "missing_validators",
Help: "Number of validators who did not sign.",
}, []string{}),
MissingValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "missing_validators_power",
Help: "Total power of the missing validators.",
}, []string{}),
ByzantineValidators: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "byzantine_validators",
Help: "Number of validators who tried to double sign.",
}, []string{}),
ByzantineValidatorsPower: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "byzantine_validators_power",
Help: "Total power of the byzantine validators.",
}, []string{}),

BlockIntervalSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "block_interval_seconds",
Help: "Time between this and the last block.",
Buckets: []float64{1, 2.5, 5, 10, 60},
}, []string{}),

NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "num_txs",
Help: "Number of transactions.",
}, []string{}),
BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "block_size_bytes",
Help: "Size of the block.",
}, []string{}),
TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "total_txs",
Help: "Total number of transactions.",
}, []string{}),
LatestBlockHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "latest_block_height",
Help: "The latest block height.",
}, []string{}),
CatchingUp: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: tmcfg.MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: "catching_up",
Help: "Whether or not a node is synced. 0 if syncing, 1 if synced.",
}, []string{}),
}
}

Expand All @@ -126,8 +157,10 @@ func NopMetrics() *Metrics {

BlockIntervalSeconds: discard.NewHistogram(),

NumTxs: discard.NewGauge(),
BlockSizeBytes: discard.NewGauge(),
TotalTxs: discard.NewGauge(),
NumTxs: discard.NewGauge(),
BlockSizeBytes: discard.NewGauge(),
TotalTxs: discard.NewGauge(),
LatestBlockHeight: discard.NewGauge(),
CatchingUp: discard.NewGauge(),
}
}
19 changes: 17 additions & 2 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/pkg/errors"

amino "github.com/tendermint/go-amino"
"github.com/tendermint/go-amino"

cstypes "github.com/tendermint/tendermint/consensus/types"
cmn "github.com/tendermint/tendermint/libs/common"
Expand Down Expand Up @@ -42,15 +42,19 @@ type ConsensusReactor struct {
mtx sync.RWMutex
fastSync bool
eventBus *types.EventBus

metrics *Metrics
}

// NewConsensusReactor returns a new ConsensusReactor with the given
// consensusState.
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *ConsensusReactor {
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool, csMetrics *Metrics) *ConsensusReactor {
conR := &ConsensusReactor{
conS: consensusState,
fastSync: fastSync,
metrics: csMetrics,
}
conR.setCatchingUp()
conR.BaseReactor = *p2p.NewBaseReactor("ConsensusReactor", conR)
return conR
}
Expand Down Expand Up @@ -94,6 +98,7 @@ func (conR *ConsensusReactor) SwitchToConsensus(state sm.State, blocksSynced int
conR.mtx.Lock()
conR.fastSync = false
conR.mtx.Unlock()
conR.metrics.CatchingUp.Set(0)

if blocksSynced > 0 {
// dont bother with the WAL if we fast synced
Expand Down Expand Up @@ -814,6 +819,16 @@ func (conR *ConsensusReactor) StringIndented(indent string) string {
return s
}

func (conR *ConsensusReactor) setCatchingUp() {
var catchingUp float64
if conR.fastSync {
catchingUp = 1
} else {
catchingUp = 0
}
conR.metrics.CatchingUp.Set(catchingUp)
}

//-----------------------------------------------------------------------------

var (
Expand Down
6 changes: 3 additions & 3 deletions consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func startConsensusNet(t *testing.T, css []*ConsensusState, N int) ([]*Consensus
for i := 0; i < N; i++ {
/*logger, err := tmflags.ParseLogLevel("consensus:info,*:error", logger, "info")
if err != nil { t.Fatal(err)}*/
reactors[i] = NewConsensusReactor(css[i], true) // so we dont start the consensus states
reactors[i] = NewConsensusReactor(css[i], true, NopMetrics()) // so we dont start the consensus states
reactors[i].SetLogger(css[i].Logger)

// eventBus is already started with the cs
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestReactorRecordsBlockParts(t *testing.T) {

// create reactor
css := randConsensusNet(1, "consensus_reactor_records_block_parts_test", newMockTickerFunc(true), newPersistentKVStore)
reactor := NewConsensusReactor(css[0], false) // so we dont start the consensus states
reactor := NewConsensusReactor(css[0], false, NopMetrics()) // so we dont start the consensus states
reactor.SetEventBus(css[0].eventBus)
reactor.SetLogger(log.TestingLogger())
sw := p2p.MakeSwitch(cfg.DefaultP2PConfig(), 1, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch { return sw })
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestReactorRecordsVotes(t *testing.T) {

// Create reactor.
css := randConsensusNet(1, "consensus_reactor_records_votes_test", newMockTickerFunc(true), newPersistentKVStore)
reactor := NewConsensusReactor(css[0], false) // so we dont start the consensus states
reactor := NewConsensusReactor(css[0], false, NopMetrics()) // so we dont start the consensus states
reactor.SetEventBus(css[0].eventBus)
reactor.SetLogger(log.TestingLogger())
sw := p2p.MakeSwitch(cfg.DefaultP2PConfig(), 1, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch { return sw })
Expand Down
2 changes: 2 additions & 0 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,8 @@ func (cs *ConsensusState) recordMetrics(height int64, block *types.Block) {
cs.metrics.NumTxs.Set(float64(block.NumTxs))
cs.metrics.BlockSizeBytes.Set(float64(block.Size()))
cs.metrics.TotalTxs.Set(float64(block.TotalTxs))
cs.metrics.LatestBlockHeight.Set(float64(block.Height))

}

//-----------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion mempool/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/discard"

prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/config"
)

// Metrics contains metrics exposed by this package.
Expand All @@ -19,6 +20,7 @@ type Metrics struct {
func PrometheusMetrics() *Metrics {
return &Metrics{
Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: config.MetricsNamespace,
Subsystem: "mempool",
Name: "size",
Help: "Size of the mempool (number of uncommitted transactions).",
Expand Down
7 changes: 4 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ func NewNode(config *cfg.Config,
bcReactor := bc.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
bcReactor.SetLogger(logger.With("module", "blockchain"))

csm := cs.WithMetrics(csMetrics)

// Make ConsensusReactor
consensusState := cs.NewConsensusState(
config.Consensus,
Expand All @@ -287,13 +289,13 @@ func NewNode(config *cfg.Config,
blockStore,
mempool,
evidencePool,
cs.WithMetrics(csMetrics),
csm,
)
consensusState.SetLogger(consensusLogger)
if privValidator != nil {
consensusState.SetPrivValidator(privValidator)
}
consensusReactor := cs.NewConsensusReactor(consensusState, fastSync)
consensusReactor := cs.NewConsensusReactor(consensusState, fastSync, csMetrics)
consensusReactor.SetLogger(consensusLogger)

p2pLogger := logger.With("module", "p2p")
Expand Down Expand Up @@ -751,7 +753,6 @@ func saveGenesisDoc(db dbm.DB, genDoc *types.GenesisDoc) {
db.SetSync(genesisDocKey, bytes)
}


// splitAndTrimEmpty slices s into all subslices separated by sep and returns a
// slice of the string s with all leading and trailing Unicode code points
// contained in cutset removed. If sep is empty, SplitAndTrim splits after each
Expand Down

0 comments on commit 9936e82

Please sign in to comment.