/** * Prometheus metrics plugin for ingest-api. * Exposes GET /metrics (text/plain; Prometheus exposition format). * * Key SLO metric: er_ingest_request_duration_seconds histogram * with p50/p95/p99 buckets for latency tracking. */ import type { FastifyInstance, FastifyRequest, FastifyReply } from "fastify"; import { Registry, collectDefaultMetrics, Histogram, Counter, Gauge, } from "prom-client"; const register = new Registry(); collectDefaultMetrics({ register, prefix: "er_ingest_" }); /* ── Ingest SLO: request latency histogram ───────────────────────────── */ export const ingestDuration = new Histogram({ name: "er_ingest_request_duration_seconds", help: "Ingest request duration in seconds (SLO target: p95 < 500ms, p99 < 1s)", labelNames: ["status_code"] as const, buckets: [0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3, 0.5, 0.75, 1, 2, 5], registers: [register], }); /* ── Ingest throughput counters ──────────────────────────────────────── */ export const ingestEventsTotal = new Counter({ name: "er_ingest_events_total", help: "Total events ingested", // rule_id is set for skipped_noise (noise policy id); empty for stored/error labelNames: ["outcome", "rule_id"] as const, registers: [register], }); export const ingestRequestsTotal = new Counter({ name: "er_ingest_requests_total", help: "Total ingest HTTP requests", labelNames: ["status_code"] as const, registers: [register], }); export const ingestBatchSize = new Histogram({ name: "er_ingest_batch_size", help: "Number of events per ingest request", buckets: [1, 2, 5, 10, 20, 50], registers: [register], }); /* ── Active connections gauge ────────────────────────────────────────── */ export const activeConnections = new Gauge({ name: "er_ingest_active_connections", help: "Number of in-flight ingest requests", registers: [register], }); /* ── Error hub SLO / alerting (BIT-4903 / BIT-4932) — names match Prometheus rules in Paperclip bundle ─ */ export const errorHubLastEventTimestamp = new Gauge({ name: "error_hub_last_event_timestamp", help: "Unix time (seconds) of the latest successfully stored ingest event occurred_at, per project_slug", labelNames: ["project_slug"] as const, registers: [register], }); export const errorHubIngestFailuresTotal = new Counter({ name: "error_hub_ingest_failures_total", help: "Ingest handler failures (HTTP 500) counted by events in the failed batch, per project_slug", labelNames: ["project_slug"] as const, registers: [register], }); export { register as prometheusRegistry }; export default async function prometheusPlugin( app: FastifyInstance, ): Promise { // Track all requests app.addHook("onRequest", async () => { activeConnections.inc(); }); app.addHook( "onResponse", async (request: FastifyRequest, reply: FastifyReply) => { activeConnections.dec(); const route = request.routeOptions?.url ?? request.url; if (route === "/metrics") return; const elapsed = reply.elapsedTime / 1000; const statusCode = String(reply.statusCode); // Only track ingest endpoint for SLO histogram if (route === "/events" || request.url.startsWith("/v1/ingest")) { ingestDuration.observe({ status_code: statusCode }, elapsed); ingestRequestsTotal.inc({ status_code: statusCode }); } }, ); // Expose /metrics app.get("/metrics", async (_request: FastifyRequest, reply: FastifyReply) => { reply.header("Content-Type", register.contentType); return reply.send(await register.metrics()); }); }