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: api ingestion metrics on prometheus #113

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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);
},
});
}
}
Loading