Clearing MDC for null context; fixes gh-1161
This commit is contained in:
@@ -77,8 +77,8 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
|||||||
final String legacyPreviousSpanId = MDC.get(LEGACY_SPAN_ID_NAME);
|
final String legacyPreviousSpanId = MDC.get(LEGACY_SPAN_ID_NAME);
|
||||||
final String legacySpanExportable = MDC.get(LEGACY_EXPORTABLE_NAME);
|
final String legacySpanExportable = MDC.get(LEGACY_EXPORTABLE_NAME);
|
||||||
final List<AbstractMap.SimpleEntry<String, String>> previousMdc = Stream
|
final List<AbstractMap.SimpleEntry<String, String>> previousMdc = Stream
|
||||||
.concat(whitelistedBaggageKeys(currentSpan),
|
.concat(whitelistedBaggageKeysWithValue(currentSpan),
|
||||||
whitelistedPropagationKeys(currentSpan))
|
whitelistedPropagationKeysWithValue(currentSpan))
|
||||||
.map((s) -> new AbstractMap.SimpleEntry<>(s, MDC.get(s)))
|
.map((s) -> new AbstractMap.SimpleEntry<>(s, MDC.get(s)))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
@@ -102,9 +102,9 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
|||||||
log.trace("With parent: {}", currentSpan.parentId());
|
log.trace("With parent: {}", currentSpan.parentId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
whitelistedBaggageKeys(currentSpan).forEach(
|
whitelistedBaggageKeysWithValue(currentSpan).forEach(
|
||||||
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
||||||
whitelistedPropagationKeys(currentSpan).forEach(
|
whitelistedPropagationKeysWithValue(currentSpan).forEach(
|
||||||
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
(s) -> MDC.put(s, ExtraFieldPropagation.get(currentSpan, s)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -116,8 +116,8 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
|||||||
MDC.remove(LEGACY_PARENT_ID_NAME);
|
MDC.remove(LEGACY_PARENT_ID_NAME);
|
||||||
MDC.remove(LEGACY_SPAN_ID_NAME);
|
MDC.remove(LEGACY_SPAN_ID_NAME);
|
||||||
MDC.remove(LEGACY_EXPORTABLE_NAME);
|
MDC.remove(LEGACY_EXPORTABLE_NAME);
|
||||||
whitelistedBaggageKeys(currentSpan).forEach(MDC::remove);
|
whitelistedBaggageKeys().forEach(MDC::remove);
|
||||||
whitelistedPropagationKeys(currentSpan).forEach(MDC::remove);
|
whitelistedPropagationKeys().forEach(MDC::remove);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,18 +146,30 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator {
|
|||||||
return new ThreadContextCurrentTraceContextScope();
|
return new ThreadContextCurrentTraceContextScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Stream<String> whitelistedBaggageKeys(TraceContext context) {
|
private Stream<String> whitelistedBaggageKeys() {
|
||||||
return this.sleuthProperties.getBaggageKeys().stream().filter(
|
return this.sleuthProperties.getBaggageKeys().stream().filter(
|
||||||
(s) -> this.sleuthSlf4jProperties.getWhitelistedMdcKeys().contains(s)
|
(s) -> this.sleuthSlf4jProperties.getWhitelistedMdcKeys().contains(s));
|
||||||
&& context != null
|
|
||||||
&& StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Stream<String> whitelistedPropagationKeys(TraceContext context) {
|
private Stream<String> whitelistedBaggageKeysWithValue(TraceContext context) {
|
||||||
|
if (context == null) {
|
||||||
|
return Stream.empty();
|
||||||
|
}
|
||||||
|
return whitelistedBaggageKeys().filter(
|
||||||
|
(s) -> StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> whitelistedPropagationKeys() {
|
||||||
return this.sleuthProperties.getPropagationKeys().stream().filter(
|
return this.sleuthProperties.getPropagationKeys().stream().filter(
|
||||||
(s) -> this.sleuthSlf4jProperties.getWhitelistedMdcKeys().contains(s)
|
(s) -> this.sleuthSlf4jProperties.getWhitelistedMdcKeys().contains(s));
|
||||||
&& context != null
|
}
|
||||||
&& StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
|
||||||
|
private Stream<String> whitelistedPropagationKeysWithValue(TraceContext context) {
|
||||||
|
if (context == null) {
|
||||||
|
return Stream.empty();
|
||||||
|
}
|
||||||
|
return whitelistedPropagationKeys().filter(
|
||||||
|
(s) -> StringUtils.hasText(ExtraFieldPropagation.get(context, s)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void log(String text, TraceContext span) {
|
private void log(String text, TraceContext span) {
|
||||||
|
|||||||
@@ -92,6 +92,26 @@ public class Slf4JSpanLoggerTest {
|
|||||||
assertThat(MDC.get("my-propagation")).isNullOrEmpty();
|
assertThat(MDC.get("my-propagation")).isNullOrEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_remove_entries_from_mdc_for_null_span() throws Exception {
|
||||||
|
ExtraFieldPropagation.set(this.span.context(), "my-baggage", "my-value");
|
||||||
|
ExtraFieldPropagation.set(this.span.context(), "my-propagation",
|
||||||
|
"my-propagation-value");
|
||||||
|
this.slf4jScopeDecorator.decorateScope(this.span.context(), () -> {
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThat(MDC.get("my-baggage")).isEqualTo("my-value");
|
||||||
|
assertThat(MDC.get("my-propagation")).isEqualTo("my-propagation-value");
|
||||||
|
|
||||||
|
Scope scope = this.slf4jScopeDecorator.decorateScope(null, () -> {
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.close();
|
||||||
|
|
||||||
|
assertThat(MDC.get("my-baggage")).isNullOrEmpty();
|
||||||
|
assertThat(MDC.get("my-propagation")).isNullOrEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_remove_entries_from_mdc_from_null_span() throws Exception {
|
public void should_remove_entries_from_mdc_from_null_span() throws Exception {
|
||||||
MDC.put("X-B3-TraceId", "A");
|
MDC.put("X-B3-TraceId", "A");
|
||||||
|
|||||||
Reference in New Issue
Block a user