Skip to content

Commit

Permalink
fix: newest unconfirmed block propsal metric
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jan 27, 2025
1 parent 03e256e commit e0f3550
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ export class PgStore extends BasePgStore {
return result;
}

async getLastPendingProposalDate(args: { sql: PgSqlClient }) {
async getPendingProposalDate(args: { sql: PgSqlClient; kind: 'oldest' | 'newest' }) {
const result = await args.sql<
{
received_at: Date | null;
Expand All @@ -1090,15 +1090,15 @@ export class PgStore extends BasePgStore {
(SELECT COALESCE(MAX(block_height), 0) FROM block_pushes)
) AS height
),
oldest_pending_proposal AS (
pending_proposal AS (
SELECT received_at
FROM block_proposals bp
WHERE bp.block_height > (SELECT height FROM max_heights)
ORDER BY received_at ASC
ORDER BY received_at ${args.kind == 'newest' ? args.sql`DESC` : args.sql`ASC`}
LIMIT 1
)
SELECT received_at
FROM oldest_pending_proposal
FROM pending_proposal
`;

// Return the computed value or null if no pending proposals exist
Expand Down
16 changes: 14 additions & 2 deletions src/prom-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ export function configureSignerMetrics(db: PgStore) {
const metricsPrefix = 'signer_api_';

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

new Gauge({
name: metricsPrefix + 'time_since_newest_pending_block_proposal_ms',
help: 'Time in milliseconds since the most recent pending block proposal',
async collect() {
const dbResult = await db.sqlTransaction(async sql => {
return await db.getPendingProposalDate({ sql, kind: 'newest' });
});
this.reset();
this.set(dbResult ? Date.now() - dbResult.getTime() : 0);
Expand Down
5 changes: 3 additions & 2 deletions tests/db/db-notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ describe("Db notifications tests", () => {
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({
const pendingProposalDate = await db.getPendingProposalDate({
sql: db.sql,
kind: 'oldest',
});
expect(pendingProposalDate).toBeInstanceOf(Date);
expect(
Expand Down Expand Up @@ -318,7 +319,7 @@ describe("Db notifications tests", () => {
const receivedLines = responseTest.text.split("\n");

const expectedPendingProposalLineRegex = new RegExp(
`# TYPE ${metricPrefix}time_since_last_pending_block_proposal_ms gauge\n${metricPrefix}time_since_last_pending_block_proposal_ms [1-9]\d*`,
`# TYPE ${metricPrefix}time_since_oldest_pending_block_proposal_ms gauge\n${metricPrefix}time_since_oldest_pending_block_proposal_ms [1-9]\d*`,
"g",
);
expect(responseTest.text).toMatch(expectedPendingProposalLineRegex);
Expand Down

0 comments on commit e0f3550

Please sign in to comment.