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: add prom metrics for stacks block count at previous burn block #2200

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions src/datastore/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4524,4 +4524,13 @@ export class PgStore extends BasePgStore {
`;
if (result.count) return result[0];
}

async getStacksBlockCountAtBurnBlock(burnBlockHeight: number): Promise<number> {
const result = await this.sql<{ count: string }[]>`
SELECT COUNT(*) AS count
FROM blocks
WHERE burn_block_height = ${burnBlockHeight} AND canonical = TRUE
`;
return parseInt(result[0]?.count ?? '0');
}
}
6 changes: 6 additions & 0 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,12 @@ export class PgWriteStore extends PgStore {
});
}

async updateBurnChainBlockHeight(args: { blockHeight: number }): Promise<void> {
await this.sql`
UPDATE chain_tip SET burn_block_height = GREATEST(${args.blockHeight}, burn_block_height)
`;
}

async insertSlotHoldersBatch(sql: PgSqlClient, slotHolders: DbRewardSlotHolder[]): Promise<void> {
const slotValues: RewardSlotHolderInsertValues[] = slotHolders.map(slot => ({
canonical: true,
Expand Down
38 changes: 29 additions & 9 deletions src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async function handleBurnBlockMessage(
burnchainBlockHeight: burnBlockMsg.burn_block_height,
slotHolders: slotHolders,
});
await db.updateBurnChainBlockHeight({ blockHeight: burnBlockMsg.burn_block_height });
}

async function handleMempoolTxsMessage(rawTxs: string[], db: PgWriteStore): Promise<void> {
Expand Down Expand Up @@ -635,14 +636,26 @@ function createMessageProcessorQueue(): EventMessageHandler {
// Create a promise queue so that only one message is handled at a time.
const processorQueue = new PQueue({ concurrency: 1 });

let eventTimer: prom.Histogram<'event'> | undefined;
let metrics:
| {
eventTimer: prom.Histogram;
blocksInPreviousBurnBlock: prom.Gauge;
}
| undefined;
if (isProdEnv) {
eventTimer = new prom.Histogram({
name: 'stacks_event_ingestion_timers',
help: 'Event ingestion timers',
labelNames: ['event'],
buckets: prom.exponentialBuckets(50, 3, 10), // 10 buckets, from 50 ms to 15 minutes
});
metrics = {
eventTimer: new prom.Histogram({
name: 'stacks_event_ingestion_timers',
help: 'Event ingestion timers',
labelNames: ['event'],
buckets: prom.exponentialBuckets(50, 3, 10), // 10 buckets, from 50 ms to 15 minutes
}),
blocksInPreviousBurnBlock: new prom.Gauge({
name: 'stacks_blocks_in_previous_burn_block',
help: 'Number of Stacks blocks produced in the previous burn block',
}),
};
metrics.blocksInPreviousBurnBlock.remove();
}

const observeEvent = async (event: string, fn: () => Promise<void>) => {
Expand All @@ -651,7 +664,7 @@ function createMessageProcessorQueue(): EventMessageHandler {
await fn();
} finally {
const elapsedMs = timer.getElapsed();
eventTimer?.observe({ event }, elapsedMs);
metrics?.eventTimer.observe({ event }, elapsedMs);
}
};

Expand Down Expand Up @@ -686,7 +699,14 @@ function createMessageProcessorQueue(): EventMessageHandler {
},
handleBurnBlock: (msg: CoreNodeBurnBlockMessage, db: PgWriteStore) => {
return processorQueue
.add(() => observeEvent('burn_block', () => handleBurnBlockMessage(msg, db)))
.add(async () => {
await observeEvent('burn_block', () => handleBurnBlockMessage(msg, db));
if (metrics) {
metrics.blocksInPreviousBurnBlock.set(
await db.getStacksBlockCountAtBurnBlock(msg.burn_block_height - 1)
);
}
})
rafaelcr marked this conversation as resolved.
Show resolved Hide resolved
.catch(e => {
logger.error(e, 'Error processing core node burn block message');
throw e;
Expand Down
Loading