Skip to content

Commit 73b77f2

Browse files
authored
Add more metrics (#4645)
1 parent 3ed4cce commit 73b77f2

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

blockchain/blockchain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ func (bc *blockchain) commitBlock(blk *block.Block) error {
562562
}
563563
_blockMtc.WithLabelValues("excessBlobGas").Set(float64(blk.ExcessBlobGas()))
564564
_blockMtc.WithLabelValues("blobGasUsed").Set(float64(blk.BlobGasUsed()))
565+
_blockMtc.WithLabelValues("gasUsed").Set(float64(blk.GasUsed()))
565566
// emit block to all block subscribers
566567
bc.emitToSubscribers(blk)
567568
return nil

consensus/scheme/rolldpos/proposalpool.go

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,54 @@ import (
66

77
"github.com/iotexproject/go-pkgs/hash"
88
"github.com/pkg/errors"
9+
"github.com/prometheus/client_golang/prometheus"
910
"go.uber.org/zap"
1011

1112
"github.com/iotexproject/iotex-core/v2/blockchain/block"
1213
"github.com/iotexproject/iotex-core/v2/pkg/log"
14+
"github.com/iotexproject/iotex-core/v2/pkg/prometheustimer"
1315
)
1416

17+
var (
18+
// _proposalPoolMtc tracks proposal pool operations
19+
_proposalPoolMtc = prometheus.NewCounterVec(prometheus.CounterOpts{
20+
Name: "iotex_consensus_proposalpool_operations_total",
21+
Help: "Total number of proposal pool operations",
22+
}, []string{"operation", "result"})
23+
24+
// _proposalPoolSizeGauge tracks the current size of the proposal pool
25+
_proposalPoolSizeGauge = prometheus.NewGauge(prometheus.GaugeOpts{
26+
Name: "iotex_consensus_proposalpool_size",
27+
Help: "Current number of blocks in the proposal pool",
28+
})
29+
30+
// _proposalPoolForksGauge tracks the current number of forks
31+
_proposalPoolForksGauge = prometheus.NewGauge(prometheus.GaugeOpts{
32+
Name: "iotex_consensus_proposalpool_forks",
33+
Help: "Current number of forks in the proposal pool",
34+
})
35+
36+
// _proposalPoolLatencyTimer tracks operation latency
37+
_proposalPoolLatencyTimer *prometheustimer.TimerFactory
38+
)
39+
40+
func init() {
41+
prometheus.MustRegister(_proposalPoolMtc)
42+
prometheus.MustRegister(_proposalPoolSizeGauge)
43+
prometheus.MustRegister(_proposalPoolForksGauge)
44+
45+
var err error
46+
_proposalPoolLatencyTimer, err = prometheustimer.New(
47+
"iotex_consensus_proposalpool_latency_nanoseconds",
48+
"Latency of proposal pool operations in nanoseconds",
49+
[]string{"operation"},
50+
[]string{"unknown"},
51+
)
52+
if err != nil {
53+
log.L().Error("Failed to create proposal pool latency timer", zap.Error(err))
54+
}
55+
}
56+
1557
// proposalPool is a pool of draft blocks
1658
type proposalPool struct {
1759
// nodes is a map of draft proposal blocks
@@ -27,43 +69,65 @@ type proposalPool struct {
2769
}
2870

2971
func newProposalPool() *proposalPool {
30-
return &proposalPool{
72+
pool := &proposalPool{
3173
nodes: make(map[hash.Hash256]*block.Block),
3274
leaves: make(map[hash.Hash256]time.Time),
3375
}
76+
// Initialize metrics
77+
_proposalPoolSizeGauge.Set(0)
78+
_proposalPoolForksGauge.Set(0)
79+
return pool
3480
}
3581

3682
func (d *proposalPool) Init(root hash.Hash256) {
3783
d.mu.Lock()
3884
defer d.mu.Unlock()
3985
d.root = root
86+
87+
// Update metrics after initialization
88+
d.updateMetrics()
89+
4090
log.L().Debug("proposal pool initialized", log.Hex("root", root[:]))
4191
}
4292

4393
// AddBlock adds a block to the draft pool
4494
func (d *proposalPool) AddBlock(blk *block.Block) error {
95+
timer := _proposalPoolLatencyTimer.NewTimer("add_block")
96+
defer timer.End()
97+
4598
d.mu.Lock()
4699
defer d.mu.Unlock()
100+
47101
// nothing to do if the block already exists
48102
hash := blk.HashBlock()
49103
if _, ok := d.nodes[hash]; ok {
104+
_proposalPoolMtc.WithLabelValues("add_block", "duplicate").Inc()
50105
return nil
51106
}
52107
// it must be a new tip of any fork, or make a new fork
53108
prevHash := blk.PrevHash()
54109
if _, ok := d.leaves[prevHash]; ok {
55110
delete(d.leaves, prevHash)
56111
} else if prevHash != d.root && d.nodes[prevHash] == nil {
112+
_proposalPoolMtc.WithLabelValues("add_block", "invalid_prev").Inc()
57113
return errors.Errorf("block %x is not a tip of any fork", prevHash[:])
58114
}
59115
d.leaves[hash] = blk.Timestamp()
60116
d.nodes[hash] = blk
117+
118+
// Update metrics after successful addition
119+
d.updateMetrics()
120+
_proposalPoolMtc.WithLabelValues("add_block", "success").Inc()
121+
61122
log.L().Debug("added block to draft pool", log.Hex("hash", hash[:]), zap.Uint64("height", blk.Height()), zap.Time("timestamp", blk.Timestamp()))
62123
return nil
63124
}
64125

65126
// ReceiveBlock a block has been confirmed and remove all the blocks that is previous to it, or other forks
66127
func (d *proposalPool) ReceiveBlock(blk *block.Block) error {
128+
timer := _proposalPoolLatencyTimer.NewTimer("receive_block")
129+
defer timer.End()
130+
67131
d.mu.Lock()
68132
defer d.mu.Unlock()
69133

@@ -102,12 +166,33 @@ func (d *proposalPool) ReceiveBlock(blk *block.Block) error {
102166
delete(d.leaves, f)
103167
}
104168
d.root = blk.HashBlock()
169+
170+
// Update metrics after successful operation
171+
d.updateMetrics()
172+
_proposalPoolMtc.WithLabelValues("receive_block", "success").Inc()
173+
105174
return nil
106175
}
107176

108177
// BlockByHash returns the block by hash
109178
func (d *proposalPool) BlockByHash(hash hash.Hash256) *block.Block {
179+
timer := _proposalPoolLatencyTimer.NewTimer("block_by_hash")
180+
defer timer.End()
181+
110182
d.mu.Lock()
111183
defer d.mu.Unlock()
112-
return d.nodes[hash]
184+
185+
block := d.nodes[hash]
186+
if block != nil {
187+
_proposalPoolMtc.WithLabelValues("block_by_hash", "hit").Inc()
188+
} else {
189+
_proposalPoolMtc.WithLabelValues("block_by_hash", "miss").Inc()
190+
}
191+
return block
192+
}
193+
194+
// updateMetrics updates the gauge metrics with current pool state
195+
func (d *proposalPool) updateMetrics() {
196+
_proposalPoolSizeGauge.Set(float64(len(d.nodes)))
197+
_proposalPoolForksGauge.Set(float64(len(d.leaves)))
113198
}

state/factory/minter.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ import (
1111
"time"
1212

1313
"github.com/iotexproject/go-pkgs/crypto"
14+
"github.com/prometheus/client_golang/prometheus"
1415

1516
"github.com/iotexproject/iotex-core/v2/action/protocol"
1617
"github.com/iotexproject/iotex-core/v2/actpool"
1718
"github.com/iotexproject/iotex-core/v2/blockchain/block"
1819
)
1920

21+
var (
22+
// Prometheus metrics for minter operations
23+
_minterDurationMtc = prometheus.NewHistogramVec(
24+
prometheus.HistogramOpts{
25+
Name: "iotex_minter_duration_seconds",
26+
Help: "Time spent in minter operations",
27+
Buckets: prometheus.DefBuckets,
28+
},
29+
[]string{"operation", "status"},
30+
)
31+
)
32+
33+
func init() {
34+
prometheus.MustRegister(_minterDurationMtc)
35+
}
36+
37+
// MintOption defines an option to configure Minter
2038
type MintOption func(*Minter)
2139

2240
// WithTimeoutOption sets the timeout for NewBlockBuilder
@@ -64,6 +82,8 @@ func (m *Minter) ReceiveBlock(blk *block.Block) error {
6482
}
6583

6684
func (m *Minter) mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block, error) {
85+
startTime := time.Now()
86+
6787
if m.timeout > 0 {
6888
// set deadline for NewBlockBuilder
6989
// ensure that minting finishes before `block start time + timeout` and that its duration does not exceed the timeout.
@@ -79,5 +99,13 @@ func (m *Minter) mint(ctx context.Context, pk crypto.PrivateKey) (*block.Block,
7999
ctx, cancel = context.WithDeadline(ctx, ddl)
80100
defer cancel()
81101
}
82-
return m.f.Mint(ctx, m.ap, pk)
102+
103+
blk, err := m.f.Mint(ctx, m.ap, pk)
104+
duration := time.Since(startTime).Seconds()
105+
if err != nil {
106+
_minterDurationMtc.WithLabelValues("mint_block", "failure").Observe(duration)
107+
return nil, err
108+
}
109+
_minterDurationMtc.WithLabelValues("mint_block", "success").Observe(duration)
110+
return blk, nil
83111
}

0 commit comments

Comments
 (0)