Video in progress
How do you link log entries for a single request across different services?
Write the trace ID (or a correlation ID) in every log line. In Spring Boot 3, Micrometer Tracing puts it into the MDC, and you can output it in the log pattern; then you search by that ID in the central log system.
📖 Answer
Without a trace ID: a user says "an error came up at 14:32". The logs of 6 services have thousands of lines in that minute, and there's no way to tell which one is their request.
When tracing is set up, traceId and spanId are in the MDC (Mapped Diagnostic Context). Adding them to the log format:
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
The result:
INFO [order-service,4bf92f3577b34da6,00f067aa0ba902b7] Order 812 created
WARN [payment-service,4bf92f3577b34da6,b9c7c989f97918e1] Card declined
Now if you search for 4bf92f3577b34da6 in Kibana, Loki or another log system, the whole story of the request comes up.
A practical tip: return the trace ID in the error response too (for example, an X-Trace-Id header or in the error body). A support person can ask the user for it and immediately find the right log.
Sign up to mark studied questions and save your progress.