From 97a110a7f8b26942cf8764fbdd66849f3ff70265 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Sat, 7 Dec 2024 14:38:46 +0000 Subject: [PATCH] feat: add metric for the latest block from the chain (tip) --- src/services/chain.ts | 10 ++++++++++ src/utils/metrics.ts | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/services/chain.ts b/src/services/chain.ts index d030e4f..940f18e 100644 --- a/src/services/chain.ts +++ b/src/services/chain.ts @@ -189,7 +189,11 @@ export class ChainContext { let fromBlock = lastProcessedBlock ? lastProcessedBlock.number + 1 : this.deploymentBlock; + let currentBlock = await provider.getBlock("latest"); + metrics.blockHeightLatest + .labels(chainId.toString()) + .set(currentBlock.number); let printSyncInfo = true; // Print sync info only once let toBlock: "latest" | number = 0; @@ -199,6 +203,9 @@ export class ChainContext { if (typeof toBlock === "number" && toBlock > currentBlock.number) { // refresh the current block currentBlock = await provider.getBlock("latest"); + metrics.blockHeightLatest + .labels(chainId.toString()) + .set(currentBlock.number); toBlock = toBlock > currentBlock.number ? currentBlock.number : toBlock; @@ -283,6 +290,9 @@ export class ChainContext { // It may have taken some time to process the blocks, so refresh the current block number // and check if we are in sync currentBlock = await provider.getBlock("latest"); + metrics.blockHeightLatest + .labels(chainId.toString()) + .set(currentBlock.number); // If we are in sync, let it be known const lastProcessedBlockNumber = lastProcessedBlock?.number || 0; diff --git a/src/utils/metrics.ts b/src/utils/metrics.ts index d2bfdd1..79b70f3 100644 --- a/src/utils/metrics.ts +++ b/src/utils/metrics.ts @@ -57,6 +57,12 @@ export const blockHeight = new client.Gauge({ labelNames: ["chain_id"], }); +export const blockHeightLatest = new client.Gauge({ + name: "watch_tower_block_height_latest", + help: "Block height of the last block (tip of the chain)", + labelNames: ["chain_id"], +}); + export const reorgDepth = new client.Gauge({ name: "watch_tower_latest_reorg_depth", help: "Depth of the most recent reorg",