Observability with logs, correlation IDs, metrics, health & error tracking
The three pillars (logs, metrics, traces), structured logging, MDC correlation ids, Actuator health, liveness and readiness probes, Prometheus + Grafana, SLOs, and Sentry with PII scrubbing.
The three pillars
| Pillar | Answers | Tools |
|---|---|---|
| Logs | What happened in this request? | SLF4J + Logback/Log4j2, CloudWatch, Loki, ELK |
| Metrics | How is the system doing over time? | Micrometer → Prometheus → Grafana |
| Traces | Where did this request spend its time, across services? | Micrometer Tracing / OpenTelemetry → Tempo, Jaeger, Zipkin |
Error tracking (Sentry) and health checks are close relatives.
Correlation IDs with MDC
@Component @Order(Ordered.HIGHEST_PRECEDENCE)
class CorrelationIdFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws ServletException, IOException {
String id = Optional.ofNullable(req.getHeader("X-Correlation-Id"))
.filter(v -> v.matches("[A-Za-z0-9-]{8,64}")) // don't trust arbitrary input
.orElse(UUID.randomUUID().toString());
MDC.put("correlationId", id);
res.setHeader("X-Correlation-Id", id);
try { chain.doFilter(req, res); } finally { MDC.remove("correlationId"); }
}
}
logging.pattern.correlation: "[%X{correlationId:-}] "
Every log line for that request now carries the id, the error response includes it, and support can search by it.
Health: liveness vs readiness
| Probe | Question | If failing |
|---|---|---|
Liveness /actuator/health/liveness |
Is the process stuck or broken? | Restart it |
Readiness /actuator/health/readiness |
Can it take traffic right now (DB up, warmed)? | Remove from the load balancer, but don’t restart |
Don’t put external dependencies (SMTP, a third-party API) into liveness checks. An outage there would restart-loop your app.
In your projects
shared/web/CorrelationIdFilter.java · ApiError.correlationId · Actuator (health, info; show-details: never)The MDC correlation id is shown in every log line and every error response, and exposed via CORS. Actuator exposes only health and info, with details hidden, so nothing about internals leaks publicly.
log4j2.xml · Actuator (health, info, metrics, prometheus) · SentryConfig · docker-compose Prometheus + Grafana- Log4j2 rolling file (
logs/app.log, gzipped daily). - Prometheus metrics with histograms and SLO buckets (e.g. p95 latency under 500 ms).
- Sentry with a
BeforeSendscrubber andsend-default-pii: false. - Tomcat RemoteIp trusts Docker proxies so logs show real client IPs.
application-prod.yaml (management port 8081) · log4j2.xml · awslogs · Sentry · ScheduledJobRecorder- Actuator on a separate management port (8081), so health and metrics aren’t exposed through the public tunnel.
- Console logs in ISO format, shipped to CloudWatch.
- Sentry with PII scrubbing.
- Noisy STOMP logs silenced.
- Background-job runs are recorded and visible to admins.
@Slf4j tagged messages: [RATE_LIMIT] [WS] [PUSH] [SWEEPER] [AUDIT]Tagged log prefixes make grepping easy without a full logging platform. The next step is structured JSON logs with fields.
What to measure (RED + USE)
- RED for services: Rate (requests/s), Errors (5xx rate), Duration (p50, p95, p99 latency).
- USE for resources: Utilisation, Saturation, Errors (CPU, Hikari pool, queues).
- Business metrics: bookings per hour, journals posted, failed payouts, DLQ depth.
- SLO: “99.5% of requests under 500 ms over 30 days”. The error budget is the 0.5% you’re allowed to miss.
Related concepts you must know
- Structured logging (JSON). Spring Boot 3.4+ has
logging.structured.format.console=ecs|logstash. - Log levels. ERROR wakes someone up, WARN is unusual but handled, INFO marks business milestones, DEBUG is for dev.
- Never log secrets, tokens, full card numbers, passwords or OTPs.
- Distributed tracing with OpenTelemetry becomes essential once you have more than one service (bonus module).
- Alerting. Alert on symptoms (error rate, latency), not causes (CPU 80%).