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
This commit is contained in:
Marcin Grzejszczak
2019-08-16 13:42:27 +02:00
committed by GitHub
parent 40a5333e56
commit 289ec4755e
5 changed files with 43 additions and 3 deletions

View File

@@ -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

View File

@@ -62,6 +62,13 @@ public class SleuthProperties {
*/
private List<String> 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<String> localKeys = new ArrayList<>();
public boolean isEnabled() {
return this.enabled;
}
@@ -102,4 +109,11 @@ public class SleuthProperties {
this.propagationKeys = propagationKeys;
}
public List<String> getLocalKeys() {
return this.localKeys;
}
public void setLocalKeys(List<String> localKeys) {
this.localKeys = localKeys;
}
}

View File

@@ -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();
}

View File

@@ -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<AbstractMap.SimpleEntry<String, String>> previousMdc = new ArrayList<>();
List<String> 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<String> whitelistedLocalKeys() {
return whitelistedKeys(this.sleuthProperties.getLocalKeys());
}
private List<String> whitelistedPropagationKeysWithValue(TraceContext context) {
return whitelistedKeysWithValue(context, whitelistedPropagationKeys());
}
private List<String> whitelistedLocalKeysWithValue(TraceContext context) {
return whitelistedKeysWithValue(context, whitelistedLocalKeys());
}
private void log(String text, TraceContext span) {
if (span == null) {
return;

View File

@@ -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