Correlate logs with trace and span IDs while using instrumented client builders for outbound propagation.
Spring tracing and logs: preserve request identity across outbound calls
Correlation is a path, not a tag dump
A receipt request may pass through an API, a pricing service and a worker. With tracing enabled, the logging context can carry trace and span identifiers so an operator can join a log line to the request path. Those identifiers are not a substitute for a stable business receipt ID in an audit record, and the receipt ID should not become a high-cardinality metric label. Metrics labels and async trace handoff require their own controls.
Use managed builders
Spring Boot customizes its injected RestClient.Builder and WebClient.Builder for observation and trace propagation when the required instrumentation is present. Constructing a raw client outside that path may omit propagation, even if incoming requests have trace IDs. The example accepts the managed builder and a configured service base address. Avoid printing authorization headers or customer payloads to compensate for missing trace context; repair the client path instead.
Check a complete hop
Send one request with a test trace context, capture logs on both services and verify the same trace ID appears on the inbound and outbound sides while span IDs differ where work is nested. Repeat through a queued task: a thread-local context may be absent unless the executor captures it. Sampling means an unsampled request can still have correlation identifiers but no stored trace; logs must remain useful without assuming every trace is retained. Client builder wiring is the companion implementation lesson.
Implementation contract
@Bean
RestClient receiptPartnerClient(
RestClient.Builder managedBuilder,
@Value("${receipt.partner-base}") String partnerBase) {
return managedBuilder.baseUrl(partnerBase).build();
}Cost and verification
Trace propagation adds small headers and context work per call; collecting and retaining spans costs more. Sample by an explicit policy, and keep log fields bounded so correlation does not become an uncontrolled data-export path.
Common Mistakes
- Do not construct a raw HTTP client and assume it received Boot's trace customization.
- Do not use customer IDs, tokens or payloads as trace tags or log correlation fields.
- Do not treat a missing stored span as proof that the request never happened.
Read next
Spring Boot tracing across RestClient: construct the client from Boot's builder, Spring async trace context: follow work across executor threads, Spring Boot metrics: bound tag values instead of tracking each receipt, Spring Prometheus scrape endpoint: registry, exposure and label contract, Spring Actuator management port: separate listener, explicit access rule.
Related operations contract
Spring Boot structured logs: stable fields without customer data.
