Skip to content

Commit 7807cff

Browse files
committed
Guard ZipkinTracingAutoConfiguration against missing Encoding bean
This commit harmonizes the check for the presence of an Encoding bean. Also, as this bean may be provided by another auto-configuration, this commit orders them explicitly. This means the dependency to spring-boot-zipkin has to elevate from a test-only dependency to (at least) an optional dependency to validate the links between the two auto-configurations. Closes gh-46295
1 parent fd96784 commit 7807cff

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

module/spring-boot-tracing/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
optional(project(":core:spring-boot-testcontainers"))
3838
optional(project(":module:spring-boot-actuator-autoconfigure"))
3939
optional(project(":module:spring-boot-metrics"))
40+
optional(project(":module:spring-boot-zipkin"))
4041
optional("io.micrometer:micrometer-core")
4142
optional("io.micrometer:micrometer-tracing-bridge-brave")
4243
optional("io.micrometer:micrometer-tracing-bridge-otel")
@@ -55,7 +56,6 @@ dependencies {
5556
testImplementation(project(":core:spring-boot-test"))
5657
testImplementation(project(":module:spring-boot-opentelemetry"))
5758
testImplementation(project(":test-support:spring-boot-test-support"))
58-
testImplementation(project(":module:spring-boot-zipkin"))
5959
testImplementation("com.squareup.okhttp3:mockwebserver")
6060
testImplementation("io.micrometer:micrometer-registry-prometheus")
6161
testImplementation("io.opentelemetry:opentelemetry-exporter-common")

module/spring-boot-tracing/src/main/java/org/springframework/boot/tracing/autoconfigure/zipkin/ZipkinTracingAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* @author Phillip Webb
5151
* @since 4.0.0
5252
*/
53-
@AutoConfiguration
53+
@AutoConfiguration(afterName = "org.springframework.boot.zipkin.autoconfigure.ZipkinAutoConfiguration")
5454
@ConditionalOnClass(Encoding.class)
5555
@Import({ BraveConfiguration.class, OpenTelemetryConfiguration.class })
5656
public class ZipkinTracingAutoConfiguration {
@@ -60,6 +60,7 @@ public class ZipkinTracingAutoConfiguration {
6060
static class BraveConfiguration {
6161

6262
@Bean
63+
@ConditionalOnBean(Encoding.class)
6364
@ConditionalOnMissingBean(value = MutableSpan.class, parameterizedContainer = BytesEncoder.class)
6465
BytesEncoder<MutableSpan> mutableSpanBytesEncoder(Encoding encoding,
6566
ObjectProvider<Tag<Throwable>> throwableTagProvider) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-present 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.boot.tracing.autoconfigure.zipkin;
18+
19+
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
20+
import io.opentelemetry.sdk.trace.export.SpanExporter;
21+
import org.junit.jupiter.api.Test;
22+
import zipkin2.reporter.BytesEncoder;
23+
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.boot.zipkin.autoconfigure.ZipkinAutoConfiguration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link ZipkinTracingAutoConfiguration}.
32+
*
33+
* @author Stephane Nicoll
34+
*/
35+
class ZipkinTracingAutoConfigurationTests {
36+
37+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38+
.withConfiguration(AutoConfigurations.of(ZipkinTracingAutoConfiguration.class));
39+
40+
@Test
41+
void shouldNotSupplyBeansIfInfrastructureIsNotAvailable() {
42+
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(BytesEncoder.class)
43+
.doesNotHaveBean(SpanExporter.class)
44+
.doesNotHaveBean(ZipkinSpanExporter.class));
45+
}
46+
47+
@Test
48+
void shouldSupplyBeansIfInfrastructureIsAvailable() {
49+
this.contextRunner.withConfiguration(AutoConfigurations.of(ZipkinAutoConfiguration.class)).run((context) -> {
50+
assertThat(context).hasSingleBean(SpanExporter.class);
51+
assertThat(context).hasSingleBean(ZipkinSpanExporter.class);
52+
});
53+
}
54+
55+
@Test
56+
void shouldNotSupplyBeansIfTracingIsDisabled() {
57+
this.contextRunner.withPropertyValues("management.tracing.enabled=false")
58+
.withConfiguration(AutoConfigurations.of(ZipkinAutoConfiguration.class))
59+
.run((context) -> {
60+
assertThat(context).doesNotHaveBean(SpanExporter.class);
61+
assertThat(context).doesNotHaveBean(ZipkinSpanExporter.class);
62+
});
63+
}
64+
65+
}

0 commit comments

Comments
 (0)