Skip to content

Commit

Permalink
feat: add sync status
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Dec 7, 2024
1 parent dd7eafb commit a032060
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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"
Expand Down Expand 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);
Expand All @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion src/utils/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ export function measureTime<T, U>({
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"],
});

Expand Down

0 comments on commit a032060

Please sign in to comment.