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 sync status and time from last indexed block #171

Merged
merged 2 commits 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
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. 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
Loading