Skip to content

Commit

Permalink
Add metrics to BlockSync (cometbft#538)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hvanz authored and faddat committed Feb 25, 2024
1 parent 6e2a1c3 commit e992e1b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[blocksync]` Generate new metrics during BlockSync
([\#543](https://github.com/cometbft/cometbft/pull/543))
58 changes: 58 additions & 0 deletions blocksync/metrics.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions blocksync/metrics.go
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 2 additions & 0 deletions blocksync/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e992e1b

Please sign in to comment.