mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Polish "Allow configuring Micrometer Tracing MDC keys"
Derive logging.pattern.correlation from the configured MDC keys so that log correlation keeps working when the keys are customized, instead of silently rendering a blank correlation field. Reject empty MDC keys and only clear Brave's default correlation fields when the keys have been customized, so that applications using the defaults are unaffected if Brave adds a default field. Replace the tests that asserted on bean wiring with integration tests covering the MDC contents, plus a smoke test for the log output. See gh-50595
This commit is contained in:
+6
-8
@@ -141,14 +141,12 @@ class BravePropagationConfigurations {
|
||||
CorrelationScopeDecorator.Builder mdcCorrelationScopeDecoratorBuilder(
|
||||
ObjectProvider<CorrelationScopeCustomizer> correlationScopeCustomizers) {
|
||||
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.
|
||||
// BravePropagationConfigurationsTests validates the assumption that
|
||||
// the builder only configures the trace/span id by default.
|
||||
.clear()
|
||||
.add(SingleCorrelationField.newBuilder(BaggageFields.TRACE_ID).name(mdc.getTraceIdKey()).build())
|
||||
.add(SingleCorrelationField.newBuilder(BaggageFields.SPAN_ID).name(mdc.getSpanIdKey()).build());
|
||||
CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder();
|
||||
if (mdc.isCustomized()) {
|
||||
builder.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;
|
||||
}
|
||||
|
||||
+13
-22
@@ -26,8 +26,10 @@ import brave.Span;
|
||||
import brave.SpanCustomizer;
|
||||
import brave.Tracer;
|
||||
import brave.Tracing;
|
||||
import brave.baggage.BaggageFields;
|
||||
import brave.baggage.BaggagePropagation;
|
||||
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
|
||||
import brave.context.slf4j.MDCScopeDecorator;
|
||||
import brave.handler.SpanHandler;
|
||||
import brave.propagation.CurrentTraceContext;
|
||||
import brave.propagation.CurrentTraceContext.ScopeDecorator;
|
||||
@@ -258,29 +260,18 @@ class BraveAutoConfigurationTests {
|
||||
.run((context) -> assertThat(context).hasBean("mdcCorrelationScopeDecoratorBuilder"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Guards the assumption behind the {@code clear()} call that
|
||||
* {@code BravePropagationConfigurations} makes when the MDC keys have been
|
||||
* customized: if Brave ever adds another default correlation field, this fails
|
||||
* instead of that field silently disappearing from the MDC of applications using
|
||||
* custom keys.
|
||||
*/
|
||||
@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");
|
||||
});
|
||||
void shouldOnlyHaveTraceIdAndSpanIdCorrelationFieldsByDefaultInBrave() {
|
||||
assertThat(MDCScopeDecorator.newBuilder().configs())
|
||||
.extracting((config) -> ((SingleCorrelationField) config).name())
|
||||
.containsExactlyInAnyOrder(BaggageFields.TRACE_ID.name(), BaggageFields.SPAN_ID.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+23
@@ -24,6 +24,7 @@ import io.micrometer.tracing.Span;
|
||||
import io.micrometer.tracing.Tracer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.slf4j.MDC;
|
||||
@@ -79,6 +80,28 @@ class BraveBaggagePropagationIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUseCustomMdcKeysForTraceAndSpanId() {
|
||||
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(BraveAutoConfiguration.class))
|
||||
.withPropertyValues("management.tracing.mdc.trace-id-key=customTraceId",
|
||||
"management.tracing.mdc.span-id-key=customSpanId")
|
||||
.run((context) -> {
|
||||
Tracer tracer = tracer(context);
|
||||
Span span = createSpan(tracer);
|
||||
try (Tracer.SpanInScope scope = tracer.withSpan(span.start())) {
|
||||
assertMdcValue("customTraceId", span.context().traceId());
|
||||
assertMdcValue("customSpanId", span.context().spanId());
|
||||
assertUnsetMdc("traceId");
|
||||
assertUnsetMdc("spanId");
|
||||
}
|
||||
finally {
|
||||
span.end();
|
||||
}
|
||||
assertUnsetMdc("customTraceId");
|
||||
assertUnsetMdc("customSpanId");
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource
|
||||
void shouldRemoveEntriesFromMdcForNullSpan(AutoConfig autoConfig) {
|
||||
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.micrometer.tracing.brave.autoconfigure;
|
||||
|
||||
import brave.baggage.BaggageFields;
|
||||
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
|
||||
import brave.baggage.CorrelationScopeDecorator;
|
||||
import brave.context.slf4j.MDCScopeDecorator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link BravePropagationConfigurations}.
|
||||
*/
|
||||
class BravePropagationConfigurationsTests {
|
||||
|
||||
/**
|
||||
* Validates the assumption that {@link MDCScopeDecorator#newBuilder()} only
|
||||
* configures traceId and spanId by default. The
|
||||
* {@link BravePropagationConfigurations.PropagationWithBaggage#mdcCorrelationScopeDecoratorBuilder}
|
||||
* method clears the builder's defaults and re-adds only these two fields so their MDC
|
||||
* key names can be customized. If Brave adds new default fields in the future, the
|
||||
* {@code .clear()} call in that method would silently drop them, and this test will
|
||||
* catch that.
|
||||
*/
|
||||
@Test
|
||||
void mdcScopeDecoratorBuilderShouldOnlyHaveTraceIdAndSpanIdByDefault() {
|
||||
CorrelationScopeDecorator.Builder builder = MDCScopeDecorator.newBuilder();
|
||||
assertThat(builder.configs()).extracting((config) -> ((SingleCorrelationField) config).name())
|
||||
.containsExactlyInAnyOrder(BaggageFields.TRACE_ID.name(), BaggageFields.SPAN_ID.name());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user