/** * Tests for the Prometheus metrics plugin in ingest-api. */ import { describe, it, expect } from 'vitest'; import Fastify from 'fastify'; import prometheusPlugin from './prometheus.js'; async function buildTestApp() { const app = Fastify({ logger: false }); await app.register(prometheusPlugin); return app; } describe('prometheusPlugin', () => { it('exposes GET /metrics returning Prometheus text format', async () => { const app = await buildTestApp(); const res = await app.inject({ method: 'GET', url: '/metrics' }); expect(res.statusCode).toBe(200); expect(res.headers['content-type']).toContain('text/plain'); expect(res.payload).toContain('er_ingest_'); }); it('increments active connections on request and decrements on response', async () => { const app = await buildTestApp(); // Fire a request — active connections hook runs around it await app.inject({ method: 'GET', url: '/metrics' }); // No assertion on exact gauge value since it resets per request, // but confirm the metrics endpoint still returns data const res = await app.inject({ method: 'GET', url: '/metrics' }); expect(res.statusCode).toBe(200); }); it('records ingest request duration for /v1/ingest paths', async () => { const app = Fastify({ logger: false }); await app.register(prometheusPlugin); app.post('/v1/ingest/events', async (_req, reply) => reply.status(200).send({ ok: true })); await app.inject({ method: 'POST', url: '/v1/ingest/events', payload: {} }); const metricsRes = await app.inject({ method: 'GET', url: '/metrics' }); expect(metricsRes.payload).toContain('er_ingest_request_duration_seconds'); expect(metricsRes.payload).toContain('er_ingest_requests_total'); }); it('does not track /metrics itself in SLO histogram', async () => { const app = await buildTestApp(); // Call metrics twice — the second call should not create a recursive entry await app.inject({ method: 'GET', url: '/metrics' }); const res = await app.inject({ method: 'GET', url: '/metrics' }); expect(res.statusCode).toBe(200); }); });