Files
errors.bitreport.pt/docs/RUNBOOK_INGEST_DEBUG.md
T
Peter Spark 1c65ed3158 ingest-api: allow unknown top-level keys on ingest batch (BIT-933)
The Fastify JSON Schema for POST /v1/ingest/events used
additionalProperties:false on the root object. CRM direct-forward
smokes that attach batch metadata beside events were rejected with
HTTP 400 before the handler.

- ingestJsonSchemas.ts: shared event and batch schemas
- ingestJsonSchemas.test.ts: Ajv regression without MySQL
- integration.twoProjects.test.ts: end-to-end case when DB URL set
- docs/RUNBOOK_INGEST_DEBUG.md: document batch-level forwards

Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376
Paperclip-Issue: BIT-933
Paperclip-Run: f027c017-4cbf-4819-84bb-85922e5c0407
Made-with: Cursor
2026-04-05 04:35:50 +00:00

140 lines
6.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Runbook — debugging ingest (errors.bitreport.pt)
**Audience:** DevOps, on-call, integrators shipping events into the **ingest API**.
**Prerequisites:** `curl`, optional `jq`, a valid per-project ingest key from the vault/admin UI, and network path to `INGEST_API_BASE`.
---
## 0. Deploy smoke: `BIT-SMOKE error hub ingest live check` (wdmi-store / CRM)
**Symptoms:** Hub incident titled like **`BIT-SMOKE error hub ingest live check`** (ex.: [BIT-931](/BIT/issues/BIT-931), `ERRSIG:0dfffa22f758`) — the application’s post-deploy probe could not complete a successful **`POST /v1/ingest/events`** (or treated the response as failure).
**Not the same as** feed/XML upload failures — those hit catalog APIs; see [RUNBOOK_REPORTER_WDMI_FEED_UPLOAD.md](RUNBOOK_REPORTER_WDMI_FEED_UPLOAD.md).
**Checks (in order)**
1. **From the reporter host** (same egress as the app), run [scripts/examples/ingest_smoke.sh](../scripts/examples/ingest_smoke.sh) with production `INGEST_API_BASE` and the **same** ingest key as the app (read token from a file; avoid shell history).
2. **URL:** must be the ingest service base (no path suffix in env) — clients append `/v1/ingest/events` themselves (see [clients/php/ErrorReporter.php](../clients/php/ErrorReporter.php), [HTTP_CONTRACTS.md](HTTP_CONTRACTS.md)).
3. **Credentials:** `Authorization: Bearer <token>` or `X-Api-Key`; key must be **active** and **unexpired** in `er_ingest_keys` (§2).
4. **HTTP codes:** `401` / `429` / `5xx` / timeouts — use sections below; CRM cURL timeouts are short (e.g. 4s) — slow proxies can fail smokes before the hub responds.
5. **HTTP 400 from schema validation** should be rare; the hub ignores unknown **event-level** JSON keys and unknown **top-level** keys on the batch object (only `events` is required). Required per event: `schema_version`, `occurred_at`, `level`, `message`.
**After the root cause is fixed**
```bash
./scripts/er-cli.sh list-open --project wdmi-store --q "ERRSIG:0dfffa22f758"
./scripts/er-cli.sh resolve <unique_id> --note "ingest smoke restored; verified POST /v1/ingest/events from wdmi-store" --assigned-to "oncall"
```
---
## 1. Quick health
```bash
curl -sS "${INGEST_API_BASE}/health"
# expect: {"status":"ok"}
```
If this fails, treat as **process / proxy / TLS** — not application auth.
---
## 2. HTTP 401 — `unauthorized` / missing credentials
**Symptoms:** JSON like `{ "error": "unauthorized", "message": "Missing ingest credentials …" }` or `Invalid or inactive ingest key`.
**Checks**
1. **Header present:** send exactly one of:
- `Authorization: Bearer <plaintext-token>`
- `X-Api-Key: <plaintext-token>`
2. **Key state in DB:** key must be `active`, not revoked, not expired (`er_ingest_keys`). A common mistake is rotating the key in the UI but leaving old tokens in systemd/env on senders.
3. **Wrong environment:** staging token against production base URL (or vice versa).
**Sanitized curl (no token in shell history):** read token from a file.
```bash
curl -sS -X POST "${INGEST_API_BASE}/v1/ingest/events" \
-H "Authorization: Bearer $(cat /run/secrets/er_ingest_token)" \
-H "Content-Type: application/json" \
-d '{"events":[{"schema_version":1,"occurred_at":"2026-04-02T12:00:00.000Z","level":"error","message":"auth probe"}]}'
```
---
## 3. HTTP 429 — rate limits
Two layers:
1. **Anonymous IP** — unauthenticated `POST /v1/ingest/events` from an IP is throttled (`INGEST_ANON_IP_MAX` / `INGEST_ANON_IP_WINDOW_MS`). Response includes **`Retry-After`** (seconds).
2. **Per API key** — after successful auth, limits use `rate_limit_max` / `rate_limit_window_ms` on the key row, else service defaults (`INGEST_RATE_LIMIT_MAX`, `INGEST_RATE_LIMIT_WINDOW_MS`).
**What to do**
- Back off using `Retry-After`.
- Fix misconfigured clients (tight loops, duplicate crons).
- For legitimate bursts, raise per-key limits in DB (or split traffic across keys only if product policy allows).
**Metrics:** `er_ingest_requests_total{status_code="429"}` and histogram `er_ingest_request_duration_seconds` — see [OBSERVABILITY_PROMETHEUS.md](OBSERVABILITY_PROMETHEUS.md).
---
## 4. Events accepted but “wrong” incident / duplicate noise — fingerprints
Fingerprinting uses **message normalization** (first line, NFKC, collapsed whitespace) and **location** from `context` (`file_path` / `file`, `line_number` / `line` / `lineno`, or nested `stack_info`). See `ingest-api` `fingerprintPolicy.ts` and `normalize.ts`.
**Symptoms**
- Same logical bug creates **multiple incidents** because `message` differs slightly (timestamps, IDs in text).
- **Unexpected grouping** because `context.file_path` is missing or varies per host.
**Checks**
1. Compare **raw** `message` strings from the sender; normalize mentally (first line only).
2. Ensure **stable** `context.file_path` (and line) for stack-less errors.
3. Optionally send an explicit **`fingerprint`** field (max 128 chars) for stable dedup when message text is inherently volatile (document this choice per project).
**Verify in hub:** use admin `GET /v1/incidents/:id` or public `recent-errors` with `q=<substring>` after triage key is set.
---
## 5. HTTP 400 — `schema_version` / `occurred_at`
- Only **`schema_version: 1`** is accepted.
- **`occurred_at`** must parse as valid ISO-8601 (see server validation).
```bash
# Bad: will 400
curl -sS -w "\n%{http_code}\n" -X POST "${INGEST_API_BASE}/v1/ingest/events" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"events":[{"schema_version":2,"occurred_at":"not-a-date","level":"e","message":"x"}]}'
```
---
## 6. HTTP 409 — idempotency conflict
When **`Idempotency-Key`** is reused with a **different** JSON body, the API returns **409** `idempotency_conflict`. Use a **new** key or retry the **exact** original payload.
---
## 7. Noise rules — events “disappear”
If the response shows `results[]` entries with `"skipped": true`, the event matched a **noise rule** (`rule_id`, `reason`). This is expected for filtered traffic. Check `noise_filtered_count` and compare with `er_ingest_events_total{outcome="skipped_noise"}` in Prometheus.
---
## 8. OpenAPI / Swagger
For interactive inspection (lab only):
- `GET ${INGEST_API_BASE}/v1/openapi.json`
- Browser: `${INGEST_API_BASE}/v1/docs/ui`
---
## Related
- [HTTP_CONTRACTS.md](HTTP_CONTRACTS.md) — full request/response shapes.
- [RUNBOOK_OPERATIONS.md](RUNBOOK_OPERATIONS.md) — hub-side listing and mutations.