Files

36 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

// /health (JSON liveness/readiness) and /metrics (Prometheus exposition).
import { getRatings } from '../lib/imdbRatings.js';
import { imdbRatingsCount, registry } from '../lib/metrics.js';
export default async function healthRoutes(fastify) {
fastify.get('/health', async (_req, reply) => {
reply.header('Cache-Control', 'no-store');
let ratings = 0;
let ratingsOk = false;
try {
const map = await getRatings();
ratings = map.size;
imdbRatingsCount.set(ratings);
ratingsOk = true;
} catch {
/* ignore — reported via ratingsOk */
}
const status = ratingsOk ? 'ok' : 'degraded';
reply.code(ratingsOk ? 200 : 503);
return {
status,
uptime: Math.round(process.uptime()),
pid: process.pid,
node: process.version,
memory_mb: Math.round(process.memoryUsage().rss / 1024 / 1024),
imdb_ratings: ratings,
};
});
fastify.get('/metrics', async (_req, reply) => {
reply.header('Content-Type', registry.contentType);
return registry.metrics();
});
}