Skip to content

Commit

Permalink
feat: tracks block time using a Prometheus gauge metric type (#1091)
Browse files Browse the repository at this point in the history
Closes #1077

I have verified the outcome of this pull request, and the block time
gauge now correctly appears in the Prometheus endpoint:

```
# HELP cometbft_consensus_block_time_seconds Duration between this block and the preceding one.
# TYPE cometbft_consensus_block_time_seconds gauge
cometbft_consensus_block_time_seconds{chain_id="mocha-4",version="1.0.0-rc16"} 11.623671263
```
  • Loading branch information
staheri14 authored Sep 26, 2023
1 parent 2ed2933 commit e2b3187
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
13 changes: 11 additions & 2 deletions consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Metrics struct {

// Time between this and the last block.
BlockIntervalSeconds metrics.Histogram
// Block time in seconds.
BlockTimeSeconds metrics.Gauge

// Number of transactions.
NumTxs metrics.Gauge
Expand All @@ -57,9 +59,9 @@ type Metrics struct {
TotalTxs metrics.Gauge
// The latest block height.
CommittedHeight metrics.Gauge
// Whether or not a node is fast syncing. 1 if yes, 0 if no.
// Whether a node is fast syncing. 1 if yes, 0 if no.
FastSyncing metrics.Gauge
// Whether or not a node is state syncing. 1 if yes, 0 if no.
// Whether a node is state syncing. 1 if yes, 0 if no.
StateSyncing metrics.Gauge

// Number of blockparts transmitted by peer.
Expand Down Expand Up @@ -171,6 +173,12 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics {
Name: "block_interval_seconds",
Help: "Time between this and the last block.",
}, labels).With(labelsAndValues...),
BlockTimeSeconds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: namespace,
Subsystem: MetricsSubsystem,
Name: "block_time_seconds",
Help: "Duration between this block and the preceding one.",
}, labels).With(labelsAndValues...),
NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: namespace,
Subsystem: MetricsSubsystem,
Expand Down Expand Up @@ -264,6 +272,7 @@ func NopMetrics() *Metrics {
ByzantineValidatorsPower: discard.NewGauge(),

BlockIntervalSeconds: discard.NewHistogram(),
BlockTimeSeconds: discard.NewGauge(),

NumTxs: discard.NewGauge(),
BlockSizeBytes: discard.NewGauge(),
Expand Down
7 changes: 4 additions & 3 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1830,9 +1830,10 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
if height > 1 {
lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
if lastBlockMeta != nil {
cs.metrics.BlockIntervalSeconds.Observe(
block.Time.Sub(lastBlockMeta.Header.Time).Seconds(),
)
elapsedTime := block.Time.Sub(lastBlockMeta.Header.Time).Seconds()
cs.metrics.BlockIntervalSeconds.Observe(elapsedTime)
cs.metrics.BlockTimeSeconds.Set(elapsedTime)

}
}

Expand Down
7 changes: 4 additions & 3 deletions test/maverick/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,9 +1668,10 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
if height > 1 {
lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1)
if lastBlockMeta != nil {
cs.metrics.BlockIntervalSeconds.Observe(
block.Time.Sub(lastBlockMeta.Header.Time).Seconds(),
)
elapsedTime := block.Time.Sub(lastBlockMeta.Header.Time).Seconds()
cs.metrics.BlockIntervalSeconds.Observe(elapsedTime)
cs.metrics.BlockTimeSeconds.Set(elapsedTime)

}
}

Expand Down

0 comments on commit e2b3187

Please sign in to comment.