Skip to content

Commit 7979fb7

Browse files
committed
Add some useful Eth1 monitor metrics
1 parent 983b3c9 commit 7979fb7

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

beacon_chain/eth1_monitor.nim

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import
2-
std/[deques, tables, hashes, options, strformat, strutils, sequtils, uri],
3-
chronos, web3, web3/ethtypes as web3Types, json, chronicles/timings,
4-
eth/common/eth_types, eth/async_utils,
2+
std/[deques, hashes, options, strformat, strutils, sequtils, tables,
3+
typetraits, uri],
4+
# Nimble packages:
5+
chronos, json, metrics, chronicles/timings,
6+
web3, web3/ethtypes as web3Types, eth/common/eth_types, eth/async_utils,
7+
# Local modules:
58
spec/[datatypes, digest, crypto, helpers],
69
ssz, beacon_chain_db, network_metadata, merkle_minimal, beacon_node_status
710

@@ -100,6 +103,24 @@ type
100103
deposits*: seq[Deposit]
101104
hasMissingDeposits*: bool
102105

106+
declareCounter failed_web3_requests,
107+
"Failed web3 requests"
108+
109+
declareGauge eth1_latest_head,
110+
"The highest Eth1 block number observed on the network"
111+
112+
declareGauge eth1_synced_head,
113+
"Block number of the highest synchronized block according to follow distance"
114+
115+
declareGauge eth1_finalized_head,
116+
"Block number of the highest Eth1 block finalized by Eth2 consensus"
117+
118+
declareGauge eth1_finalized_deposits,
119+
"Number of deposits that were finalized by the Eth2 consensus"
120+
121+
declareGauge eth1_chain_len,
122+
"The length of the in-memory chain of Eth1 blocks"
123+
103124
template depositContractAddress*(m: Eth1Monitor): Eth1Address =
104125
m.dataProvider.ns.contractAddress
105126

@@ -147,6 +168,15 @@ proc fixupWeb3Urls*(web3Url: var string) =
147168
web3Url = "ws://" & normalizedUrl.substr(pos)
148169
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url
149170

171+
func toGaugeValue(x: uint64): int64 =
172+
if x > uint64(int64.high):
173+
int64.high
174+
else:
175+
int64(x)
176+
177+
template toGaugeValue(x: Quantity): int64 =
178+
toGaugeValue(distinctBase x)
179+
150180
# TODO: Add preset validation
151181
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
152182
# doAssert SECONDS_PER_ETH1_BLOCK * preset.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
@@ -200,13 +230,15 @@ func latestCandidateBlock(m: Eth1Monitor, periodStart: uint64): Eth1Block =
200230
if is_candidate_block(m.preset, blk, periodStart):
201231
return blk
202232

203-
func popFirst(eth1Chain: var Eth1Chain) =
233+
proc popFirst(eth1Chain: var Eth1Chain) =
204234
let removed = eth1Chain.blocks.popFirst
205235
eth1Chain.blocksByHash.del removed.voteData.block_hash.asBlockHash
236+
eth1_chain_len.set eth1Chain.blocks.len.int64
206237

207-
func addBlock(eth1Chain: var Eth1Chain, newBlock: Eth1Block) =
238+
proc addBlock(eth1Chain: var Eth1Chain, newBlock: Eth1Block) =
208239
eth1Chain.blocks.addLast newBlock
209240
eth1Chain.blocksByHash[newBlock.voteData.block_hash.asBlockHash] = newBlock
241+
eth1_chain_len.set eth1Chain.blocks.len.int64
210242

211243
func hash*(x: Eth1Data): Hash =
212244
hashData(unsafeAddr x, sizeof(x))
@@ -235,6 +267,7 @@ template awaitWithRetries[T](lazyFutExpr: Future[T],
235267
raise f.error
236268
else:
237269
debug "Web3 request failed", req = reqType, err = f.error.msg
270+
inc failed_web3_requests
238271
else:
239272
break
240273

@@ -425,6 +458,9 @@ proc pruneOldBlocks(m: Eth1Monitor, depositIndex: uint64) =
425458
eth1Block: lastBlock.voteData.block_hash,
426459
depositContractState: m.eth2FinalizedDepositsMerkleizer.toDepositContractState)
427460

461+
eth1_finalized_head.set lastBlock.number.toGaugeValue
462+
eth1_finalized_deposits.set lastBlock.voteData.deposit_count.toGaugeValue
463+
428464
debug "Eth1 blocks pruned",
429465
newTailBlock = lastBlock.voteData.block_hash,
430466
depositsCount = lastBlock.voteData.deposit_count
@@ -710,6 +746,7 @@ proc syncBlockRange(m: Eth1Monitor,
710746
"Request time out while obtaining json logs")
711747
except CatchableError as err:
712748
debug "Request for deposit logs failed", err = err.msg
749+
inc failed_web3_requests
713750
backoff = (backoff * 3) div 2
714751
retryOrRaise err
715752

@@ -736,8 +773,10 @@ proc syncBlockRange(m: Eth1Monitor,
736773

737774
m.eth1Chain.addBlock(
738775
lastBlock.makeSuccessorWithoutDeposits(blockWithoutDeposits))
776+
eth1_synced_head.set blockWithoutDeposits.number.toGaugeValue
739777

740778
m.eth1Chain.addBlock blk
779+
eth1_synced_head.set blk.number.toGaugeValue
741780

742781
if blocksWithDeposits.len > 0:
743782
let lastIdx = blocksWithDeposits.len - 1
@@ -857,6 +896,11 @@ proc startEth1Syncing(m: Eth1Monitor) {.async.} =
857896
m.eth2FinalizedDepositsMerkleizer))
858897

859898
var eth1SyncedTo = Eth1BlockNumber startBlock.number
899+
eth1_synced_head.set eth1SyncedTo.toGaugeValue
900+
eth1_finalized_head.set eth1SyncedTo.toGaugeValue
901+
eth1_finalized_deposits.set(
902+
m.eth2FinalizedDepositsMerkleizer.getChunkCount.toGaugeValue)
903+
860904
var scratchMerkleizer = newClone(copy m.eth2FinalizedDepositsMerkleizer)
861905

862906
debug "Starting Eth1 syncing", `from` = shortLog(m.eth1Chain.blocks[0])
@@ -886,6 +930,7 @@ proc startEth1Syncing(m: Eth1Monitor) {.async.} =
886930
targetBlock,
887931
earliestBlockOfInterest)
888932
eth1SyncedTo = targetBlock
933+
eth1_synced_head.set eth1SyncedTo.toGaugeValue
889934

890935
proc run(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
891936
if delayBeforeStart != ZeroDuration:
@@ -898,7 +943,8 @@ proc run(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
898943
{.raises: [Defect], gcsafe.}:
899944
try:
900945
if blk.number.uint64 > m.latestEth1BlockNumber:
901-
m.latestEth1BlockNumber = blk.number.uint64
946+
eth1_latest_head.set blk.number.toGaugeValue
947+
m.latestEth1BlockNumber = Eth1BlockNumber blk.number
902948
m.eth1Progress.fire()
903949
except Exception:
904950
# TODO Investigate why this exception is being raised

0 commit comments

Comments
 (0)