feat(observability): stats endpoint, Prometheus metrics, SLO tracking, Grafana dashboard (BIT-734)
- 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
This commit is contained in:
1 parent
5c97326d11
commit
c6e4305c79
11 files changed
+1098
-2
No files matched your search
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Prometheus metrics plugin for the API service.
|
||||
* Exposes GET /metrics (text/plain; Prometheus exposition format).
|
||||
*
|
||||
* Collects:
|
||||
* - Default Node.js metrics (GC, event loop, heap, active handles)
|
||||
* - HTTP request duration histogram (route, method, status)
|
||||
* - Error-monitoring domain gauges (updated by /api/error-monitoring/stats)
|
||||
*/
|
||||
|
||||
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import {
|
||||
Registry,
|
||||
collectDefaultMetrics,
|
||||
Histogram,
|
||||
Gauge,
|
||||
Counter,
|
||||
} from 'prom-client';
|
||||
|
||||
const register = new Registry();
|
||||
|
||||
collectDefaultMetrics({ register, prefix: 'er_api_' });
|
||||
|
||||
/* ── HTTP request metrics ────────────────────────────────────────────── */
|
||||
|
||||
const httpRequestDuration = new Histogram({
|
||||
name: 'er_api_http_request_duration_seconds',
|
||||
help: 'HTTP request duration in seconds',
|
||||
labelNames: ['method', 'route', 'status_code'] as const,
|
||||
buckets: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const httpRequestsTotal = new Counter({
|
||||
name: 'er_api_http_requests_total',
|
||||
help: 'Total HTTP requests',
|
||||
labelNames: ['method', 'route', 'status_code'] as const,
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
/* ── Domain gauges (set externally via metricsGauges) ────────────────── */
|
||||
|
||||
const incidentsTotalGauge = new Gauge({
|
||||
name: 'er_incidents_total',
|
||||
help: 'Total incidents by state',
|
||||
labelNames: ['state'] as const,
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const incidentsBySeverityGauge = new Gauge({
|
||||
name: 'er_incidents_by_severity',
|
||||
help: 'Total incidents by severity',
|
||||
labelNames: ['severity'] as const,
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const occurrences24hGauge = new Gauge({
|
||||
name: 'er_occurrences_last_24h',
|
||||
help: 'Occurrence count in the last 24 hours',
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const resolved24hGauge = new Gauge({
|
||||
name: 'er_resolved_last_24h',
|
||||
help: 'Incidents resolved in the last 24 hours',
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const projectIncidentsGauge = new Gauge({
|
||||
name: 'er_project_incidents_total',
|
||||
help: 'Total incidents per project',
|
||||
labelNames: ['project', 'state'] as const,
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
const projectResolutionRateGauge = new Gauge({
|
||||
name: 'er_project_resolution_rate',
|
||||
help: 'Resolution rate per project (0..1)',
|
||||
labelNames: ['project'] as const,
|
||||
registers: [register],
|
||||
});
|
||||
|
||||
export const metricsGauges = {
|
||||
incidentsTotalGauge,
|
||||
incidentsBySeverityGauge,
|
||||
occurrences24hGauge,
|
||||
resolved24hGauge,
|
||||
projectIncidentsGauge,
|
||||
projectResolutionRateGauge,
|
||||
};
|
||||
|
||||
export { register as prometheusRegistry };
|
||||
|
||||
export default async function prometheusPlugin(app: FastifyInstance): Promise<void> {
|
||||
// Track request duration
|
||||
app.addHook('onResponse', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const route = request.routeOptions?.url ?? request.url;
|
||||
// Skip metrics endpoint itself to avoid recursion
|
||||
if (route === '/metrics') return;
|
||||
const elapsed = reply.elapsedTime / 1000; // ms → seconds
|
||||
const labels = {
|
||||
method: request.method,
|
||||
route,
|
||||
status_code: String(reply.statusCode),
|
||||
};
|
||||
httpRequestDuration.observe(labels, elapsed);
|
||||
httpRequestsTotal.inc(labels);
|
||||
});
|
||||
|
||||
// Expose /metrics
|
||||
app.get('/metrics', async (_request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.header('Content-Type', register.contentType);
|
||||
return reply.send(await register.metrics());
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user