trails/apps/planner/app/lib/metrics.server.ts
Ullrich Schäfer 62e208d850
Add Prometheus metrics + Grafana panels for Overpass proxy
Adds four metrics surfaced via the existing /metrics endpoint:
- overpass_cache_events_total{result=hit|miss|coalesced}
- overpass_cache_size (gauge)
- overpass_upstream_duration_seconds (histogram)
- overpass_upstream_requests_total{status} — covers 2xx/4xx/5xx and
  an explicit "error" label for network-level failures

Grafana dashboards/planner.json gets a new row:
- Upstream health (5m 2xx success ratio, red <90%, green >99%)
- Cache hit ratio
- Cache size
- Upstream p95 latency
Plus timeseries panels for cache event breakdown, upstream status
over time, and p50/p95/p99 upstream latency.

The success-rate formula uses clamp_min(..., 1) on the denominator so
it doesn't NaN when traffic is zero.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 02:12:19 +02:00

75 lines
2.4 KiB
TypeScript

import client from "prom-client";
// Guard all metric registration — Vite's dev server can re-evaluate
// this module, causing "already registered" errors.
function getOrCreate<T>(name: string, create: () => T): T {
return (client.register.getSingleMetric(name) as T) ?? create();
}
if (!client.register.getSingleMetric("process_cpu_user_seconds_total")) {
client.collectDefaultMetrics();
}
export const httpRequestDuration = getOrCreate("http_request_duration_seconds", () =>
new client.Histogram({
name: "http_request_duration_seconds",
help: "Duration of HTTP requests in seconds",
labelNames: ["method", "route", "status"] as const,
buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5],
}),
);
export const plannerActiveSessions = getOrCreate("planner_active_sessions", () =>
new client.Gauge({
name: "planner_active_sessions",
help: "Number of active planner sessions",
}),
);
export const plannerConnectedClients = getOrCreate("planner_connected_clients", () =>
new client.Gauge({
name: "planner_connected_clients",
help: "Number of connected WebSocket clients",
}),
);
export const brouterRequestDuration = getOrCreate("brouter_request_duration_seconds", () =>
new client.Histogram({
name: "brouter_request_duration_seconds",
help: "Duration of BRouter API requests in seconds",
buckets: [0.1, 0.25, 0.5, 1, 2, 5, 10],
}),
);
export const overpassCacheEvents = getOrCreate("overpass_cache_events_total", () =>
new client.Counter({
name: "overpass_cache_events_total",
help: "Overpass proxy cache events",
labelNames: ["result"] as const, // hit | miss | coalesced
}),
);
export const overpassCacheSize = getOrCreate("overpass_cache_size", () =>
new client.Gauge({
name: "overpass_cache_size",
help: "Current number of entries in the Overpass proxy cache",
}),
);
export const overpassUpstreamDuration = getOrCreate("overpass_upstream_duration_seconds", () =>
new client.Histogram({
name: "overpass_upstream_duration_seconds",
help: "Duration of upstream Overpass API requests in seconds",
buckets: [0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
}),
);
export const overpassUpstreamRequests = getOrCreate("overpass_upstream_requests_total", () =>
new client.Counter({
name: "overpass_upstream_requests_total",
help: "Upstream Overpass API requests by status",
labelNames: ["status"] as const,
}),
);
export const registry = client.register;