Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metric for the latest block from the chain (tip) #172

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines 193 to +196
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: since this is repeated 3 times, would it be worth it to wrap around a fn? Just so we don't forget to track the metrics if we want to use this elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i saw it only after merging. The whole code needs some serious refactoring. Lets clean it up all in the future 🙏


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
Loading