Adds integration of new customizers in Brave
fixes gh-1436
This commit is contained in:
@@ -34,7 +34,7 @@
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring-boot.version>2.2.0.BUILD-SNAPSHOT</spring-boot.version>
|
||||
<brave.version>5.6.9</brave.version>
|
||||
<brave.version>5.7.0</brave.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
@@ -833,6 +833,16 @@ Running the preceding method with a value of `15` leads to setting a tag with a
|
||||
|
||||
== Customizations
|
||||
|
||||
=== Customizers
|
||||
|
||||
With Brave 5.7 you have various options of providing customizers for your project. Brave ships with
|
||||
|
||||
* `TracingCustomizer` - allows configuration plugins to collaborate on building an instance of `Tracing`.
|
||||
* `CurrentTraceContextCustomizer` - allows configuration plugins to collaborate on building an instance of `CurrentTraceContext`.
|
||||
* `ExtraFieldCustomizer` - allows configuration plugins to collaborate on building an instance of `ExtraFieldPropagation.Factory`.
|
||||
|
||||
Sleuth will search for beans of those types and automatically apply customizations.
|
||||
|
||||
=== HTTP
|
||||
|
||||
If a customization of client / server parsing of the HTTP related spans is required,
|
||||
|
||||
@@ -24,9 +24,12 @@ import brave.CurrentSpanCustomizer;
|
||||
import brave.ErrorParser;
|
||||
import brave.Tracer;
|
||||
import brave.Tracing;
|
||||
import brave.TracingCustomizer;
|
||||
import brave.handler.FinishedSpanHandler;
|
||||
import brave.propagation.B3Propagation;
|
||||
import brave.propagation.CurrentTraceContext;
|
||||
import brave.propagation.CurrentTraceContextCustomizer;
|
||||
import brave.propagation.ExtraFieldCustomizer;
|
||||
import brave.propagation.ExtraFieldPropagation;
|
||||
import brave.propagation.Propagation;
|
||||
import brave.propagation.ThreadLocalCurrentTraceContext;
|
||||
@@ -87,6 +90,15 @@ public class TraceAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
ExtraFieldPropagation.FactoryBuilder extraFieldPropagationFactoryBuilder;
|
||||
|
||||
@Autowired(required = false)
|
||||
List<TracingCustomizer> tracingCustomizers = new ArrayList<>();
|
||||
|
||||
@Autowired(required = false)
|
||||
List<CurrentTraceContextCustomizer> currentTraceContextCustomizers = new ArrayList<>();
|
||||
|
||||
@Autowired(required = false)
|
||||
List<ExtraFieldCustomizer> extraFieldCustomizers = new ArrayList<>();
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
// NOTE: stable bean name as might be used outside sleuth
|
||||
@@ -107,6 +119,9 @@ public class TraceAutoConfiguration {
|
||||
for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) {
|
||||
builder.addFinishedSpanHandler(finishedSpanHandlerFactory);
|
||||
}
|
||||
for (TracingCustomizer customizer : this.tracingCustomizers) {
|
||||
customizer.customize(builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@@ -160,6 +175,9 @@ public class TraceAutoConfiguration {
|
||||
factoryBuilder = factoryBuilder.addRedactedField(key);
|
||||
}
|
||||
}
|
||||
for (ExtraFieldCustomizer customizer : this.extraFieldCustomizers) {
|
||||
customizer.customize(factoryBuilder);
|
||||
}
|
||||
return factoryBuilder.build();
|
||||
}
|
||||
|
||||
@@ -168,6 +186,9 @@ public class TraceAutoConfiguration {
|
||||
for (CurrentTraceContext.ScopeDecorator scopeDecorator : this.scopeDecorators) {
|
||||
builder.addScopeDecorator(scopeDecorator);
|
||||
}
|
||||
for (CurrentTraceContextCustomizer customizer : this.currentTraceContextCustomizers) {
|
||||
customizer.customize(builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.instrument.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import brave.ErrorParser;
|
||||
import brave.Tracing;
|
||||
import brave.http.HttpAdapter;
|
||||
@@ -23,7 +26,9 @@ import brave.http.HttpClientParser;
|
||||
import brave.http.HttpSampler;
|
||||
import brave.http.HttpServerParser;
|
||||
import brave.http.HttpTracing;
|
||||
import brave.http.HttpTracingCustomizer;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -51,6 +56,9 @@ public class TraceHttpAutoConfiguration {
|
||||
|
||||
static final int TRACING_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
|
||||
|
||||
@Autowired(required = false)
|
||||
List<HttpTracingCustomizer> httpTracingCustomizers = new ArrayList<>();
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
// NOTE: stable bean name as might be used outside sleuth
|
||||
@@ -60,9 +68,13 @@ public class TraceHttpAutoConfiguration {
|
||||
@Nullable @ServerSampler HttpSampler serverSampler) {
|
||||
HttpSampler combinedSampler = combineUserProvidedSamplerWithSkipPatternSampler(
|
||||
serverSampler, provider);
|
||||
return HttpTracing.newBuilder(tracing).clientParser(clientParser)
|
||||
.serverParser(serverParser).clientSampler(clientSampler)
|
||||
.serverSampler(combinedSampler).build();
|
||||
HttpTracing.Builder builder = HttpTracing.newBuilder(tracing)
|
||||
.clientParser(clientParser).serverParser(serverParser)
|
||||
.clientSampler(clientSampler).serverSampler(combinedSampler);
|
||||
for (HttpTracingCustomizer customizer : this.httpTracingCustomizers) {
|
||||
customizer.customize(builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private HttpSampler combineUserProvidedSamplerWithSkipPatternSampler(
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.cloud.sleuth.autoconfig;
|
||||
|
||||
import brave.TracingCustomizer;
|
||||
import brave.http.HttpTracingCustomizer;
|
||||
import brave.propagation.CurrentTraceContextCustomizer;
|
||||
import brave.propagation.ExtraFieldCustomizer;
|
||||
import brave.sampler.Sampler;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration;
|
||||
import org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
|
||||
public class TraceAutoConfigurationCustomizersTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withPropertyValues("spring.sleuth.baggage-keys=my-baggage")
|
||||
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class,
|
||||
TraceWebAutoConfiguration.class, TraceHttpAutoConfiguration.class))
|
||||
.withUserConfiguration(Customizers.class);
|
||||
|
||||
@Test
|
||||
public void should_apply_customizers() {
|
||||
this.contextRunner.run((context) -> {
|
||||
Customizers bean = context.getBean(Customizers.class);
|
||||
|
||||
shouldApplyCustomizations(bean);
|
||||
shouldNotOverrideTheDefaults(context);
|
||||
});
|
||||
}
|
||||
|
||||
private void shouldNotOverrideTheDefaults(AssertableApplicationContext context) {
|
||||
then(context.getBean(Sampler.class)).isSameAs(Sampler.ALWAYS_SAMPLE);
|
||||
}
|
||||
|
||||
private void shouldApplyCustomizations(Customizers bean) {
|
||||
then(bean.tracingCustomizerApplied).isTrue();
|
||||
then(bean.contextCustomizerApplied).isTrue();
|
||||
then(bean.extraFieldCustomizerApplied).isTrue();
|
||||
then(bean.httpCustomizerApplied).isTrue();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Customizers {
|
||||
|
||||
boolean tracingCustomizerApplied;
|
||||
|
||||
boolean contextCustomizerApplied;
|
||||
|
||||
boolean extraFieldCustomizerApplied;
|
||||
|
||||
boolean httpCustomizerApplied;
|
||||
|
||||
@Bean
|
||||
TracingCustomizer sleuthTracingCustomizer() {
|
||||
return builder -> tracingCustomizerApplied = true;
|
||||
}
|
||||
|
||||
@Bean
|
||||
CurrentTraceContextCustomizer sleuthCurrentTraceContextCustomizer() {
|
||||
return builder -> contextCustomizerApplied = true;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExtraFieldCustomizer sleuthExtraFieldCustomizer() {
|
||||
return builder -> extraFieldCustomizerApplied = true;
|
||||
}
|
||||
|
||||
@Bean
|
||||
HttpTracingCustomizer sleuthHttpTracingCustomizer() {
|
||||
return builder -> httpCustomizerApplied = true;
|
||||
}
|
||||
|
||||
@Bean
|
||||
Sampler sampler() {
|
||||
return Sampler.ALWAYS_SAMPLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,7 @@
|
||||
<name>spring-cloud-sleuth-dependencies</name>
|
||||
<description>Spring Cloud Sleuth Dependencies</description>
|
||||
<properties>
|
||||
<brave.version>5.6.10</brave.version>
|
||||
<brave.version>5.7.0</brave.version>
|
||||
<brave.opentracing.version>0.34.1</brave.opentracing.version>
|
||||
<grpc.spring.boot.version>3.4.1</grpc.spring.boot.version>
|
||||
</properties>
|
||||
|
||||
Reference in New Issue
Block a user