Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;

import brave.baggage.BaggageField;
import brave.baggage.BaggageFields;
import brave.baggage.BaggagePropagation;
import brave.baggage.BaggagePropagation.FactoryBuilder;
import brave.baggage.BaggagePropagationConfig;
Expand All @@ -40,6 +41,7 @@
import org.springframework.boot.micrometer.tracing.autoconfigure.ConditionalOnEnabledTracingExport;
import org.springframework.boot.micrometer.tracing.autoconfigure.TracingProperties;
import org.springframework.boot.micrometer.tracing.autoconfigure.TracingProperties.Baggage.Correlation;
import org.springframework.boot.micrometer.tracing.autoconfigure.TracingProperties.Mdc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -138,7 +140,13 @@ Factory propagationFactory(BaggagePropagation.FactoryBuilder factoryBuilder) {
@ConditionalOnMissingBean
CorrelationScopeDecorator.Builder mdcCorrelationScopeDecoratorBuilder(
ObjectProvider<CorrelationScopeCustomizer> correlationScopeCustomizers) {
CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder();
Mdc mdc = this.tracingProperties.getMdc();
CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder()
// Clear existing traceId/spanId backage field mappings
// so the MDC key names can be customized below.
.clear()
.add(SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID).name(mdc.getTraceIdKey()).build())
.add(SingleCorrelationField.newBuilder(BaggageFields.SPAN_ID).name(mdc.getSpanIdKey()).build());
correlationScopeCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,31 @@ void shouldSupplyMdcCorrelationScopeDecoratorIfBaggageCorrelationDisabled() {
.run((context) -> assertThat(context).hasBean("mdcCorrelationScopeDecoratorBuilder"));
}

@Test
void correlationScopeDecoratorUsesDefaultMdcKeys() {
this.contextRunner.run((context) -> {
ScopeDecorator scopeDecorator = context.getBean(ScopeDecorator.class);
assertThat(scopeDecorator)
.extracting("fields", InstanceOfAssertFactories.array(SingleCorrelationField[].class))
.extracting(SingleCorrelationField::name)
.containsExactly("traceId", "spanId");
});
}

@Test
void correlationScopeDecoratorUsesCustomMdcKeys() {
this.contextRunner
.withPropertyValues("management.tracing.mdc.trace-id-key=customTraceId",
"management.tracing.mdc.span-id-key=customSpanId")
.run((context) -> {
ScopeDecorator scopeDecorator = context.getBean(ScopeDecorator.class);
assertThat(scopeDecorator)
.extracting("fields", InstanceOfAssertFactories.array(SingleCorrelationField[].class))
.extracting(SingleCorrelationField::name)
.containsExactly("customTraceId", "customSpanId");
});
}

@Test
void shouldHave128BitTraceId() {
this.contextRunner.run((context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.springframework.boot.micrometer.tracing.autoconfigure.MicrometerTracingAutoConfiguration;
import org.springframework.boot.micrometer.tracing.autoconfigure.NoopTracerAutoConfiguration;
import org.springframework.boot.micrometer.tracing.autoconfigure.TracingProperties;
import org.springframework.boot.micrometer.tracing.autoconfigure.TracingProperties.Mdc;
import org.springframework.boot.micrometer.tracing.opentelemetry.autoconfigure.OpenTelemetryPropagationConfigurations.NoPropagation;
import org.springframework.boot.micrometer.tracing.opentelemetry.autoconfigure.OpenTelemetryPropagationConfigurations.PropagationWithBaggage;
import org.springframework.boot.micrometer.tracing.opentelemetry.autoconfigure.OpenTelemetryPropagationConfigurations.PropagationWithoutBaggage;
Expand Down Expand Up @@ -219,7 +220,8 @@ OtelCurrentTraceContext otelCurrentTraceContext() {
@Bean
@ConditionalOnMissingBean
Slf4JEventListener otelSlf4JEventListener() {
return new Slf4JEventListener();
Mdc mdc = this.tracingProperties.getMdc();
return new Slf4JEventListener(mdc.getTraceIdKey(), mdc.getSpanIdKey());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ void samplerCanBeSetToParentBasedTraceIdRatio() {
});
}

@Test
void slf4jEventListenerUsesDefaultMdcKeys() {
this.contextRunner.run((context) -> {
Slf4JEventListener listener = context.getBean(Slf4JEventListener.class);
assertThat(listener).hasFieldOrPropertyWithValue("traceIdKey", "traceId")
.hasFieldOrPropertyWithValue("spanIdKey", "spanId");
});
}

@Test
void slf4jEventListenerUsesCustomMdcKeys() {
this.contextRunner
.withPropertyValues("management.tracing.mdc.trace-id-key=customTraceId",
"management.tracing.mdc.span-id-key=customSpanId")
.run((context) -> {
Slf4JEventListener listener = context.getBean(Slf4JEventListener.class);
assertThat(listener).hasFieldOrPropertyWithValue("traceIdKey", "customTraceId")
.hasFieldOrPropertyWithValue("spanIdKey", "customSpanId");
});
}

@ParameterizedTest
@ValueSource(strings = { "io.micrometer.tracing.otel", "io.opentelemetry.sdk", "io.opentelemetry.api" })
void shouldNotSupplyBeansIfDependencyIsMissing(String packageName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class TracingProperties {
*/
private final Exemplars exemplars = new Exemplars();

/**
* {@code org.slf4j.MDC} key configuration for trace and span IDs.
*/
private final Mdc mdc = new Mdc();

public Sampling getSampling() {
return this.sampling;
}
Expand All @@ -69,6 +74,10 @@ public Exemplars getExemplars() {
return this.exemplars;
}

public Mdc getMdc() {
return this.mdc;
}

public static class Sampling {

/**
Expand Down Expand Up @@ -258,6 +267,39 @@ public enum PropagationType {

}

/**
* {@code org.slf4j.MDC} key configuration for trace and span IDs.
*/
public static class Mdc {

/**
* Key under which the trace ID is added to the {@code org.slf4j.MDC}.
*/
private String traceIdKey = "traceId";

/**
* Key under which the span ID is added to the {@code org.slf4j.MDC}.
*/
private String spanIdKey = "spanId";

public String getTraceIdKey() {
return this.traceIdKey;
}

public void setTraceIdKey(String traceIdKey) {
this.traceIdKey = traceIdKey;
}

public String getSpanIdKey() {
return this.spanIdKey;
}

public void setSpanIdKey(String spanIdKey) {
this.spanIdKey = spanIdKey;
}

}

/**
* Exemplars configuration.
*/
Expand Down