-
Notifications
You must be signed in to change notification settings - Fork 196
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
Changes from 2 commits
14b04c2
af880cc
e1efc76
e1eee34
6ad0bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,12 @@ async function getLatestStoredBlockNumber(): Promise<bigint | undefined> { | |
return currentChainState?.lastUpdatedBlockNumber ?? undefined; | ||
} | ||
|
||
async function getDistanceFromFollowBlock(): Promise<bigint | undefined> { | ||
const latestStoredBlockNumber = (await getLatestStoredBlockNumber()) ?? -1n; | ||
const latestFollowBlockNumber = (await publicClient.getBlock({ blockTag: env.FOLLOW_BLOCK_TAG })).number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -132,6 +138,7 @@ server.use( | |
isHealthy: () => true, | ||
isReady: () => isCaughtUp, | ||
getLatestStoredBlockNumber, | ||
getDistanceFromFollowBlock, | ||
followBlockTag: env.FOLLOW_BLOCK_TAG, | ||
}), | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ type MetricsOptions = { | |
isHealthy?: () => boolean; | ||
isReady?: () => boolean; | ||
getLatestStoredBlockNumber?: () => Promise<bigint | undefined>; | ||
getDistanceFromFollowBlock?: () => Promise<bigint | undefined>; | ||
followBlockTag?: "latest" | "safe" | "finalized"; | ||
}; | ||
|
||
|
@@ -15,6 +16,7 @@ export function metrics({ | |
isHealthy, | ||
isReady, | ||
getLatestStoredBlockNumber, | ||
getDistanceFromFollowBlock, | ||
followBlockTag, | ||
}: MetricsOptions = {}): Middleware { | ||
promClient.collectDefaultMetrics(); | ||
|
@@ -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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think number is better here so we can do things like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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?