Added propagation keys to list of whitelisted mdc keys
fixes gh-1135
This commit is contained in:
@@ -452,8 +452,9 @@ Notice that there's no `x-` in front of the header keys.
|
||||
|
||||
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 keys. E.g. `spring.sleuth.log.slf4j.whitelisted-mdc-keys=foo` will set the value of the
|
||||
`foo` baggage into MDC.
|
||||
baggage and propagation keys. E.g. `spring.sleuth.log.slf4j.whitelisted-mdc-keys=foo` will set the value of the `foo` baggage into MDC.
|
||||
|
||||
IMPORTANT: Remember that adding entries to MDC can drastically decrease the performance of your application!
|
||||
|
||||
==== Extracting a Propagated Context
|
||||
|
||||
|
||||
@@ -76,9 +76,11 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
||||
final String legacyPreviousParentId = MDC.get(LEGACY_PARENT_ID_NAME);
|
||||
final String legacyPreviousSpanId = MDC.get(LEGACY_SPAN_ID_NAME);
|
||||
final String legacySpanExportable = MDC.get(LEGACY_EXPORTABLE_NAME);
|
||||
final List<AbstractMap.SimpleEntry<String, String>> previousMdc = whitelistedBaggageKeys(
|
||||
currentSpan).map((s) -> new AbstractMap.SimpleEntry<>(s, MDC.get(s)))
|
||||
.collect(Collectors.toList());
|
||||
final List<AbstractMap.SimpleEntry<String, String>> previousMdc = Stream
|
||||
.concat(whitelistedBaggageKeys(currentSpan),
|
||||
whitelistedPropagationKeys(currentSpan))
|
||||
.map((s) -> new AbstractMap.SimpleEntry<>(s, MDC.get(s)))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (currentSpan != null) {
|
||||
String traceIdString = currentSpan.traceIdString();
|
||||
@@ -102,6 +104,8 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
||||
}
|
||||
whitelistedBaggageKeys(currentSpan).forEach(
|
||||
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
||||
whitelistedPropagationKeys(currentSpan).forEach(
|
||||
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
||||
}
|
||||
else {
|
||||
MDC.remove("traceId");
|
||||
@@ -113,6 +117,7 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
||||
MDC.remove(LEGACY_SPAN_ID_NAME);
|
||||
MDC.remove(LEGACY_EXPORTABLE_NAME);
|
||||
whitelistedBaggageKeys(currentSpan).forEach(MDC::remove);
|
||||
whitelistedPropagationKeys(currentSpan).forEach(MDC::remove);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,6 +153,13 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
||||
&& StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
||||
}
|
||||
|
||||
private Stream<String> whitelistedPropagationKeys(TraceContext context) {
|
||||
return this.sleuthProperties.getPropagationKeys().stream().filter(
|
||||
(s) -> this.sleuthSlf4jProperties.getWhitelistedMdcKeys().contains(s)
|
||||
&& context != null
|
||||
&& StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
||||
}
|
||||
|
||||
private void log(String text, TraceContext span) {
|
||||
if (span == null) {
|
||||
return;
|
||||
|
||||
@@ -39,7 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = {
|
||||
"spring.sleuth.baggage-keys=my-baggage",
|
||||
"spring.sleuth.log.slf4j.whitelisted-mdc-keys=my-baggage" })
|
||||
"spring.sleuth.propagation-keys=my-propagation",
|
||||
"spring.sleuth.log.slf4j.whitelisted-mdc-keys=my-baggage,my-propagation" })
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
public class Slf4JSpanLoggerTest {
|
||||
@@ -77,14 +78,18 @@ public class Slf4JSpanLoggerTest {
|
||||
@Test
|
||||
public void should_set_entries_to_mdc_from_span_with_baggage() throws Exception {
|
||||
ExtraFieldPropagation.set(this.span.context(), "my-baggage", "my-value");
|
||||
ExtraFieldPropagation.set(this.span.context(), "my-propagation",
|
||||
"my-propagation-value");
|
||||
Scope scope = this.slf4jScopeDecorator.decorateScope(this.span.context(), () -> {
|
||||
});
|
||||
|
||||
assertThat(MDC.get("my-baggage")).isEqualTo("my-value");
|
||||
assertThat(MDC.get("my-propagation")).isEqualTo("my-propagation-value");
|
||||
|
||||
scope.close();
|
||||
|
||||
assertThat(MDC.get("my-baggage")).isNullOrEmpty();
|
||||
assertThat(MDC.get("my-propagation")).isNullOrEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user