Skip to content

Commit 3d3e83e

Browse files
authored
Merge pull request #19692 from karalabe/metrics-extensions
core, ethdb, metrics, p2p: expose various counter metrics for grafana
2 parents f9c0e09 + b02958b commit 3d3e83e

15 files changed

Lines changed: 341 additions & 117 deletions

File tree

‎core/blockchain.go‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ import (
4646
)
4747

4848
var (
49+
headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil)
50+
headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil)
51+
headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil)
52+
4953
accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil)
5054
accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil)
5155
accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil)
@@ -332,6 +336,7 @@ func (bc *BlockChain) loadLastState() error {
332336
}
333337
// Everything seems to be fine, set as the head block
334338
bc.currentBlock.Store(currentBlock)
339+
headBlockGauge.Update(int64(currentBlock.NumberU64()))
335340

336341
// Restore the last known head header
337342
currentHeader := currentBlock.Header()
@@ -344,12 +349,14 @@ func (bc *BlockChain) loadLastState() error {
344349

345350
// Restore the last known head fast block
346351
bc.currentFastBlock.Store(currentBlock)
352+
headFastBlockGauge.Update(int64(currentBlock.NumberU64()))
353+
347354
if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) {
348355
if block := bc.GetBlockByHash(head); block != nil {
349356
bc.currentFastBlock.Store(block)
357+
headFastBlockGauge.Update(int64(block.NumberU64()))
350358
}
351359
}
352-
353360
// Issue a status log for the user
354361
currentFastBlock := bc.CurrentFastBlock()
355362

@@ -388,6 +395,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
388395
}
389396
rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash())
390397
bc.currentBlock.Store(newHeadBlock)
398+
headBlockGauge.Update(int64(newHeadBlock.NumberU64()))
391399
}
392400

393401
// Rewind the fast block in a simpleton way to the target head
@@ -399,6 +407,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
399407
}
400408
rawdb.WriteHeadFastBlockHash(db, newHeadFastBlock.Hash())
401409
bc.currentFastBlock.Store(newHeadFastBlock)
410+
headFastBlockGauge.Update(int64(newHeadFastBlock.NumberU64()))
402411
}
403412
}
404413

@@ -450,6 +459,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
450459
// If all checks out, manually set the head block
451460
bc.chainmu.Lock()
452461
bc.currentBlock.Store(block)
462+
headBlockGauge.Update(int64(block.NumberU64()))
453463
bc.chainmu.Unlock()
454464

455465
log.Info("Committed new head block", "number", block.Number(), "hash", hash)
@@ -522,9 +532,12 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
522532
bc.genesisBlock = genesis
523533
bc.insert(bc.genesisBlock)
524534
bc.currentBlock.Store(bc.genesisBlock)
535+
headBlockGauge.Update(int64(bc.genesisBlock.NumberU64()))
536+
525537
bc.hc.SetGenesis(bc.genesisBlock.Header())
526538
bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
527539
bc.currentFastBlock.Store(bc.genesisBlock)
540+
headFastBlockGauge.Update(int64(bc.genesisBlock.NumberU64()))
528541

529542
return nil
530543
}
@@ -598,13 +611,15 @@ func (bc *BlockChain) insert(block *types.Block) {
598611
rawdb.WriteHeadBlockHash(bc.db, block.Hash())
599612

600613
bc.currentBlock.Store(block)
614+
headBlockGauge.Update(int64(block.NumberU64()))
601615

602616
// If the block is better than our head or is on a different chain, force update heads
603617
if updateHeads {
604618
bc.hc.SetCurrentHeader(block.Header())
605619
rawdb.WriteHeadFastBlockHash(bc.db, block.Hash())
606620

607621
bc.currentFastBlock.Store(block)
622+
headFastBlockGauge.Update(int64(block.NumberU64()))
608623
}
609624
}
610625

@@ -862,11 +877,13 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
862877
newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1)
863878
rawdb.WriteHeadFastBlockHash(bc.db, newFastBlock.Hash())
864879
bc.currentFastBlock.Store(newFastBlock)
880+
headFastBlockGauge.Update(int64(newFastBlock.NumberU64()))
865881
}
866882
if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash {
867883
newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1)
868884
rawdb.WriteHeadBlockHash(bc.db, newBlock.Hash())
869885
bc.currentBlock.Store(newBlock)
886+
headBlockGauge.Update(int64(newBlock.NumberU64()))
870887
}
871888
}
872889
// Truncate ancient data which exceeds the current header.
@@ -952,6 +969,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
952969
if bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()).Cmp(td) < 0 {
953970
rawdb.WriteHeadFastBlockHash(bc.db, head.Hash())
954971
bc.currentFastBlock.Store(head)
972+
headFastBlockGauge.Update(int64(head.NumberU64()))
955973
isCanonical = true
956974
}
957975
}

‎core/headerchain.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c
104104
}
105105
}
106106
hc.currentHeaderHash = hc.CurrentHeader().Hash()
107+
headHeaderGauge.Update(hc.CurrentHeader().Number.Int64())
107108

108109
return hc, nil
109110
}
@@ -185,12 +186,12 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
185186

186187
hc.currentHeaderHash = hash
187188
hc.currentHeader.Store(types.CopyHeader(header))
189+
headHeaderGauge.Update(header.Number.Int64())
188190

189191
status = CanonStatTy
190192
} else {
191193
status = SideStatTy
192194
}
193-
194195
hc.headerCache.Add(hash, header)
195196
hc.numberCache.Add(hash, number)
196197

@@ -456,6 +457,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
456457

457458
hc.currentHeader.Store(head)
458459
hc.currentHeaderHash = head.Hash()
460+
headHeaderGauge.Update(head.Number.Int64())
459461
}
460462

461463
type (
@@ -508,6 +510,7 @@ func (hc *HeaderChain) SetHead(head uint64, updateFn UpdateHeadBlocksCallback, d
508510

509511
hc.currentHeader.Store(parent)
510512
hc.currentHeaderHash = parentHash
513+
headHeaderGauge.Update(parent.Number.Int64())
511514
}
512515
batch.Write()
513516

‎core/rawdb/freezer.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ type freezer struct {
8080
func newFreezer(datadir string, namespace string) (*freezer, error) {
8181
// Create the initial freezer object
8282
var (
83-
readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
84-
writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
83+
readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
84+
writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
85+
sizeCounter = metrics.NewRegisteredCounter(namespace+"ancient/size", nil)
8586
)
8687
// Ensure the datadir is not a symbolic link if it exists.
8788
if info, err := os.Lstat(datadir); !os.IsNotExist(err) {
@@ -102,7 +103,7 @@ func newFreezer(datadir string, namespace string) (*freezer, error) {
102103
instanceLock: lock,
103104
}
104105
for name, disableSnappy := range freezerNoSnappy {
105-
table, err := newTable(datadir, name, readMeter, writeMeter, disableSnappy)
106+
table, err := newTable(datadir, name, readMeter, writeMeter, sizeCounter, disableSnappy)
106107
if err != nil {
107108
for _, table := range freezer.tables {
108109
table.Close()

‎core/rawdb/freezer_table.go‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,18 @@ type freezerTable struct {
9494
// to count how many historic items have gone missing.
9595
itemOffset uint32 // Offset (number of discarded items)
9696

97-
headBytes uint32 // Number of bytes written to the head file
98-
readMeter metrics.Meter // Meter for measuring the effective amount of data read
99-
writeMeter metrics.Meter // Meter for measuring the effective amount of data written
97+
headBytes uint32 // Number of bytes written to the head file
98+
readMeter metrics.Meter // Meter for measuring the effective amount of data read
99+
writeMeter metrics.Meter // Meter for measuring the effective amount of data written
100+
sizeCounter metrics.Counter // Counter for tracking the combined size of all freezer tables
100101

101102
logger log.Logger // Logger with database path and table name ambedded
102103
lock sync.RWMutex // Mutex protecting the data file descriptors
103104
}
104105

105106
// newTable opens a freezer table with default settings - 2G files
106-
func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, disableSnappy bool) (*freezerTable, error) {
107-
return newCustomTable(path, name, readMeter, writeMeter, 2*1000*1000*1000, disableSnappy)
107+
func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, disableSnappy bool) (*freezerTable, error) {
108+
return newCustomTable(path, name, readMeter, writeMeter, sizeCounter, 2*1000*1000*1000, disableSnappy)
108109
}
109110

110111
// openFreezerFileForAppend opens a freezer table file and seeks to the end
@@ -148,7 +149,7 @@ func truncateFreezerFile(file *os.File, size int64) error {
148149
// newCustomTable opens a freezer table, creating the data and index files if they are
149150
// non existent. Both files are truncated to the shortest common length to ensure
150151
// they don't go out of sync.
151-
func newCustomTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, maxFilesize uint32, noCompression bool) (*freezerTable, error) {
152+
func newCustomTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, maxFilesize uint32, noCompression bool) (*freezerTable, error) {
152153
// Ensure the containing directory exists and open the indexEntry file
153154
if err := os.MkdirAll(path, 0755); err != nil {
154155
return nil, err
@@ -171,6 +172,7 @@ func newCustomTable(path string, name string, readMeter metrics.Meter, writeMete
171172
files: make(map[uint32]*os.File),
172173
readMeter: readMeter,
173174
writeMeter: writeMeter,
175+
sizeCounter: sizeCounter,
174176
name: name,
175177
path: path,
176178
logger: log.New("database", path, "table", name),
@@ -181,6 +183,14 @@ func newCustomTable(path string, name string, readMeter metrics.Meter, writeMete
181183
tab.Close()
182184
return nil, err
183185
}
186+
// Initialize the starting size counter
187+
size, err := tab.sizeNolock()
188+
if err != nil {
189+
tab.Close()
190+
return nil, err
191+
}
192+
tab.sizeCounter.Inc(int64(size))
193+
184194
return tab, nil
185195
}
186196

@@ -321,6 +331,11 @@ func (t *freezerTable) truncate(items uint64) error {
321331
if atomic.LoadUint64(&t.items) <= items {
322332
return nil
323333
}
334+
// We need to truncate, save the old size for metrics tracking
335+
oldSize, err := t.sizeNolock()
336+
if err != nil {
337+
return err
338+
}
324339
// Something's out of sync, truncate the table's offset index
325340
t.logger.Warn("Truncating freezer table", "items", t.items, "limit", items)
326341
if err := truncateFreezerFile(t.index, int64(items+1)*indexEntrySize); err != nil {
@@ -355,6 +370,14 @@ func (t *freezerTable) truncate(items uint64) error {
355370
// All data files truncated, set internal counters and return
356371
atomic.StoreUint64(&t.items, items)
357372
atomic.StoreUint32(&t.headBytes, expected.offset)
373+
374+
// Retrieve the new size and update the total size counter
375+
newSize, err := t.sizeNolock()
376+
if err != nil {
377+
return err
378+
}
379+
t.sizeCounter.Dec(int64(oldSize - newSize))
380+
358381
return nil
359382
}
360383

@@ -483,7 +506,10 @@ func (t *freezerTable) Append(item uint64, blob []byte) error {
483506
}
484507
// Write indexEntry
485508
t.index.Write(idx.marshallBinary())
509+
486510
t.writeMeter.Mark(int64(bLen + indexEntrySize))
511+
t.sizeCounter.Inc(int64(bLen + indexEntrySize))
512+
487513
atomic.AddUint64(&t.items, 1)
488514
return nil
489515
}
@@ -562,6 +588,12 @@ func (t *freezerTable) size() (uint64, error) {
562588
t.lock.RLock()
563589
defer t.lock.RUnlock()
564590

591+
return t.sizeNolock()
592+
}
593+
594+
// sizeNolock returns the total data size in the freezer table without obtaining
595+
// the mutex first.
596+
func (t *freezerTable) sizeNolock() (uint64, error) {
565597
stat, err := t.index.Stat()
566598
if err != nil {
567599
return 0, err

0 commit comments

Comments
 (0)