Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit ce25cb0

Browse files
committed
fix: fastify-metrics version
1 parent 081f442 commit ce25cb0

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

api/package-lock.json

+18-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"bignumber.js": "^9.1.2",
5757
"env-schema": "^5.2.1",
5858
"fastify": "4.15.0",
59-
"fastify-metrics": "^10.2.0",
59+
"fastify-metrics": "10.2.0",
6060
"pino": "^8.10.0",
6161
"postgres": "^3.3.4"
6262
}

api/src/api/init.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
2-
import { buildFastifyApiServer } from '@hirosystems/api-toolkit';
2+
import FastifyCors from '@fastify/cors';
3+
import Fastify, { FastifyInstance } from 'fastify';
4+
import FastifyMetrics, { IFastifyMetrics } from 'fastify-metrics';
35
import { FastifyPluginAsync } from 'fastify';
46
import { Server } from 'http';
57
import { PgStore } from '../pg/pg-store';
@@ -8,6 +10,7 @@ import { AddressRoutes } from './routes/addresses';
810
import { TransactionRoutes } from './routes/transactions';
911
import { BlockRoutes } from './routes/blocks';
1012
import { StatusRoutes } from './routes/status';
13+
import { PINO_LOGGER_CONFIG, isProdEnv } from '@hirosystems/api-toolkit';
1114

1215
export const Api: FastifyPluginAsync<
1316
Record<never, never>,
@@ -22,11 +25,35 @@ export const Api: FastifyPluginAsync<
2225
};
2326

2427
export async function buildApiServer(args: { db: PgStore }) {
25-
const fastify = await buildFastifyApiServer();
26-
28+
const fastify = Fastify({
29+
trustProxy: true,
30+
logger: PINO_LOGGER_CONFIG,
31+
}).withTypeProvider<TypeBoxTypeProvider>();
32+
if (isProdEnv) {
33+
await fastify.register(FastifyMetrics, { endpoint: null });
34+
}
35+
await fastify.register(FastifyCors);
2736
fastify.decorate('db', args.db);
2837
await fastify.register(Api, { prefix: '/runes/v1' });
2938
await fastify.register(Api, { prefix: '/runes' });
3039

3140
return fastify;
3241
}
42+
43+
export async function buildPrometheusServer(args: {
44+
metrics: IFastifyMetrics;
45+
}): Promise<FastifyInstance> {
46+
const promServer = Fastify({
47+
trustProxy: true,
48+
logger: PINO_LOGGER_CONFIG,
49+
});
50+
promServer.route({
51+
url: '/metrics',
52+
method: 'GET',
53+
logLevel: 'info',
54+
handler: async (_, reply) => {
55+
await reply.type('text/plain').send(await args.metrics.client.register.metrics());
56+
},
57+
});
58+
return promServer;
59+
}

api/src/index.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
buildPrometheusServer,
3-
isProdEnv,
4-
logger,
5-
registerShutdownConfig,
6-
} from '@hirosystems/api-toolkit';
7-
import { buildApiServer } from './api/init';
1+
import { isProdEnv, logger, registerShutdownConfig } from '@hirosystems/api-toolkit';
2+
import { buildApiServer, buildPrometheusServer } from './api/init';
83
import { ENV } from './env';
94
import { PgStore } from './pg/pg-store';
105
import { ApiMetrics } from './metrics/metrics';

0 commit comments

Comments
 (0)