Polish "Allow configuring Micrometer Tracing MDC keys"

Derive logging.pattern.correlation from the configured MDC keys so that
log correlation keeps working when the keys are customized, instead of
silently rendering a blank correlation field.

Reject empty MDC keys and only clear Brave's default correlation fields
when the keys have been customized, so that applications using the
defaults are unaffected if Brave adds a default field.

Replace the tests that asserted on bean wiring with integration tests
covering the MDC contents, plus a smoke test for the log output.

See gh-50595
This commit is contained in:
Moritz Halbritter
2026-08-25 13:54:00 +02:00
parent dc2274e5b6
commit 3f7cebd802
13 changed files with 296 additions and 105 deletions
@@ -75,7 +75,14 @@ If you are using Micrometer Tracing, Spring Boot will include correlation IDs in
The default correlation ID is built from `traceId` and `spanId` https://logback.qos.ch/manual/mdc.html[MDC] values.
For example, if Micrometer Tracing has added an MDC `traceId` of `803B448A0489F84084905D3093480352` and an MDC `spanId` of `3425F23BB2432450` the log output will include the correlation ID `[803B448A0489F84084905D3093480352-3425F23BB2432450]`.
The `traceId` and `spanId` MDC key names can be customized using the configprop:management.tracing.mdc.trace-id-key[] and configprop:management.tracing.mdc.span-id-key[] properties.
The default correlation ID format follows those keys automatically, padding them to 32 and 16 characters respectively, so log correlation keeps working without further configuration.
NOTE: With Brave, the MDC key names are only applied when baggage is enabled.
Setting configprop:management.tracing.baggage.enabled[] to `false` stops Brave from writing the trace and span IDs to the MDC altogether, which disables log correlation.
If you prefer to use a different format for your correlation ID, you can use the configprop:logging.pattern.correlation[] property to define one.
A value that you set yourself takes precedence over the format derived from the MDC key names, so it has to reference any customized key names itself.
For example, the following will provide a correlation ID for Logback in format previously used by Spring Cloud Sleuth:
[configprops,yaml]