Adapt to changes in Brave tagged fields handling
See gh-38724 See gh-37435
This commit is contained in:
parent
0ca55bf0a6
commit
7120dc07ae
|
@ -1,43 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2023 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.actuate.autoconfigure.tracing;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import brave.Tags;
|
|
||||||
import brave.baggage.BaggageField;
|
|
||||||
import brave.handler.MutableSpan;
|
|
||||||
import brave.handler.SpanHandler;
|
|
||||||
import brave.propagation.TraceContext;
|
|
||||||
|
|
||||||
class BaggageTagSpanHandler extends SpanHandler {
|
|
||||||
|
|
||||||
private final Collection<BaggageField> fieldsToTag;
|
|
||||||
|
|
||||||
BaggageTagSpanHandler(Collection<BaggageField> fieldsToTag) {
|
|
||||||
this.fieldsToTag = fieldsToTag;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean end(TraceContext context, MutableSpan span, Cause cause) {
|
|
||||||
for (BaggageField field : this.fieldsToTag) {
|
|
||||||
Tags.BAGGAGE_FIELD.tag(field, context, span);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -70,13 +70,17 @@ import org.springframework.core.env.Environment;
|
||||||
BravePropagationConfigurations.NoPropagation.class })
|
BravePropagationConfigurations.NoPropagation.class })
|
||||||
public class BraveAutoConfiguration {
|
public class BraveAutoConfiguration {
|
||||||
|
|
||||||
static final BraveBaggageManager BRAVE_BAGGAGE_MANAGER = new BraveBaggageManager();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default value for application name if {@code spring.application.name} is not set.
|
* Default value for application name if {@code spring.application.name} is not set.
|
||||||
*/
|
*/
|
||||||
private static final String DEFAULT_APPLICATION_NAME = "application";
|
private static final String DEFAULT_APPLICATION_NAME = "application";
|
||||||
|
|
||||||
|
private final TracingProperties tracingProperties;
|
||||||
|
|
||||||
|
BraveAutoConfiguration(TracingProperties tracingProperties) {
|
||||||
|
this.tracingProperties = tracingProperties;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
@ -88,22 +92,22 @@ public class BraveAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Tracing braveTracing(Environment environment, TracingProperties properties, List<SpanHandler> spanHandlers,
|
Tracing braveTracing(Environment environment, List<SpanHandler> spanHandlers,
|
||||||
List<TracingCustomizer> tracingCustomizers, CurrentTraceContext currentTraceContext,
|
List<TracingCustomizer> tracingCustomizers, CurrentTraceContext currentTraceContext,
|
||||||
Factory propagationFactory, Sampler sampler) {
|
Factory propagationFactory, Sampler sampler) {
|
||||||
if (properties.getBrave().isSpanJoiningSupported()) {
|
if (this.tracingProperties.getBrave().isSpanJoiningSupported()) {
|
||||||
if (properties.getPropagation().getType() != null
|
if (this.tracingProperties.getPropagation().getType() != null
|
||||||
&& properties.getPropagation().getType().contains(PropagationType.W3C)) {
|
&& this.tracingProperties.getPropagation().getType().contains(PropagationType.W3C)) {
|
||||||
throw new IncompatibleConfigurationException("management.tracing.propagation.type",
|
throw new IncompatibleConfigurationException("management.tracing.propagation.type",
|
||||||
"management.tracing.brave.span-joining-supported");
|
"management.tracing.brave.span-joining-supported");
|
||||||
}
|
}
|
||||||
if (properties.getPropagation().getType() == null
|
if (this.tracingProperties.getPropagation().getType() == null
|
||||||
&& properties.getPropagation().getProduce().contains(PropagationType.W3C)) {
|
&& this.tracingProperties.getPropagation().getProduce().contains(PropagationType.W3C)) {
|
||||||
throw new IncompatibleConfigurationException("management.tracing.propagation.produce",
|
throw new IncompatibleConfigurationException("management.tracing.propagation.produce",
|
||||||
"management.tracing.brave.span-joining-supported");
|
"management.tracing.brave.span-joining-supported");
|
||||||
}
|
}
|
||||||
if (properties.getPropagation().getType() == null
|
if (this.tracingProperties.getPropagation().getType() == null
|
||||||
&& properties.getPropagation().getConsume().contains(PropagationType.W3C)) {
|
&& this.tracingProperties.getPropagation().getConsume().contains(PropagationType.W3C)) {
|
||||||
throw new IncompatibleConfigurationException("management.tracing.propagation.consume",
|
throw new IncompatibleConfigurationException("management.tracing.propagation.consume",
|
||||||
"management.tracing.brave.span-joining-supported");
|
"management.tracing.brave.span-joining-supported");
|
||||||
}
|
}
|
||||||
|
@ -112,7 +116,7 @@ public class BraveAutoConfiguration {
|
||||||
Builder builder = Tracing.newBuilder()
|
Builder builder = Tracing.newBuilder()
|
||||||
.currentTraceContext(currentTraceContext)
|
.currentTraceContext(currentTraceContext)
|
||||||
.traceId128Bit(true)
|
.traceId128Bit(true)
|
||||||
.supportsJoin(properties.getBrave().isSpanJoiningSupported())
|
.supportsJoin(this.tracingProperties.getBrave().isSpanJoiningSupported())
|
||||||
.propagationFactory(propagationFactory)
|
.propagationFactory(propagationFactory)
|
||||||
.sampler(sampler)
|
.sampler(sampler)
|
||||||
.localServiceName(applicationName);
|
.localServiceName(applicationName);
|
||||||
|
@ -125,13 +129,13 @@ public class BraveAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public brave.Tracer braveTracer(Tracing tracing) {
|
brave.Tracer braveTracer(Tracing tracing) {
|
||||||
return tracing.tracer();
|
return tracing.tracer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public CurrentTraceContext braveCurrentTraceContext(List<CurrentTraceContext.ScopeDecorator> scopeDecorators,
|
CurrentTraceContext braveCurrentTraceContext(List<CurrentTraceContext.ScopeDecorator> scopeDecorators,
|
||||||
List<CurrentTraceContextCustomizer> currentTraceContextCustomizers) {
|
List<CurrentTraceContextCustomizer> currentTraceContextCustomizers) {
|
||||||
ThreadLocalCurrentTraceContext.Builder builder = ThreadLocalCurrentTraceContext.newBuilder();
|
ThreadLocalCurrentTraceContext.Builder builder = ThreadLocalCurrentTraceContext.newBuilder();
|
||||||
scopeDecorators.forEach(builder::addScopeDecorator);
|
scopeDecorators.forEach(builder::addScopeDecorator);
|
||||||
|
@ -143,14 +147,15 @@ public class BraveAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Sampler braveSampler(TracingProperties properties) {
|
Sampler braveSampler() {
|
||||||
return Sampler.create(properties.getSampling().getProbability());
|
return Sampler.create(this.tracingProperties.getSampling().getProbability());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(io.micrometer.tracing.Tracer.class)
|
@ConditionalOnMissingBean(io.micrometer.tracing.Tracer.class)
|
||||||
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext) {
|
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext) {
|
||||||
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), BRAVE_BAGGAGE_MANAGER);
|
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext),
|
||||||
|
new BraveBaggageManager(this.tracingProperties.getBaggage().getTagFields()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -27,11 +27,11 @@ import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
|
||||||
import brave.baggage.CorrelationScopeCustomizer;
|
import brave.baggage.CorrelationScopeCustomizer;
|
||||||
import brave.baggage.CorrelationScopeDecorator;
|
import brave.baggage.CorrelationScopeDecorator;
|
||||||
import brave.context.slf4j.MDCScopeDecorator;
|
import brave.context.slf4j.MDCScopeDecorator;
|
||||||
import brave.handler.SpanHandler;
|
|
||||||
import brave.propagation.CurrentTraceContext.ScopeDecorator;
|
import brave.propagation.CurrentTraceContext.ScopeDecorator;
|
||||||
import brave.propagation.Propagation;
|
import brave.propagation.Propagation;
|
||||||
import brave.propagation.Propagation.Factory;
|
import brave.propagation.Propagation.Factory;
|
||||||
import brave.propagation.Propagation.KeyFactory;
|
import brave.propagation.Propagation.KeyFactory;
|
||||||
|
import io.micrometer.tracing.brave.bridge.BraveBaggageManager;
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Baggage.Correlation;
|
import org.springframework.boot.actuate.autoconfigure.tracing.TracingProperties.Baggage.Correlation;
|
||||||
|
@ -41,7 +41,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Brave propagation configurations. They are imported by {@link BraveAutoConfiguration}.
|
* Brave propagation configurations. They are imported by {@link BraveAutoConfiguration}.
|
||||||
|
@ -92,7 +91,8 @@ class BravePropagationConfigurations {
|
||||||
baggagePropagationCustomizers.orderedStream()
|
baggagePropagationCustomizers.orderedStream()
|
||||||
.forEach((customizer) -> customizer.customize(throwAwayBuilder));
|
.forEach((customizer) -> customizer.customize(throwAwayBuilder));
|
||||||
CompositePropagationFactory propagationFactory = CompositePropagationFactory.create(
|
CompositePropagationFactory propagationFactory = CompositePropagationFactory.create(
|
||||||
this.tracingProperties.getPropagation(), BraveAutoConfiguration.BRAVE_BAGGAGE_MANAGER,
|
this.tracingProperties.getPropagation(),
|
||||||
|
new BraveBaggageManager(this.tracingProperties.getBaggage().getTagFields()),
|
||||||
LocalBaggageFields.extractFrom(throwAwayBuilder));
|
LocalBaggageFields.extractFrom(throwAwayBuilder));
|
||||||
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(propagationFactory);
|
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(propagationFactory);
|
||||||
throwAwayBuilder.configs().forEach(builder::add);
|
throwAwayBuilder.configs().forEach(builder::add);
|
||||||
|
@ -112,20 +112,12 @@ class BravePropagationConfigurations {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Order(0)
|
|
||||||
BaggagePropagationCustomizer remoteFieldsBaggagePropagationCustomizer() {
|
BaggagePropagationCustomizer remoteFieldsBaggagePropagationCustomizer() {
|
||||||
return (builder) -> {
|
return (builder) -> {
|
||||||
List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields();
|
List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields();
|
||||||
for (String fieldName : remoteFields) {
|
for (String fieldName : remoteFields) {
|
||||||
builder.add(BaggagePropagationConfig.SingleBaggageField.remote(BaggageField.create(fieldName)));
|
builder.add(BaggagePropagationConfig.SingleBaggageField.remote(BaggageField.create(fieldName)));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Order(1)
|
|
||||||
BaggagePropagationCustomizer localFieldsBaggagePropagationCustomizer() {
|
|
||||||
return (builder) -> {
|
|
||||||
List<String> localFields = this.tracingProperties.getBaggage().getLocalFields();
|
List<String> localFields = this.tracingProperties.getBaggage().getLocalFields();
|
||||||
for (String localFieldName : localFields) {
|
for (String localFieldName : localFields) {
|
||||||
builder.add(BaggagePropagationConfig.SingleBaggageField.local(BaggageField.create(localFieldName)));
|
builder.add(BaggagePropagationConfig.SingleBaggageField.local(BaggageField.create(localFieldName)));
|
||||||
|
@ -133,16 +125,6 @@ class BravePropagationConfigurations {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Order(2)
|
|
||||||
SpanHandler baggageTagSpanHandler() {
|
|
||||||
List<String> tagFields = this.tracingProperties.getBaggage().getTagFields();
|
|
||||||
if (CollectionUtils.isEmpty(tagFields)) {
|
|
||||||
return SpanHandler.NOOP;
|
|
||||||
}
|
|
||||||
return new BaggageTagSpanHandler(tagFields.stream().map(BaggageField::create).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnEnabledTracing
|
@ConditionalOnEnabledTracing
|
||||||
|
|
|
@ -26,7 +26,6 @@ import brave.Span;
|
||||||
import brave.SpanCustomizer;
|
import brave.SpanCustomizer;
|
||||||
import brave.Tracer;
|
import brave.Tracer;
|
||||||
import brave.Tracing;
|
import brave.Tracing;
|
||||||
import brave.baggage.BaggageField;
|
|
||||||
import brave.baggage.BaggagePropagation;
|
import brave.baggage.BaggagePropagation;
|
||||||
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
|
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
|
||||||
import brave.handler.SpanHandler;
|
import brave.handler.SpanHandler;
|
||||||
|
@ -345,22 +344,6 @@ class BraveAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldCreateTagHandler() {
|
|
||||||
this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=country-code,bp")
|
|
||||||
.run((context) -> assertThat(context.getBean(BaggageTagSpanHandler.class)).extracting("fieldsToTag")
|
|
||||||
.asInstanceOf(InstanceOfAssertFactories.list(BaggageField.class))
|
|
||||||
.extracting(BaggageField::name)
|
|
||||||
.containsExactlyInAnyOrder("country-code", "bp"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void noopOnNoTagFields() {
|
|
||||||
this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=")
|
|
||||||
.run((context) -> assertThat(context.getBean("baggageTagSpanHandler", SpanHandler.class))
|
|
||||||
.isSameAs(SpanHandler.NOOP));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDisablePropagationIfTracingIsDisabled() {
|
void shouldDisablePropagationIfTracingIsDisabled() {
|
||||||
this.contextRunner.withPropertyValues("management.tracing.enabled=false").run((context) -> {
|
this.contextRunner.withPropertyValues("management.tracing.enabled=false").run((context) -> {
|
||||||
|
@ -371,6 +354,16 @@ class BraveAutoConfigurationTests {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldConfigureTaggedFields() {
|
||||||
|
this.contextRunner.withPropertyValues("management.tracing.baggage.tag-fields=t1").run((context) -> {
|
||||||
|
BraveTracer braveTracer = context.getBean(BraveTracer.class);
|
||||||
|
assertThat(braveTracer).extracting("braveBaggageManager.tagFields")
|
||||||
|
.asInstanceOf(InstanceOfAssertFactories.list(String.class))
|
||||||
|
.containsExactly("t1");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void injectToMap(Map<String, String> map, String key, String value) {
|
private void injectToMap(Map<String, String> map, String key, String value) {
|
||||||
map.put(key, value);
|
map.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue