Moves Slf4jScopeDecorator to use CorrelationField internally (#1597)

This commit is contained in:
Adrian Cole
2020-04-03 19:57:37 +08:00
committed by GitHub
parent 50ebf7b5c7
commit c20f6c6a11
5 changed files with 40 additions and 20 deletions

View File

@@ -33,7 +33,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<brave.version>5.11.0</brave.version>
<brave.version>5.11.1</brave.version>
<okhttp.version>3.14.6</okhttp.version>
</properties>

View File

@@ -264,7 +264,7 @@
<spring-cloud-stream.version>Horsham.SR3</spring-cloud-stream.version>
<spring-cloud-netflix.version>2.2.3.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.2.3.BUILD-SNAPSHOT</spring-cloud-openfeign.version>
<brave.version>5.11.0</brave.version>
<brave.version>5.11.1</brave.version>
<spring-security-boot-autoconfigure.version>2.1.7.RELEASE</spring-security-boot-autoconfigure.version>
<spring-cloud-aws.version>2.2.1.RELEASE</spring-cloud-aws.version>
<disable.nohttp.checks>false</disable.nohttp.checks>

View File

@@ -22,6 +22,7 @@ import java.util.TreeSet;
import brave.baggage.BaggageField;
import brave.baggage.BaggageFields;
import brave.baggage.CorrelationField;
import brave.baggage.CorrelationScopeDecorator;
import brave.context.slf4j.MDCScopeDecorator;
import brave.propagation.CurrentTraceContext.Scope;
@@ -45,10 +46,16 @@ final class Slf4jScopeDecorator implements ScopeDecorator {
// Backward compatibility for all logging patterns
private static final ScopeDecorator LEGACY_IDS = MDCScopeDecorator.newBuilder()
.clear().addField(BaggageFields.TRACE_ID, "X-B3-TraceId")
.addField(BaggageFields.PARENT_ID, "X-B3-ParentSpanId")
.addField(BaggageFields.SPAN_ID, "X-B3-SpanId")
.addField(BaggageFields.SAMPLED, "X-Span-Export").build();
.clear()
.addField(CorrelationField.newBuilder(BaggageFields.TRACE_ID)
.name("X-B3-TraceId").build())
.addField(CorrelationField.newBuilder(BaggageFields.PARENT_ID)
.name("X-B3-ParentSpanId").build())
.addField(CorrelationField.newBuilder(BaggageFields.SPAN_ID)
.name("X-B3-SpanId").build())
.addField(CorrelationField.newBuilder(BaggageFields.SAMPLED)
.name("X-Span-Export").build())
.build();
private final ScopeDecorator delegate;
@@ -56,25 +63,28 @@ final class Slf4jScopeDecorator implements ScopeDecorator {
SleuthSlf4jProperties sleuthSlf4jProperties) {
CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder().clear()
.addField(BaggageFields.TRACE_ID).addField(BaggageFields.PARENT_ID)
.addField(BaggageFields.SPAN_ID)
.addField(BaggageFields.SAMPLED, "spanExportable");
.addField(CorrelationField.create(BaggageFields.TRACE_ID))
.addField(CorrelationField.create(BaggageFields.PARENT_ID))
.addField(CorrelationField.create(BaggageFields.SPAN_ID))
.addField(CorrelationField.newBuilder(BaggageFields.SAMPLED)
.name("spanExportable").build());
Set<String> whitelist = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
whitelist.addAll(sleuthSlf4jProperties.getWhitelistedMdcKeys());
// Note: we are adding all the keys as-is because correlation context doesn't
// prefix, only ExtraFieldPropagation does
Set<String> retained = new LinkedHashSet<>();
retained.addAll(sleuthProperties.getBaggageKeys());
retained.addAll(sleuthProperties.getLocalKeys());
retained.addAll(sleuthProperties.getPropagationKeys());
retained.retainAll(whitelist);
// For backwards compatibility set all fields dirty, so that any changes made by
// MDC directly are reverted.
for (String name : retained) {
if (whitelist.contains(name)) {
// Until we move off ExtraFieldPropagation onto BaggagePropagation,
// manually create the fields...
builder.addField(BaggageField.create(name));
builder.addDirtyName(name);
}
builder.addField(CorrelationField.newBuilder(BaggageField.create(name))
.dirty().build());
}
this.delegate = builder.build();

View File

@@ -18,8 +18,10 @@ package org.springframework.cloud.sleuth.log;
import brave.Span;
import brave.Tracer;
import brave.baggage.CorrelationField;
import brave.propagation.CurrentTraceContext.Scope;
import brave.propagation.ExtraFieldPropagation;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -37,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Marcin Grzejszczak
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = {
"spring.sleuth.baggage-keys=my-baggage",
"spring.sleuth.baggage-keys=my-baggage,my-baggage-two",
"spring.sleuth.propagation-keys=my-propagation",
"spring.sleuth.local-keys=my-local",
"spring.sleuth.log.slf4j.whitelisted-mdc-keys=my-baggage,my-propagation,my-local" })
@@ -170,8 +172,16 @@ public class Slf4JSpanLoggerTest {
}
@Test
public void should_pick_previous_mdc_entries_when_their_keys_are_whitelisted()
throws Exception {
public void should_only_include_whitelist() {
assertThat(this.slf4jScopeDecorator).extracting("delegate.fields")
.asInstanceOf(InstanceOfAssertFactories.array(CorrelationField[].class))
.extracting(CorrelationField::name).containsExactly("traceId", "parentId",
"spanId", "spanExportable", "my-baggage", "my-local",
"my-propagation"); // my-baggage-two is baggage not in the whitelist
}
@Test
public void should_pick_previous_mdc_entries_when_their_keys_are_whitelisted() {
MDC.put("my-baggage", "A1");
MDC.put("my-propagation", "B1");

View File

@@ -31,8 +31,8 @@
<name>spring-cloud-sleuth-dependencies</name>
<description>Spring Cloud Sleuth Dependencies</description>
<properties>
<brave.version>5.11.0</brave.version>
<brave.opentracing.version>0.36.0</brave.opentracing.version>
<brave.version>5.11.1</brave.version>
<brave.opentracing.version>0.36.1</brave.opentracing.version>
<grpc.spring.boot.version>3.4.1</grpc.spring.boot.version>
</properties>
<dependencyManagement>