Skip to content

Commit

Permalink
fix: add block_height to status and prometheus (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Aug 27, 2024
1 parent a9c6024 commit 4502c7d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/api/routes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const StatusRoutes: FastifyPluginCallback<
},
async (request, reply) => {
const result = await fastify.db.sqlTransaction(async sql => {
const block_height = await fastify.db.getChainTipBlockHeight();

const smartContracts: Record<string, number> = {};
const contractCounts = await fastify.db.getSmartContractCounts();
for (const row of contractCounts) {
Expand All @@ -45,6 +47,9 @@ export const StatusRoutes: FastifyPluginCallback<
return {
server_version: `token-metadata-api ${SERVER_VERSION.tag} (${SERVER_VERSION.branch}:${SERVER_VERSION.commit})`,
status: 'ready',
chain_tip: {
block_height,
},
tokens: tokenCounts.length ? tokens : undefined,
token_contracts: contractCounts.length ? smartContracts : undefined,
job_queue: jobCounts.length ? queue : undefined,
Expand Down
3 changes: 3 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ export const ApiStatusResponse = Type.Object(
{
server_version: Type.String({ examples: ['token-metadata-api v0.0.1 (master:a1b2c3)'] }),
status: Type.String({ examples: ['ready'] }),
chain_tip: Type.Object({
block_height: Type.Integer({ examples: [163541] }),
}),
tokens: Type.Optional(
Type.Object(
{
Expand Down
8 changes: 4 additions & 4 deletions src/pg/chainhook/chainhook-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export class ChainhookPgStore extends BasePgStoreModule {
);
}

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

private async getLastIngestedBlockHeight(): Promise<number> {
const result = await this.sql<{ block_height: number }[]>`SELECT block_height FROM chain_tip`;
return result[0].block_height;
Expand Down Expand Up @@ -383,10 +387,6 @@ export class ChainhookPgStore extends BasePgStoreModule {
throw new ContractNotFoundError();
}

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

private async enqueueDynamicTokensDueForRefresh(): Promise<void> {
const interval = ENV.METADATA_DYNAMIC_TOKEN_REFRESH_INTERVAL.toString();
await this.sql`
Expand Down
9 changes: 9 additions & 0 deletions src/token-processor/token-processor-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as prom from 'prom-client';
import { PgStore } from '../pg/pg-store';

export class TokenProcessorMetrics {
readonly token_metadata_block_height: prom.Gauge;
/** Job count divided by status */
readonly token_metadata_job_count: prom.Gauge;
/** Smart contract count divided by SIP number */
Expand All @@ -14,6 +15,14 @@ export class TokenProcessorMetrics {
}

private constructor(db: PgStore) {
this.token_metadata_block_height = new prom.Gauge({
name: `token_metadata_block_height`,
help: 'The most recent Bitcoin block height ingested by the API',
async collect() {
const height = await db.getChainTipBlockHeight();
this.set(height);
},
});
this.token_metadata_job_count = new prom.Gauge({
name: `token_metadata_job_count`,
help: 'Job count divided by status',
Expand Down
7 changes: 7 additions & 0 deletions tests/api/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ describe('Status routes', () => {
expect(json).toStrictEqual({
server_version: 'token-metadata-api v0.0.1 (test:123456)',
status: 'ready',
chain_tip: {
block_height: 1,
},
});
const noVersionResponse = await fastify.inject({ method: 'GET', url: '/metadata/' });
expect(response.statusCode).toEqual(noVersionResponse.statusCode);
Expand All @@ -43,12 +46,16 @@ describe('Status routes', () => {
DbSipNumber.sip009,
1n
);
await db.chainhook.updateChainTipBlockHeight(100);

const response = await fastify.inject({ method: 'GET', url: '/metadata/v1/' });
const json = response.json();
expect(json).toStrictEqual({
server_version: 'token-metadata-api v0.0.1 (test:123456)',
status: 'ready',
chain_tip: {
block_height: 100,
},
job_queue: {
pending: 2,
},
Expand Down

0 comments on commit 4502c7d

Please sign in to comment.