From 289ec4755e6bcda77830a25c958c19fb1f5af7e2 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 16 Aug 2019 13:42:27 +0200 Subject: [PATCH] Adds "extra fields" but at process scope (#1424) * Adds "extra fields" but at process scope fixes gh-1421 * Added changes following the review * Added changes following the review * Added changes following the review --- docs/src/main/asciidoc/spring-cloud-sleuth.adoc | 5 +++-- .../cloud/sleuth/autoconfig/SleuthProperties.java | 14 ++++++++++++++ .../sleuth/autoconfig/TraceAutoConfiguration.java | 5 +++++ .../cloud/sleuth/log/Slf4jScopeDecorator.java | 15 +++++++++++++++ .../cloud/sleuth/log/Slf4JSpanLoggerTest.java | 7 ++++++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index 1c8ddf154..f7a095efc 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -446,9 +446,10 @@ String countryCode = ExtraFieldPropagation.get(span.context(), "x-country-code") ``` IMPORTANT: A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. -There are two properties to achieve this. +There are the following properties to achieve this. With the `spring.sleuth.baggage-keys`, you set keys that get prefixed with `baggage-` for HTTP calls and `baggage_` for messaging. -You can also use the `spring.sleuth.propagation-keys` property to pass a list of prefixed keys that are whitelisted without any prefix. +You can also use the `spring.sleuth.propagation-keys` property to pass a list of prefixed keys that are propagated to remote services without any prefix. +You can also use the `spring.sleuth.local-keys` property to pass a list keys that will be propagated locally but will not be propagated over the wire. 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 diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthProperties.java index 13e64d7ad..4cf24a467 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthProperties.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/SleuthProperties.java @@ -62,6 +62,13 @@ public class SleuthProperties { */ private List propagationKeys = new ArrayList<>(); + /** + * Same as {@link #propagationKeys} except that this field is not propagated to remote services. + * + * @see brave.propagation.ExtraFieldPropagation.FactoryBuilder#addRedactedField(String) + */ + private List localKeys = new ArrayList<>(); + public boolean isEnabled() { return this.enabled; } @@ -102,4 +109,11 @@ public class SleuthProperties { this.propagationKeys = propagationKeys; } + public List getLocalKeys() { + return this.localKeys; + } + + public void setLocalKeys(List localKeys) { + this.localKeys = localKeys; + } } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java index 2859765b7..fe5f45820 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java @@ -155,6 +155,11 @@ public class TraceAutoConfiguration { factoryBuilder = factoryBuilder.addField(key); } } + if (!sleuthProperties.getLocalKeys().isEmpty()) { + for (String key : sleuthProperties.getLocalKeys()) { + factoryBuilder = factoryBuilder.addRedactedField(key); + } + } return factoryBuilder.build(); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java index 87eb87b4f..e1dca9a80 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jScopeDecorator.java @@ -114,6 +114,9 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator { for (String key : whitelistedPropagationKeysWithValue(currentSpan)) { MDC.put(key, ExtraFieldPropagation.get(currentSpan, key)); } + for (String key : whitelistedLocalKeysWithValue(currentSpan)) { + MDC.put(key, ExtraFieldPropagation.get(currentSpan, key)); + } } else { MDC.remove("traceId"); @@ -130,6 +133,9 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator { for (String s : whitelistedPropagationKeys()) { MDC.remove(s); } + for (String s : whitelistedLocalKeys()) { + MDC.remove(s); + } previousMdc.clear(); } @@ -165,6 +171,7 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator { List> previousMdc = new ArrayList<>(); List keys = new ArrayList<>(whitelistedBaggageKeys()); keys.addAll(whitelistedPropagationKeys()); + keys.addAll(whitelistedLocalKeys()); for (String key : keys) { previousMdc.add(new AbstractMap.SimpleEntry<>(key, MDC.get(key))); } @@ -207,10 +214,18 @@ final class Slf4jScopeDecorator implements CurrentTraceContext.ScopeDecorator { return whitelistedKeys(this.sleuthProperties.getPropagationKeys()); } + private List whitelistedLocalKeys() { + return whitelistedKeys(this.sleuthProperties.getLocalKeys()); + } + private List whitelistedPropagationKeysWithValue(TraceContext context) { return whitelistedKeysWithValue(context, whitelistedPropagationKeys()); } + private List whitelistedLocalKeysWithValue(TraceContext context) { + return whitelistedKeysWithValue(context, whitelistedLocalKeys()); + } + private void log(String text, TraceContext span) { if (span == null) { return; diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/log/Slf4JSpanLoggerTest.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/log/Slf4JSpanLoggerTest.java index 5bae5e6b6..3cd1b5d7b 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/log/Slf4JSpanLoggerTest.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/log/Slf4JSpanLoggerTest.java @@ -41,7 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = { "spring.sleuth.baggage-keys=my-baggage", "spring.sleuth.propagation-keys=my-propagation", - "spring.sleuth.log.slf4j.whitelisted-mdc-keys=my-baggage,my-propagation" }) + "spring.sleuth.local-keys=my-local", + "spring.sleuth.log.slf4j.whitelisted-mdc-keys=my-baggage,my-propagation,my-local" }) @SpringBootConfiguration @EnableAutoConfiguration public class Slf4JSpanLoggerTest { @@ -81,16 +82,20 @@ public class Slf4JSpanLoggerTest { ExtraFieldPropagation.set(this.span.context(), "my-baggage", "my-value"); ExtraFieldPropagation.set(this.span.context(), "my-propagation", "my-propagation-value"); + ExtraFieldPropagation.set(this.span.context(), "my-local", + "my-local-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"); + assertThat(MDC.get("my-local")).isEqualTo("my-local-value"); scope.close(); assertThat(MDC.get("my-baggage")).isNullOrEmpty(); assertThat(MDC.get("my-propagation")).isNullOrEmpty(); + assertThat(MDC.get("my-local")).isNullOrEmpty(); } @Test