Skip to content

Commit

Permalink
feat: prometheus metrics for time_since_last_pending_block_proposal_ms
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Nov 22, 2024
1 parent 66eacf4 commit e0a2adc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/api/routes/prom-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ export const SignerPromMetricsRoutes: FastifyPluginAsync<

const signerRegistry = new Registry();

new Gauge({
name: 'time_since_last_pending_block_proposal_ms',
help: 'Time in milliseconds since the oldest pending block proposal',
registers: [signerRegistry],
async collect() {
const dbResult = await db.sqlTransaction(async sql => {
return await db.getLastPendingProposalDate({ sql });
});
this.reset();
this.set(dbResult ? Date.now() - dbResult.getTime() : 0);
},
});

new Gauge({
name: 'avg_block_push_time_ms',
help: 'Average time (in milliseconds) taken for block proposals to be accepted and pushed over different block periods',
Expand Down
29 changes: 29 additions & 0 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,4 +1059,33 @@ export class PgStore extends BasePgStore {
`;
return result;
}

async getLastPendingProposalDate(args: { sql: PgSqlClient }) {
const result = await args.sql<
{
received_at: Date | null;
}[]
>`
WITH last_confirmed_height AS (
SELECT GREATEST(
COALESCE(MAX(b.block_height), 0),
COALESCE(MAX(bp.block_height), 0)
) AS height
FROM blocks b
LEFT JOIN block_pushes bp ON true
),
oldest_pending_proposal AS (
SELECT received_at
FROM block_proposals bp
WHERE bp.block_height > (SELECT height FROM last_confirmed_height)
ORDER BY received_at ASC
LIMIT 1
)
SELECT received_at
FROM oldest_pending_proposal
`;

// Return the computed value or null if no pending proposals exist
return result.length > 0 ? result[0].received_at : null;
}
}
15 changes: 15 additions & 0 deletions tests/db/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ describe('Endpoint tests', () => {
ENV.reload();
const blockRanges = ENV[bucketsEnvName].split(',').map(Number);

// Delete from confirmed blocks tables to ensure there is a pending block_proposal
const [{ block_height }] = await db.sql<
{ block_height: number }[]
>`SELECT block_height FROM block_proposals ORDER BY block_height DESC LIMIT 1`;
await db.sql`DELETE FROM blocks WHERE block_height >= ${block_height}`;
await db.sql`DELETE FROM block_pushes WHERE block_height >= ${block_height}`;

const pendingProposalDate = await db.getLastPendingProposalDate({ sql: db.sql });
expect(pendingProposalDate).toBeInstanceOf(Date);
expect(new Date().getTime() - pendingProposalDate!.getTime()).toBeGreaterThan(0);

const dbPushMetricsResult = await db.getRecentBlockPushMetrics({ sql: db.sql, blockRanges });
expect(dbPushMetricsResult).toEqual([
{ block_range: 1, avg_push_time_ms: 27262 },
Expand Down Expand Up @@ -143,6 +154,10 @@ describe('Endpoint tests', () => {
.expect(200);
const receivedLines = responseTest.text.split('\n');

const expectedPendingProposalLineRegex =
/# TYPE time_since_last_pending_block_proposal_ms gauge\ntime_since_last_pending_block_proposal_ms [1-9]\d*/;
expect(responseTest.text).toMatch(expectedPendingProposalLineRegex);

const expectedPushTimeLines = `# TYPE avg_block_push_time_ms gauge
avg_block_push_time_ms{period="1"} 27262
avg_block_push_time_ms{period="2"} 30435.5
Expand Down

0 comments on commit e0a2adc

Please sign in to comment.