Skip to content

Commit 3ab6ec2

Browse files
Adds integration of new customizers in Brave
fixes gh-1436
1 parent 6ce2df4 commit 3ab6ec2

File tree

6 files changed

+151
-5
lines changed

6 files changed

+151
-5
lines changed

benchmarks/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<maven.compiler.target>1.8</maven.compiler.target>
3535
<maven.compiler.source>1.8</maven.compiler.source>
3636
<spring-boot.version>2.2.0.BUILD-SNAPSHOT</spring-boot.version>
37-
<brave.version>5.6.9</brave.version>
37+
<brave.version>5.7.0</brave.version>
3838
</properties>
3939

4040
<dependencyManagement>

docs/src/main/asciidoc/spring-cloud-sleuth.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,16 @@ Running the preceding method with a value of `15` leads to setting a tag with a
833833

834834
== Customizations
835835

836+
=== Customizers
837+
838+
With Brave 5.7 you have various options of providing customizers for your project. Brave ships with
839+
840+
* `TracingCustomizer` - allows configuration plugins to collaborate on building an instance of `Tracing`.
841+
* `CurrentTraceContextCustomizer` - allows configuration plugins to collaborate on building an instance of `CurrentTraceContext`.
842+
* `ExtraFieldCustomizer` - allows configuration plugins to collaborate on building an instance of `ExtraFieldPropagation.Factory`.
843+
844+
Sleuth will search for beans of those types and automatically apply customizations.
845+
836846
=== HTTP
837847

838848
If a customization of client / server parsing of the HTTP related spans is required,

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
import brave.ErrorParser;
2525
import brave.Tracer;
2626
import brave.Tracing;
27+
import brave.TracingCustomizer;
2728
import brave.handler.FinishedSpanHandler;
2829
import brave.propagation.B3Propagation;
2930
import brave.propagation.CurrentTraceContext;
31+
import brave.propagation.CurrentTraceContextCustomizer;
32+
import brave.propagation.ExtraFieldCustomizer;
3033
import brave.propagation.ExtraFieldPropagation;
3134
import brave.propagation.Propagation;
3235
import brave.propagation.ThreadLocalCurrentTraceContext;
@@ -87,6 +90,15 @@ public class TraceAutoConfiguration {
8790
@Autowired(required = false)
8891
ExtraFieldPropagation.FactoryBuilder extraFieldPropagationFactoryBuilder;
8992

93+
@Autowired(required = false)
94+
List<TracingCustomizer> tracingCustomizers = new ArrayList<>();
95+
96+
@Autowired(required = false)
97+
List<CurrentTraceContextCustomizer> currentTraceContextCustomizers = new ArrayList<>();
98+
99+
@Autowired(required = false)
100+
List<ExtraFieldCustomizer> extraFieldCustomizers = new ArrayList<>();
101+
90102
@Bean
91103
@ConditionalOnMissingBean
92104
// NOTE: stable bean name as might be used outside sleuth
@@ -107,6 +119,9 @@ Tracing tracing(
107119
for (FinishedSpanHandler finishedSpanHandlerFactory : this.finishedSpanHandlers) {
108120
builder.addFinishedSpanHandler(finishedSpanHandlerFactory);
109121
}
122+
for (TracingCustomizer customizer : this.tracingCustomizers) {
123+
customizer.customize(builder);
124+
}
110125
return builder.build();
111126
}
112127

@@ -160,6 +175,9 @@ Propagation.Factory sleuthPropagation(SleuthProperties sleuthProperties) {
160175
factoryBuilder = factoryBuilder.addRedactedField(key);
161176
}
162177
}
178+
for (ExtraFieldCustomizer customizer : this.extraFieldCustomizers) {
179+
customizer.customize(factoryBuilder);
180+
}
163181
return factoryBuilder.build();
164182
}
165183

@@ -168,6 +186,9 @@ CurrentTraceContext sleuthCurrentTraceContext(CurrentTraceContext.Builder builde
168186
for (CurrentTraceContext.ScopeDecorator scopeDecorator : this.scopeDecorators) {
169187
builder.addScopeDecorator(scopeDecorator);
170188
}
189+
for (CurrentTraceContextCustomizer customizer : this.currentTraceContextCustomizers) {
190+
customizer.customize(builder);
191+
}
171192
return builder.build();
172193
}
173194

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616

1717
package org.springframework.cloud.sleuth.instrument.web;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
import brave.ErrorParser;
2023
import brave.Tracing;
2124
import brave.http.HttpAdapter;
2225
import brave.http.HttpClientParser;
2326
import brave.http.HttpSampler;
2427
import brave.http.HttpServerParser;
2528
import brave.http.HttpTracing;
29+
import brave.http.HttpTracingCustomizer;
2630

31+
import org.springframework.beans.factory.annotation.Autowired;
2732
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2833
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2934
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -51,6 +56,9 @@ public class TraceHttpAutoConfiguration {
5156

5257
static final int TRACING_FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
5358

59+
@Autowired(required = false)
60+
List<HttpTracingCustomizer> httpTracingCustomizers = new ArrayList<>();
61+
5462
@Bean
5563
@ConditionalOnMissingBean
5664
// NOTE: stable bean name as might be used outside sleuth
@@ -60,9 +68,13 @@ HttpTracing httpTracing(Tracing tracing, SkipPatternProvider provider,
6068
@Nullable @ServerSampler HttpSampler serverSampler) {
6169
HttpSampler combinedSampler = combineUserProvidedSamplerWithSkipPatternSampler(
6270
serverSampler, provider);
63-
return HttpTracing.newBuilder(tracing).clientParser(clientParser)
64-
.serverParser(serverParser).clientSampler(clientSampler)
65-
.serverSampler(combinedSampler).build();
71+
HttpTracing.Builder builder = HttpTracing.newBuilder(tracing)
72+
.clientParser(clientParser).serverParser(serverParser)
73+
.clientSampler(clientSampler).serverSampler(combinedSampler);
74+
for (HttpTracingCustomizer customizer : this.httpTracingCustomizers) {
75+
customizer.customize(builder);
76+
}
77+
return builder.build();
6678
}
6779

6880
private HttpSampler combineUserProvidedSamplerWithSkipPatternSampler(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.autoconfig;
18+
19+
import brave.TracingCustomizer;
20+
import brave.http.HttpTracingCustomizer;
21+
import brave.propagation.CurrentTraceContextCustomizer;
22+
import brave.propagation.ExtraFieldCustomizer;
23+
import brave.sampler.Sampler;
24+
import org.junit.Test;
25+
26+
import org.springframework.boot.autoconfigure.AutoConfigurations;
27+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
28+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
29+
import org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration;
30+
import org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
34+
import static org.assertj.core.api.BDDAssertions.then;
35+
36+
public class TraceAutoConfigurationCustomizersTests {
37+
38+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
39+
.withPropertyValues("spring.sleuth.baggage-keys=my-baggage")
40+
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class,
41+
TraceWebAutoConfiguration.class, TraceHttpAutoConfiguration.class))
42+
.withUserConfiguration(Customizers.class);
43+
44+
@Test
45+
public void should_apply_customizers() {
46+
this.contextRunner.run((context) -> {
47+
Customizers bean = context.getBean(Customizers.class);
48+
49+
shouldApplyCustomizations(bean);
50+
shouldNotOverrideTheDefaults(context);
51+
});
52+
}
53+
54+
private void shouldNotOverrideTheDefaults(AssertableApplicationContext context) {
55+
then(context.getBean(Sampler.class)).isSameAs(Sampler.ALWAYS_SAMPLE);
56+
}
57+
58+
private void shouldApplyCustomizations(Customizers bean) {
59+
then(bean.tracingCustomizerApplied).isTrue();
60+
then(bean.contextCustomizerApplied).isTrue();
61+
then(bean.extraFieldCustomizerApplied).isTrue();
62+
then(bean.httpCustomizerApplied).isTrue();
63+
}
64+
65+
@Configuration
66+
static class Customizers {
67+
68+
boolean tracingCustomizerApplied;
69+
70+
boolean contextCustomizerApplied;
71+
72+
boolean extraFieldCustomizerApplied;
73+
74+
boolean httpCustomizerApplied;
75+
76+
@Bean
77+
TracingCustomizer sleuthTracingCustomizer() {
78+
return builder -> tracingCustomizerApplied = true;
79+
}
80+
81+
@Bean
82+
CurrentTraceContextCustomizer sleuthCurrentTraceContextCustomizer() {
83+
return builder -> contextCustomizerApplied = true;
84+
}
85+
86+
@Bean
87+
ExtraFieldCustomizer sleuthExtraFieldCustomizer() {
88+
return builder -> extraFieldCustomizerApplied = true;
89+
}
90+
91+
@Bean
92+
HttpTracingCustomizer sleuthHttpTracingCustomizer() {
93+
return builder -> httpCustomizerApplied = true;
94+
}
95+
96+
@Bean
97+
Sampler sampler() {
98+
return Sampler.ALWAYS_SAMPLE;
99+
}
100+
101+
}
102+
103+
}

spring-cloud-sleuth-dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<name>spring-cloud-sleuth-dependencies</name>
3232
<description>Spring Cloud Sleuth Dependencies</description>
3333
<properties>
34-
<brave.version>5.6.10</brave.version>
34+
<brave.version>5.7.0</brave.version>
3535
<brave.opentracing.version>0.34.1</brave.opentracing.version>
3636
<grpc.spring.boot.version>3.4.1</grpc.spring.boot.version>
3737
</properties>

0 commit comments

Comments
 (0)