From 582b45204e70be8eb396b909ddb40ad65a31de82 Mon Sep 17 00:00:00 2001 From: Benedikt Labrenz Date: Thu, 19 Feb 2026 14:52:27 +0100 Subject: [PATCH 01/19] add prom-client to provide prometheus metrics --- AGENTS.md | 22 +++++++++++++++++-- package-lock.json | 40 ++++++++++++++++++++++++++++++++++- package.json | 3 ++- src/hooks.server.ts | 23 +++++++++++++++++--- src/lib/server/metrics.ts | 13 ++++++++++++ src/routes/metrics/+server.ts | 8 +++++++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 src/lib/server/metrics.ts create mode 100644 src/routes/metrics/+server.ts diff --git a/AGENTS.md b/AGENTS.md index b8783f8d..d0c36f61 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,6 +16,7 @@ The application is designed with a **plugin/module architecture** so future Stac - **zod & superforms** - All forms are to use zod & superforms - **Generic OIDC** (openid-client) - Authentication via any OIDC provider - **OPA** - Authorisation / feature visibility +- **prom-client** - Prometheus metrics, exposed at `/metrics` ## Project Structure @@ -32,7 +33,7 @@ This is a **single SvelteKit application** (not a monorepo). │ └── hooks.server.ts # Server hooks (auth middleware) ├── e2e/ # Playwright E2E tests ├── static/ # Static assets -├── Dockerfile # Production container image +├── docker/Dockerfile # Production container image └── CLAUDE.md # AI assistant instructions ``` @@ -69,6 +70,14 @@ The application must comply with **BITV 2.0** (German accessibility regulation, - The editor must be loaded client-side only - Use the custom `trinosql` language registration for Trino SQL support +### Metrics + +The app exposes a Prometheus scrape endpoint at `/metrics` via `src/routes/metrics/+server.ts`. HTTP request duration is tracked automatically for all routes in `hooks.server.ts`. + +- **Adding custom metrics**: Define new counters, histograms, or gauges in `src/lib/server/metrics.ts` and import them in the relevant server-side code (e.g. track query execution counts, Trino errors, cache hits) +- **Server-only**: Never import from `src/lib/server/metrics.ts` in client-side code or `.svelte` files +- **When to add metrics**: New API endpoints, background operations, external service calls, and any operation where latency or error rates are operationally significant + ### Code Style & Best Practices - Use spaces not tabs @@ -84,7 +93,7 @@ The application must comply with **BITV 2.0** (German accessibility regulation, } throw redirect(303, '/path'); // Outside try-catch ``` -- **Public Routes**: Update `hooks.server.ts` when adding unauthenticated pages. +- **Public Routes**: Update `hooks.server.ts` when adding unauthenticated pages. The `/metrics` endpoint is permanently public (Prometheus scraping) — never add auth in front of it. - **Label-Element Association**: Always explicitly associate `