Skip to content

Commit

Permalink
fix: run prometheus server at port 9153 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed May 15, 2023
1 parent 9f6927d commit 33823d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { InscriptionsRoutes } from './routes/inscriptions';
import { PgStore } from '../pg/pg-store';
import { SatRoutes } from './routes/sats';
import { StatusRoutes } from './routes/status';
import FastifyMetrics from 'fastify-metrics';
import FastifyMetrics, { IFastifyMetrics } from 'fastify-metrics';
import { isProdEnv } from './util/helpers';

export const Api: FastifyPluginAsync<
Expand All @@ -28,11 +28,29 @@ export async function buildApiServer(args: { db: PgStore }) {

fastify.decorate('db', args.db);
if (isProdEnv) {
await fastify.register(FastifyMetrics);
await fastify.register(FastifyMetrics, { endpoint: null });
}
await fastify.register(FastifyCors);
await fastify.register(Api, { prefix: '/ordinals/v1' });
await fastify.register(Api, { prefix: '/ordinals' });

return fastify;
}

export async function buildPromServer(args: { metrics: IFastifyMetrics }) {
const promServer = Fastify({
trustProxy: true,
logger: PINO_CONFIG,
});

promServer.route({
url: '/metrics',
method: 'GET',
logLevel: 'info',
handler: async (_, reply) => {
await reply.type('text/plain').send(await args.metrics.client.register.metrics());
},
});

return promServer;
}
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildApiServer } from './api/init';
import { buildApiServer, buildPromServer } from './api/init';
import { isProdEnv } from './api/util/helpers';
import { buildChainhookServer } from './chainhook/server';
import { ENV } from './env';
import { logger } from './logger';
Expand Down Expand Up @@ -31,6 +32,19 @@ 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();
},
});

await promServer.listen({ host: ENV.API_HOST, port: 9153 });
}
}

async function initApp() {
Expand Down

0 comments on commit 33823d2

Please sign in to comment.