- GET /api/error-monitoring/stats: 24h ingestion volume, severity distribution, per-project metrics with resolution rates, hourly trend - Prometheus /metrics on both api (port 3200) and ingest-api (port 3041) with HTTP request duration histograms, ingest SLO latency (p50/p95/p99), event throughput counters, domain gauges - Grafana dashboard JSON (deploy/grafana/) with SLO panels, error volume, severity/state distribution, per-project charts, Node.js process metrics - SLO runbook (docs/RUNBOOK_SLO.md): alert rules, scrape config, triage procedures for latency, error rate, and availability breaches Paperclip-Agent: Peter Spark (urlKey peter-spark; id 49ce66f8-6be6-4644-afd3-eb6d8d0d4b3e) Paperclip-Issue: BIT-734 Paperclip-Run: f45bcdc4-4757-4829-8e75-b60a8f9a7be4
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
/**
|
|
* 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",
|
|
labelNames: ["outcome"] as const, // stored, skipped_noise, error
|
|
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],
|
|
});
|
|
|
|
export { register as prometheusRegistry };
|
|
|
|
export default async function prometheusPlugin(
|
|
app: FastifyInstance,
|
|
): Promise<void> {
|
|
// 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());
|
|
});
|
|
}
|