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(store-indexer): add metric for distance from block tag to follow #2763

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
7 changes: 7 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,12 @@ async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {
}
}

async function getDistanceFromFollowBlock(): Promise<bigint | undefined> {
const latestStoredBlockNumber = (await getLatestStoredBlockNumber()) ?? -1n;
const latestFollowBlockNumber = (await publicClient.getBlock({ blockTag: env.FOLLOW_BLOCK_TAG })).number;
return latestFollowBlockNumber - latestStoredBlockNumber;
}

const latestStoredBlockNumber = await getLatestStoredBlockNumber();
if (latestStoredBlockNumber != null) {
startBlock = latestStoredBlockNumber + 1n;
Expand Down Expand Up @@ -117,6 +123,7 @@ if (env.HEALTHCHECK_HOST != null || env.HEALTHCHECK_PORT != null) {
isHealthy: () => true,
isReady: () => isCaughtUp,
getLatestStoredBlockNumber,
getDistanceFromFollowBlock,
followBlockTag: env.FOLLOW_BLOCK_TAG,
}),
);
Expand Down
7 changes: 7 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,12 @@ async function getLatestStoredBlockNumber(): Promise<bigint | undefined> {
return currentChainState?.lastUpdatedBlockNumber ?? undefined;
}

async function getDistanceFromFollowBlock(): Promise<bigint | undefined> {
Copy link
Member

Choose a reason for hiding this comment

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

looks like we're always returning a bigint now - do we need undefined here?

const latestStoredBlockNumber = (await getLatestStoredBlockNumber()) ?? -1n;
const latestFollowBlockNumber = (await publicClient.getBlock({ blockTag: env.FOLLOW_BLOCK_TAG })).number;
Copy link
Member

Choose a reason for hiding this comment

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

should we parallelize these to maybe have a better chance of these aligning?

return latestFollowBlockNumber - latestStoredBlockNumber;
}

const currentChainState = await getCurrentChainState();
if (currentChainState) {
// Reset the db if the version changed
Expand Down Expand Up @@ -132,6 +138,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 | undefined>;
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()));
Copy link
Member

@holic holic Apr 29, 2024

Choose a reason for hiding this comment

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

does this.set on a gauge only support number? vs a numeric string like BigInt.toString()

Copy link
Member Author

Choose a reason for hiding this comment

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

I think number is better here so we can do things like > for the alerts (and very unlikely the numbers ever get into a range where they don't fit into a number anymore)

Copy link
Member

@holic holic Apr 29, 2024

Choose a reason for hiding this comment

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

oh yes, I just assumed that what "gauge" metric type is for, not the value we pass into the gauge

just wondering about the unlikely edge case/assumption where the bigint value > JS number max

},
});
}

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