feat(api): BIT-2601 rate-limit typing, login preset, and rejection metrics
CI / Test ingest-api (push) Has been cancelled
CI / Test api (push) Has been cancelled
CI / Lint and typecheck admin-ui (push) Has been cancelled
CI / Validate Docker build (push) Has been cancelled

Align hub rate-limit error context with @fastify/rate-limit types, parse RATE_*
and body-size env vars via parsePositiveIntEnv, wire POST /admin/auth/login to
the login preset, increment er_api_rate_limit_rejections_total per bucket, and
document ingest_token_id plus Prometheus scraping notes.

Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-12 11:29:31 +00:00
1 parent 6c22dc8bb9
commit b29f72bef4
9 files changed
+155 -58

No files matched your search

+12
View File
@@ -15,6 +15,18 @@
---
## Hub API — rate limiting (`api/`)
The hub registers `@fastify/rate-limit` globally and attaches stricter presets to selected routes (`api/src/plugins/rate-limit.ts`, `api/src/index.ts`). Defaults and env tunables are documented in the plugin header (`RATE_*`, body size caps).
### Request augmentation: `ingest_token_id`
Hub ingest handlers (e.g. future `POST /api/ingest/*`) **may** set `request.ingest_token_id` to the authenticated ingest key’s stable id **before** the rate-limit hook runs. The ingest preset’s `keyGenerator` then uses `ingest:token:<id>` instead of `ingest:ip:<ip>`, so many clients behind one NAT do not share one counter. The separate **ingest-api** process already applies DB-backed per-key limits; this field keeps the hub aligned if ingest is ever co-hosted in the same Fastify app.
Rate-limit denials are counted in Prometheus as **`er_api_rate_limit_rejections_total`** (`bucket` label: `global`, `ingest`, `login`, `mutation`). See [OBSERVABILITY_PROMETHEUS.md](OBSERVABILITY_PROMETHEUS.md).
---
## Hub API — health & metrics
### `GET /health`
+1
View File
@@ -45,6 +45,7 @@ If TLS or auth is fronted by Nginx, scrape the **internal** listener or enable m
- **`er_api_http_requests_total`** — labels: `method`, `route`, `status_code`.
- **`er_api_http_request_duration_seconds`** — histogram for latency SLOs.
- **`er_api_rate_limit_rejections_total`** — labels: `bucket` (`global`, `ingest`, `login`, `mutation`). Increments when `@fastify/rate-limit` returns 429 (or 403 when ban applies) for that policy. Use alongside `status_code="429"` on the HTTP counter to alert on abuse vs. organic load.
### Domain gauges (refreshed when **`GET /api/error-monitoring/stats`** runs)