Commit e8eda53
feat(observability): production-readiness PR 4 — request tracing + JSON logs + structured errors
Fourth of 5 production-readiness PRs. Closes the missing-MDC, hot-path
health probe, MCP error leak, and structured-logging gaps.
Why
---
Pre-PR-4 every `MDC.get("request_id")` call across BearerAuthFilter,
RateLimitFilter, GraphController, and GlobalExceptionHandler returned
null — the four consumers all generated synthetic UUIDs that never
correlated to the same request. The /actuator/health/readiness probe
ran a Cypher count() on every probe (k8s default ~1Hz). MCP tools
returned flat `{error: "..."}` strings with no correlation field. Logs
were plaintext `%msg%n` — unparseable by Loki/Splunk.
Changes
-------
* **`RequestIdFilter` (new)** — outermost in the security chain.
Populates `MDC.request_id` per request, echoes back in
`X-Request-Id` response header, allow-list validates inbound
([A-Za-z0-9_-]{8,64}), clears MDC in finally to prevent leak
across pooled threads (Tomcat platform + virtual-thread carriers).
* **JSON-structured logging** in serving profile via
`logstash-logback-encoder` 9.0 (MIT). One JSON event per log line
with ts/level/logger/thread/msg/stack + all MDC entries +
`application: codeiq` tag. Indexing/CLI profiles keep plaintext.
* **`GraphHealthIndicator` 30s TTL cache** via
`AtomicReference<CachedHealth>` (lock-free). One underlying
count() per 30s regardless of probe rate. Error response sanitized
— `e.getMessage()` no longer surfaces to the permitAll endpoint
(CodeQL CWE-209 again); only `error_class` + log line.
* **Liveness/readiness groups** — `graphHealthIndicator` on
readiness only. Pre-PR-4 it flapped the pod (k8s killing) on
graph-down instead of just routing away.
* **`/actuator/prometheus`** — `micrometer-registry-prometheus`
added; exposed under bearer auth (NOT permitAll — full metrics
tree is reconnaissance). Application tag `codeiq` for multi-pod
scraping. Step 10s.
* **Structured MCP error envelope** — `errorEnvelope(code, e)`
helper returns `{code, message, request_id, error}` (legacy
`error` preserved for backwards-compat). Codes: INTERNAL_ERROR,
INVALID_INPUT, FILE_READ_FAILED, SERIALIZATION_FAILED. Full
exception logged server-side; sanitized envelope to client.
`readFile` no longer concatenates `e.getMessage()` (CWE-209).
Test coverage
-------------
* New `RequestIdFilterTest` — 7 cases (UUID generation, header
pass-through, control-char rejection, length bounds, MDC
clear-in-finally including throw path).
* `GraphHealthIndicatorTest` — added cache-hit assertion (3 calls
→ 1 underlying `count()`); updated for sanitized error fields.
* `McpToolsTest#readFileShouldHandleMissingFile` — updated for new
envelope contract (asserts `code: FILE_READ_FAILED`).
* Full suite: 3680 / 0F / 0E / 32S.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent e40350f commit e8eda53
12 files changed
Lines changed: 580 additions & 63 deletions
File tree
- src
- main
- java/io/github/randomcodespace/iq
- config/security
- health
- mcp
- resources
- test/java/io/github/randomcodespace/iq
- config/security
- health
- mcp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
416 | 475 | | |
417 | 476 | | |
418 | 477 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
455 | 462 | | |
456 | 463 | | |
457 | 464 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
173 | 191 | | |
174 | 192 | | |
175 | 193 | | |
| |||
Lines changed: 81 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
Lines changed: 11 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | | - | |
73 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
74 | 80 | | |
75 | | - | |
| 81 | + | |
76 | 82 | | |
77 | 83 | | |
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
| 88 | + | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
| |||
0 commit comments