From 4b62b519d140e89c4db421db22e1c4eebb6a5e8f Mon Sep 17 00:00:00 2001 From: maxxedev <5051664+maxxedev@users.noreply.github.com> Date: Tue, 17 Nov 2020 23:38:30 -0800 Subject: [PATCH] Add doc example for setting extra baggage field to MDC in current context. (#1777) --- docs/src/main/asciidoc/spring-cloud-sleuth.adoc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index 7d494b516..daae86937 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -447,6 +447,22 @@ In order to automatically set the baggage values to Slf4j's MDC, you have to set the `spring.sleuth.log.slf4j.whitelisted-mdc-keys` property with a list of whitelisted baggage and propagation keys. E.g. `spring.sleuth.log.slf4j.whitelisted-mdc-keys=foo` will set the value of the `foo` baggage into MDC. +Note that the extra field is propagated and added to MDC starting with the next downstream trace context. To immediately +add the extra field to MDC in the current trace context, configure the field to flush on update: + +``` +@Bean +ScopeDecorator mdcScopeDecorator() { + BaggageField countryCodeField = BaggageField.create("x-country-code"); + return MDCScopeDecorator.newBuilder() + .clear() + .add(SingleCorrelationField.newBuilder(countryCodeField) + .flushOnUpdate() + .build()) + .build(); +} +``` + IMPORTANT: Remember that adding entries to MDC can drastically decrease the performance of your application! If you want to add the baggage entries as tags, to make it possible to search for spans via the baggage entries, you can set the value of