Skip to content

Commit

Permalink
fix: enable prometheus metrics (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Jul 2, 2024
1 parent bd4d666 commit e3533ef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
34 changes: 19 additions & 15 deletions api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { isProdEnv, logger, registerShutdownConfig } from '@hirosystems/api-toolkit';
import {
buildPrometheusServer,
isProdEnv,
logger,
registerShutdownConfig,
} from '@hirosystems/api-toolkit';
import { buildApiServer } from './api/init';
import { ENV } from './env';
import { PgStore } from './pg/pg-store';
// import { ApiMetrics } from './metrics/metrics';
import { ApiMetrics } from './metrics/metrics';

async function initApiService(db: PgStore) {
logger.info('Initializing API service...');
Expand All @@ -17,19 +22,18 @@ async function initApiService(db: PgStore) {

await fastify.listen({ host: ENV.API_HOST, port: ENV.API_PORT });

// if (isProdEnv) {
// const promServer = await buildPromServer({ metrics: fastify.metrics });
// registerShutdownConfig({
// name: 'Prometheus Server',
// forceKillable: false,
// handler: async () => {
// await promServer.close();
// },
// });

// // ApiMetrics.configure(db);
// await promServer.listen({ host: ENV.API_HOST, port: 9153 });
// }
if (isProdEnv) {
const promServer = await buildPrometheusServer({ metrics: fastify.metrics });
registerShutdownConfig({
name: 'Prometheus Server',
forceKillable: false,
handler: async () => {
await promServer.close();
},
});
ApiMetrics.configure(db);
await promServer.listen({ host: ENV.API_HOST, port: 9153 });
}
}

async function initApp() {
Expand Down
22 changes: 22 additions & 0 deletions api/src/metrics/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 runes_api_block_height: prom.Gauge;

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

private constructor(db: PgStore) {
this.runes_api_block_height = new prom.Gauge({
name: `runes_api_block_height`,
help: 'The most recent Bitcoin block height ingested by the API',
async collect() {
const height = await db.getChainTipBlockHeight();
this.set(parseInt(height ?? '0'));
},
});
}
}

0 comments on commit e3533ef

Please sign in to comment.