From a0320605b5043506be37c26227610cdb8a892087 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Sat, 7 Dec 2024 13:38:23 +0000 Subject: [PATCH] feat: add sync status --- src/services/chain.ts | 15 +++++++++++---- src/utils/metrics.ts | 14 +++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/services/chain.ts b/src/services/chain.ts index ace123e..d030e4f 100644 --- a/src/services/chain.ts +++ b/src/services/chain.ts @@ -78,7 +78,7 @@ export class ChainContext { readonly addresses?: string[]; readonly processEveryNumBlocks: number; - private sync: ChainSync = ChainSync.SYNCING; + private sync: ChainSync; static chains: Chains = {}; provider: providers.Provider; @@ -104,6 +104,8 @@ export class ChainContext { orderBookApi: orderBookApiUrl, filterPolicy, } = options; + + this.sync = ChainSync.SYNCING; this.deploymentBlock = deploymentBlock; this.pageSize = pageSize ?? PAGE_SIZE_DEFAULT; this.dryRun = dryRun; @@ -175,6 +177,9 @@ export class ChainContext { let { lastProcessedBlock } = this.registry; const { pageSize } = this; + // Set the sync status metric (Syncing) + metrics.syncStatus.labels(chainId.toString()).set(0); + // Set the block height metric metrics.blockHeight .labels(chainId.toString()) @@ -289,6 +294,7 @@ export class ChainContext { } } while (this.sync === ChainSync.SYNCING); + metrics.syncStatus.labels(chainId.toString()).set(1); log.info( `☀️ ${ oneShot ? "Chain watcher is in sync" : "Chain watcher is warmed up" @@ -524,7 +530,6 @@ async function persistLastProcessedBlock(params: { log: LoggerWithMethods; }) { const { context, block, log } = params; - const blockNumber = block.number; // Set the last processed block to the current block number context.registry.lastProcessedBlock = blockToRegistryBlock(block); @@ -533,8 +538,10 @@ async function persistLastProcessedBlock(params: { await context.registry.write(); log.debug(`Block has been processed`); - // Set the block height metric - metrics.blockHeight.labels(context.chainId.toString()).set(blockNumber); + // Set the block metrics + const chain = context.chainId.toString(); + metrics.blockTimestamp.labels(chain).set(block.timestamp); + metrics.blockHeight.labels(chain).set(block.number); return context.registry.lastProcessedBlock; } diff --git a/src/utils/metrics.ts b/src/utils/metrics.ts index 853094a..b348e56 100644 --- a/src/utils/metrics.ts +++ b/src/utils/metrics.ts @@ -39,9 +39,21 @@ export function measureTime({ return result; } +export const syncStatus = new client.Gauge({ + name: "watch_tower_sync_status", + help: "Sync status of watch rower. 1 if in sync, 0 otherwise", + labelNames: ["chain_id"], +}); + +export const blockTimestamp = new client.Gauge({ + name: "watch_tower_block_timestamp", + help: "Block timestamp of the last processed block", + labelNames: ["chain_id"], +}); + export const blockHeight = new client.Gauge({ name: "watch_tower_block_height", - help: "Block height of the block watcher", + help: "Block height of the last processed block", labelNames: ["chain_id"], });