Skip to content

Commit

Permalink
feat: api ingestion metrics on prometheus (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Jun 28, 2023
1 parent 641a627 commit 10ec679
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isProdEnv } from './api/util/helpers';
import { buildChainhookServer } from './chainhook/server';
import { ENV } from './env';
import { logger } from './logger';
import { ApiMetrics } from './metrics/metrics';
import { PgStore } from './pg/pg-store';
import { registerShutdownConfig } from './shutdown-handler';

Expand Down Expand Up @@ -43,6 +44,7 @@ async function initApiService(db: PgStore) {
},
});

ApiMetrics.configure(db);
await promServer.listen({ host: ENV.API_HOST, port: 9153 });
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/metrics/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as prom from 'prom-client';
import { PgStore } from '../pg/pg-store';

export class ApiMetrics {
/** The most recent Bitcoin block height ingested by the API */
readonly ordinals_api_block_height: prom.Gauge;
/** Maximum blessed inscription number */
readonly ordinals_api_max_inscription_number: prom.Gauge;
/** Maximum cursed inscription number */
readonly ordinals_api_max_cursed_inscription_number: prom.Gauge;

static configure(db: PgStore): ApiMetrics {
return new ApiMetrics(db);
}

private constructor(db: PgStore) {
this.ordinals_api_block_height = new prom.Gauge({
name: `ordinals_api_block_height`,
help: 'The most recent Bitcoin block height ingested by the API',
async collect() {
const height = await db.getChainTipBlockHeight();
this.set(height);
},
});
this.ordinals_api_max_inscription_number = new prom.Gauge({
name: `ordinals_api_max_inscription_number`,
help: 'Maximum blessed inscription number',
async collect() {
const max = await db.getMaxInscriptionNumber();
if (max) this.set(max);
},
});
this.ordinals_api_max_cursed_inscription_number = new prom.Gauge({
name: `ordinals_api_max_cursed_inscription_number`,
help: 'Maximum cursed inscription number',
async collect() {
const max = await db.getMaxCursedInscriptionNumber();
if (max) this.set(max);
},
});
}
}

0 comments on commit 10ec679

Please sign in to comment.