Exercise — Distributed Tracing: propagate a correlation id across two services.
Topic: microservices, Observability. Level: Intermediate.
Answer
Generate a correlation/trace id at the entry point if absent, store it in the logging MDC, and forward it on outbound calls. The downstream reads the same header and reuses it, so logs from both services can be joined by one id.
```java
// Inbound filter: adopt or create the id, put it in MDC (shows in every log line)
String cid = Optional.ofNullable(req.getHeader("X-Correlation-Id"))
.orElse(UUID.randomUUID().toString());
MDC.put("correlationId", cid);
// Outbound: forward it so the next service continues the same trace
restClient.get().uri("http://inventory/stock/{s}", sku)
.header("X-Correlation-Id", MDC.get("correlationId"))
.retrieve();
```