Skip to content

Commit 6cc3619

Browse files
committed
Require micrometer-tracing-bridge-brave to auto-configure Brave
Closes gh-32502
1 parent fcafd2a commit 6cc3619

File tree

2 files changed

+41
-84
lines changed

2 files changed

+41
-84
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfiguration.java

+24-30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import brave.Tracer;
2122
import brave.Tracing;
2223
import brave.Tracing.Builder;
2324
import brave.TracingCustomizer;
@@ -48,7 +49,6 @@
4849
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4950
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5051
import org.springframework.context.annotation.Bean;
51-
import org.springframework.context.annotation.Configuration;
5252
import org.springframework.core.env.Environment;
5353

5454
/**
@@ -58,7 +58,7 @@
5858
* @since 3.0.0
5959
*/
6060
@AutoConfiguration(before = MicrometerTracingAutoConfiguration.class)
61-
@ConditionalOnClass(brave.Tracer.class)
61+
@ConditionalOnClass({ Tracer.class, BraveTracer.class })
6262
@EnableConfigurationProperties(TracingProperties.class)
6363
@ConditionalOnEnabledTracing
6464
public class BraveAutoConfiguration {
@@ -135,37 +135,31 @@ public HttpClientHandler<HttpClientRequest, HttpClientResponse> httpClientHandle
135135
return HttpClientHandler.create(httpTracing);
136136
}
137137

138-
@Configuration(proxyBeanMethods = false)
139-
@ConditionalOnClass(BraveTracer.class)
140-
static class BraveMicrometer {
141-
142-
@Bean
143-
@ConditionalOnMissingBean
144-
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext,
145-
BraveBaggageManager braveBaggageManager) {
146-
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), braveBaggageManager);
147-
}
148-
149-
@Bean
150-
@ConditionalOnMissingBean
151-
BraveBaggageManager braveBaggageManager() {
152-
return new BraveBaggageManager();
153-
}
138+
@Bean
139+
@ConditionalOnMissingBean
140+
BraveTracer braveTracerBridge(brave.Tracer tracer, CurrentTraceContext currentTraceContext,
141+
BraveBaggageManager braveBaggageManager) {
142+
return new BraveTracer(tracer, new BraveCurrentTraceContext(currentTraceContext), braveBaggageManager);
143+
}
154144

155-
@Bean
156-
@ConditionalOnMissingBean
157-
BraveHttpServerHandler braveHttpServerHandler(
158-
HttpServerHandler<HttpServerRequest, HttpServerResponse> httpServerHandler) {
159-
return new BraveHttpServerHandler(httpServerHandler);
160-
}
145+
@Bean
146+
@ConditionalOnMissingBean
147+
BraveBaggageManager braveBaggageManager() {
148+
return new BraveBaggageManager();
149+
}
161150

162-
@Bean
163-
@ConditionalOnMissingBean
164-
BraveHttpClientHandler braveHttpClientHandler(
165-
HttpClientHandler<HttpClientRequest, HttpClientResponse> httpClientHandler) {
166-
return new BraveHttpClientHandler(httpClientHandler);
167-
}
151+
@Bean
152+
@ConditionalOnMissingBean
153+
BraveHttpServerHandler braveHttpServerHandler(
154+
HttpServerHandler<HttpServerRequest, HttpServerResponse> httpServerHandler) {
155+
return new BraveHttpServerHandler(httpServerHandler);
156+
}
168157

158+
@Bean
159+
@ConditionalOnMissingBean
160+
BraveHttpClientHandler braveHttpClientHandler(
161+
HttpClientHandler<HttpClientRequest, HttpClientResponse> httpClientHandler) {
162+
return new BraveHttpClientHandler(httpClientHandler);
169163
}
170164

171165
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java

+17-54
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class BraveAutoConfigurationTests {
5555
.withConfiguration(AutoConfigurations.of(BraveAutoConfiguration.class));
5656

5757
@Test
58-
void shouldSupplyBraveBeans() {
58+
void shouldSupplyDefaultBeans() {
5959
this.contextRunner.run((context) -> {
6060
assertThat(context).hasSingleBean(Tracing.class);
6161
assertThat(context).hasSingleBean(Tracer.class);
@@ -65,12 +65,16 @@ void shouldSupplyBraveBeans() {
6565
assertThat(context).hasSingleBean(HttpTracing.class);
6666
assertThat(context).hasSingleBean(HttpServerHandler.class);
6767
assertThat(context).hasSingleBean(HttpClientHandler.class);
68+
assertThat(context).hasSingleBean(BraveTracer.class);
69+
assertThat(context).hasSingleBean(BraveBaggageManager.class);
70+
assertThat(context).hasSingleBean(BraveHttpServerHandler.class);
71+
assertThat(context).hasSingleBean(BraveHttpClientHandler.class);
6872
});
6973
}
7074

7175
@Test
72-
void shouldBackOffOnCustomBraveBeans() {
73-
this.contextRunner.withUserConfiguration(CustomBraveConfiguration.class).run((context) -> {
76+
void shouldBackOffOnCustomBeans() {
77+
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
7478
assertThat(context).hasBean("customTracing");
7579
assertThat(context).hasSingleBean(Tracing.class);
7680
assertThat(context).hasBean("customTracer");
@@ -87,22 +91,6 @@ void shouldBackOffOnCustomBraveBeans() {
8791
assertThat(context).hasSingleBean(HttpServerHandler.class);
8892
assertThat(context).hasBean("customHttpClientHandler");
8993
assertThat(context).hasSingleBean(HttpClientHandler.class);
90-
});
91-
}
92-
93-
@Test
94-
void shouldSupplyMicrometerBeans() {
95-
this.contextRunner.run((context) -> {
96-
assertThat(context).hasSingleBean(BraveTracer.class);
97-
assertThat(context).hasSingleBean(BraveBaggageManager.class);
98-
assertThat(context).hasSingleBean(BraveHttpServerHandler.class);
99-
assertThat(context).hasSingleBean(BraveHttpClientHandler.class);
100-
});
101-
}
102-
103-
@Test
104-
void shouldBackOffOnCustomMicrometerBraveBeans() {
105-
this.contextRunner.withUserConfiguration(CustomMicrometerBraveConfiguration.class).run((context) -> {
10694
assertThat(context).hasBean("customBraveTracer");
10795
assertThat(context).hasSingleBean(BraveTracer.class);
10896
assertThat(context).hasBean("customBraveBaggageManager");
@@ -115,45 +103,25 @@ void shouldBackOffOnCustomMicrometerBraveBeans() {
115103
}
116104

117105
@Test
118-
void shouldNotSupplyBraveBeansIfBraveIsMissing() {
119-
this.contextRunner.withClassLoader(new FilteredClassLoader("brave")).run((context) -> {
120-
assertThat(context).doesNotHaveBean(Tracing.class);
121-
assertThat(context).doesNotHaveBean(Tracer.class);
122-
assertThat(context).doesNotHaveBean(CurrentTraceContext.class);
123-
assertThat(context).doesNotHaveBean(Factory.class);
124-
assertThat(context).doesNotHaveBean(Sampler.class);
125-
assertThat(context).doesNotHaveBean(HttpTracing.class);
126-
assertThat(context).doesNotHaveBean(HttpServerHandler.class);
127-
assertThat(context).doesNotHaveBean(HttpClientHandler.class);
128-
});
106+
void shouldNotSupplyBeansIfBraveIsMissing() {
107+
this.contextRunner.withClassLoader(new FilteredClassLoader("brave"))
108+
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
129109
}
130110

131111
@Test
132-
void shouldNotSupplyMicrometerBeansIfMicrometerIsMissing() {
133-
this.contextRunner.withClassLoader(new FilteredClassLoader("io.micrometer")).run((context) -> {
134-
assertThat(context).doesNotHaveBean(BraveTracer.class);
135-
assertThat(context).doesNotHaveBean(BraveBaggageManager.class);
136-
assertThat(context).doesNotHaveBean(BraveHttpServerHandler.class);
137-
assertThat(context).doesNotHaveBean(BraveHttpClientHandler.class);
138-
});
112+
void shouldNotSupplyBeansIfMicrometerIsMissing() {
113+
this.contextRunner.withClassLoader(new FilteredClassLoader("brave"))
114+
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
139115
}
140116

141117
@Test
142-
void shouldNotSupplyBraveBeansIfTracingIsDisabled() {
143-
this.contextRunner.withPropertyValues("management.tracing.enabled=false").run((context) -> {
144-
assertThat(context).doesNotHaveBean(Tracing.class);
145-
assertThat(context).doesNotHaveBean(Tracer.class);
146-
assertThat(context).doesNotHaveBean(CurrentTraceContext.class);
147-
assertThat(context).doesNotHaveBean(Factory.class);
148-
assertThat(context).doesNotHaveBean(Sampler.class);
149-
assertThat(context).doesNotHaveBean(HttpTracing.class);
150-
assertThat(context).doesNotHaveBean(HttpServerHandler.class);
151-
assertThat(context).doesNotHaveBean(HttpClientHandler.class);
152-
});
118+
void shouldNotSupplyBeansIfTracingIsDisabled() {
119+
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
120+
.run((context) -> assertThat(context).doesNotHaveBean(BraveAutoConfiguration.class));
153121
}
154122

155123
@Configuration(proxyBeanMethods = false)
156-
private static class CustomBraveConfiguration {
124+
private static class CustomConfiguration {
157125

158126
@Bean
159127
Tracing customTracing() {
@@ -197,11 +165,6 @@ HttpClientHandler<HttpClientRequest, HttpClientResponse> customHttpClientHandler
197165
return HttpClientHandler.create(httpTracing);
198166
}
199167

200-
}
201-
202-
@Configuration(proxyBeanMethods = false)
203-
private static class CustomMicrometerBraveConfiguration {
204-
205168
@Bean
206169
BraveTracer customBraveTracer() {
207170
return mock(BraveTracer.class);

0 commit comments

Comments
 (0)