How do you approach Metrics, Health Checks, and Monitoring?
Topic: microservices, Observability. Level: Intermediate.
Answer
- **Metrics**: emit time-series (request rate, error rate, latency percentiles, saturation) — the RED (Rate/Errors/Duration) and USE methods — via Prometheus/Micrometer, visualized in Grafana, with alerting on SLOs.
- **Health checks**: expose **liveness** (is the process healthy? restart if not) and **readiness** (can it serve traffic? pull from LB if not) endpoints so orchestrators route correctly.
- **Monitoring/alerting**: alert on symptoms users feel (error rate, latency), not just resources.
```java
// Micrometer metrics + Spring Boot Actuator health probes
meterRegistry.counter("orders.placed", "channel", "web").increment();
Timer.builder("checkout.latency").publishPercentiles(0.5, 0.95, 0.99)
.register(meterRegistry).record(() -> checkout(cart));
// Actuator exposes /actuator/health/liveness and /actuator/health/readiness
// which Kubernetes probes hit to restart or de-route the pod.
```