Skip to content

Commit

Permalink
feat: add metric for the latest block from the chain (tip)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Dec 7, 2024
1 parent 03bf9fd commit 97a110a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 97a110a

Please sign in to comment.