Skip to content

Commit

Permalink
feat(store-indexer): add metric for distance from block tag to follow (
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Apr 29, 2024
1 parent 4e66730 commit 93690fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-fireants-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-indexer": patch
---

Added a `distance_from_follow_block` metric to compare the latest stored block number with the block number corresponding to the block tag the indexer follows.
9 changes: 9 additions & 0 deletions packages/store-indexer/bin/postgres-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {
}
}

async function getDistanceFromFollowBlock(): Promise<bigint> {
const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([
getLatestStoredBlockNumber(),
publicClient.getBlock({ blockTag: env.FOLLOW_BLOCK_TAG }),
]);
return (latestStoredBlockNumber ?? -1n) - latestFollowBlock.number;
}

const latestStoredBlockNumber = await getLatestStoredBlockNumber();
if (latestStoredBlockNumber != null) {
startBlock = latestStoredBlockNumber + 1n;
Expand Down Expand Up @@ -117,6 +125,7 @@ if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {
isHealthy: () => true,
isReady: () => isCaughtUp,
getLatestStoredBlockNumber,
getDistanceFromFollowBlock,
followBlockTag: env.FOLLOW_BLOCK_TAG,
}),
);
Expand Down
9 changes: 9 additions & 0 deletions packages/store-indexer/bin/sqlite-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {
return currentChainState?.lastUpdatedBlockNumber ?? undefined;
}

async function getDistanceFromFollowBlock(): Promise<bigint> {
const [latestStoredBlockNumber, latestFollowBlock] = await Promise.all([
getLatestStoredBlockNumber(),
publicClient.getBlock({ blockTag: env.FOLLOW_BLOCK_TAG }),
]);
return (latestStoredBlockNumber ?? -1n) - latestFollowBlock.number;
}

const currentChainState = await getCurrentChainState();
if (currentChainState) {
// Reset the db if the version changed
Expand Down Expand Up @@ -132,6 +140,7 @@ server.use(
isHealthy: () => true,
isReady: () => isCaughtUp,
getLatestStoredBlockNumber,
getDistanceFromFollowBlock,
followBlockTag: env.FOLLOW_BLOCK_TAG,
}),
);
Expand Down
12 changes: 12 additions & 0 deletions packages/store-indexer/src/koa-middleware/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type MetricsOptions = {
isHealthy?: () => boolean;
isReady?: () => boolean;
getLatestStoredBlockNumber?: () => Promise<bigint | undefined>;
getDistanceFromFollowBlock?: () => Promise<bigint>;
followBlockTag?: "latest" | "safe" | "finalized";
};

Expand All @@ -15,6 +16,7 @@ export function metrics({
isHealthy,
isReady,
getLatestStoredBlockNumber,
getDistanceFromFollowBlock,
followBlockTag,
}: MetricsOptions = {}): Middleware {
promClient.collectDefaultMetrics();
Expand Down Expand Up @@ -61,6 +63,16 @@ export function metrics({
blockTagGauge.set(blockTagToValue[followBlockTag]);
}

if (getDistanceFromFollowBlock != null) {
new promClient.Gauge({
name: "distance_from_follow_block",
help: "Block distance from the block tag this the indexer is following",
async collect(): Promise<void> {
this.set(Number(await getDistanceFromFollowBlock()));
},
});
}

return async function metricsMiddleware(ctx, next): Promise<void> {
if (ctx.path === "/metrics") {
ctx.status = 200;
Expand Down

0 comments on commit 93690fd

Please sign in to comment.