Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tracks block time using a Prometheus gauge metric type #1091

Merged
merged 11 commits into from
Sep 26, 2023
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.",
}, append(labels, "block_height")).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
9 changes: 6 additions & 3 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1830,9 +1830,11 @@ 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.With("block_height",
fmt.Sprintf("%d", block.Height)).Set(elapsedTime)
staheri14 marked this conversation as resolved.
Show resolved Hide resolved

}
}

Expand All @@ -1844,6 +1846,7 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
cs.metrics.NumTxs.Set(float64(len(block.Data.Txs)))
cs.metrics.TotalTxs.Add(float64(len(block.Data.Txs)))
cs.metrics.BlockSizeBytes.Set(float64(blockSize))
// cs.metrics.BlockTimeSeconds
staheri14 marked this conversation as resolved.
Show resolved Hide resolved
cs.metrics.CommittedHeight.Set(float64(block.Height))
}

Expand Down
11 changes: 7 additions & 4 deletions test/maverick/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"reflect"
"runtime/debug"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -1668,9 +1669,11 @@ 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.With("block_height",
strconv.FormatInt(block.Height, 10)).Set(elapsedTime)

}
}

Expand Down Expand Up @@ -1892,7 +1895,7 @@ func (cs *State) signAddVote(msgType cmtproto.SignedMsgType, hash []byte, header
}
// if !cs.replayMode {
cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
//}
// }
return nil
}

Expand Down
Loading