From e2b318726c064bf607a48c0fcc245e84faa0c633 Mon Sep 17 00:00:00 2001 From: Sanaz Taheri <35961250+staheri14@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:37:45 -0600 Subject: [PATCH] feat: tracks block time using a Prometheus gauge metric type (#1091) Closes https://github.com/celestiaorg/celestia-core/issues/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 ``` --- consensus/metrics.go | 13 +++++++++++-- consensus/state.go | 7 ++++--- test/maverick/consensus/state.go | 7 ++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/consensus/metrics.go b/consensus/metrics.go index dcaaae9399..c684c339f6 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -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 @@ -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. @@ -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, @@ -264,6 +272,7 @@ func NopMetrics() *Metrics { ByzantineValidatorsPower: discard.NewGauge(), BlockIntervalSeconds: discard.NewHistogram(), + BlockTimeSeconds: discard.NewGauge(), NumTxs: discard.NewGauge(), BlockSizeBytes: discard.NewGauge(), diff --git a/consensus/state.go b/consensus/state.go index bbbb1d3d44..398abc0856 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -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) + } } diff --git a/test/maverick/consensus/state.go b/test/maverick/consensus/state.go index f73e4a14c6..ae5afad0ae 100644 --- a/test/maverick/consensus/state.go +++ b/test/maverick/consensus/state.go @@ -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) + } }