From e992e1b2648de61650b4df7c2249a5ea5e20d957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hern=C3=A1n=20Vanzetto?= <15466498+hvanz@users.noreply.github.com> Date: Wed, 22 Mar 2023 12:59:02 +0100 Subject: [PATCH] Add metrics to BlockSync (#538) * Add 4 metrics related to block * Remove unused function * Unregister metrics when BlockSync finishes * Add changelog * Rename method * Move method * Do not export unregister method * Do not unregister metrics --- .../improvements/543-metrics-for-blocksync.md | 2 + blocksync/metrics.gen.go | 58 +++++++++++++++++++ blocksync/metrics.go | 35 +++++++++++ blocksync/reactor.go | 2 + consensus/metrics.go | 9 --- 5 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 .changelog/unreleased/improvements/543-metrics-for-blocksync.md create mode 100644 blocksync/metrics.gen.go create mode 100644 blocksync/metrics.go diff --git a/.changelog/unreleased/improvements/543-metrics-for-blocksync.md b/.changelog/unreleased/improvements/543-metrics-for-blocksync.md new file mode 100644 index 0000000000..824e20df9c --- /dev/null +++ b/.changelog/unreleased/improvements/543-metrics-for-blocksync.md @@ -0,0 +1,2 @@ +- `[blocksync]` Generate new metrics during BlockSync + ([\#543](https://github.com/cometbft/cometbft/pull/543)) \ No newline at end of file diff --git a/blocksync/metrics.gen.go b/blocksync/metrics.gen.go new file mode 100644 index 0000000000..46c24df208 --- /dev/null +++ b/blocksync/metrics.gen.go @@ -0,0 +1,58 @@ +// Code generated by metricsgen. DO NOT EDIT. + +package blocksync + +import ( + "github.com/go-kit/kit/metrics/discard" + prometheus "github.com/go-kit/kit/metrics/prometheus" + stdprometheus "github.com/prometheus/client_golang/prometheus" +) + +func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { + labels := []string{} + for i := 0; i < len(labelsAndValues); i += 2 { + labels = append(labels, labelsAndValues[i]) + } + return &Metrics{ + Syncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "syncing", + Help: "Whether or not a node is block syncing. 1 if yes, 0 if no.", + }, labels).With(labelsAndValues...), + NumTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "num_txs", + Help: "Number of transactions in the latest block.", + }, labels).With(labelsAndValues...), + TotalTxs: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "total_txs", + Help: "Total number of transactions.", + }, labels).With(labelsAndValues...), + BlockSizeBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "block_size_bytes", + Help: "Size of the latest block.", + }, labels).With(labelsAndValues...), + LatestBlockHeight: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "latest_block_height", + Help: "The height of the latest block.", + }, labels).With(labelsAndValues...), + } +} + +func NopMetrics() *Metrics { + return &Metrics{ + Syncing: discard.NewGauge(), + NumTxs: discard.NewGauge(), + TotalTxs: discard.NewGauge(), + BlockSizeBytes: discard.NewGauge(), + LatestBlockHeight: discard.NewGauge(), + } +} diff --git a/blocksync/metrics.go b/blocksync/metrics.go new file mode 100644 index 0000000000..f120b4f7c4 --- /dev/null +++ b/blocksync/metrics.go @@ -0,0 +1,35 @@ +package blocksync + +import ( + "github.com/cometbft/cometbft/types" + "github.com/go-kit/kit/metrics" +) + +const ( + // MetricsSubsystem is a subsystem shared by all metrics exposed by this + // package. + MetricsSubsystem = "blocksync" +) + +//go:generate go run ../scripts/metricsgen -struct=Metrics + +// Metrics contains metrics exposed by this package. +type Metrics struct { + // Whether or not a node is block syncing. 1 if yes, 0 if no. + Syncing metrics.Gauge + // Number of transactions in the latest block. + NumTxs metrics.Gauge + // Total number of transactions. + TotalTxs metrics.Gauge + // Size of the latest block. + BlockSizeBytes metrics.Gauge + // The height of the latest block. + LatestBlockHeight metrics.Gauge +} + +func (m *Metrics) recordBlockMetrics(block *types.Block) { + m.NumTxs.Set(float64(len(block.Data.Txs))) + m.TotalTxs.Add(float64(len(block.Data.Txs))) + m.BlockSizeBytes.Set(float64(block.Size())) + m.LatestBlockHeight.Set(float64(block.Height)) +} diff --git a/blocksync/reactor.go b/blocksync/reactor.go index 629fbbedc4..d7644f4c26 100644 --- a/blocksync/reactor.go +++ b/blocksync/reactor.go @@ -60,6 +60,7 @@ type Reactor struct { requestsCh <-chan BlockRequest errorsCh <-chan peerError appHashErrorsCh chan p2p.AppHashError + metrics *Metrics } func NewReactorWithOfflineStateSync(state sm.State, blockExec *sm.BlockExecutor, store *store.BlockStore, @@ -425,6 +426,7 @@ FOR_LOOP: // TODO This is bad, are we zombie? panic(fmt.Sprintf("Failed to process committed block (%d:%X): %v", first.Height, first.Hash(), err)) } + bcR.metrics.recordBlockMetrics(first) blocksSynced++ if blocksSynced%100 == 0 { diff --git a/consensus/metrics.go b/consensus/metrics.go index e9f81940a5..513a05bd52 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -8,7 +8,6 @@ import ( cstypes "github.com/cometbft/cometbft/consensus/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cometbft/cometbft/types" ) const ( @@ -116,14 +115,6 @@ type Metrics struct { LateVotes metrics.Counter `metrics_labels:"vote_type"` } -// RecordConsMetrics uses for recording the block related metrics during fast-sync. -func (m *Metrics) RecordConsMetrics(block *types.Block) { - m.NumTxs.Set(float64(len(block.Data.Txs))) - m.TotalTxs.Add(float64(len(block.Data.Txs))) - m.BlockSizeBytes.Set(float64(block.Size())) - m.CommittedHeight.Set(float64(block.Height)) -} - func (m *Metrics) MarkProposalProcessed(accepted bool) { status := "accepted" if !accepted {