Skip to content

Commit 5c9f8a7

Browse files
Fixes the case where the sender is overriden with the predefined name; fixes gh-1637
1 parent fcee4fd commit 5c9f8a7

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin2/ZipkinBackwardsCompatibilityAutoConfiguration.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ Reporter<Span> reporter(ReporterMetrics reporterMetrics, ZipkinProperties zipkin
9898
DefaultListableBeanFactory beanFactory) {
9999
List<String> beanNames = new ArrayList<>(
100100
Arrays.asList(beanFactory.getBeanNamesForType(Sender.class)));
101-
beanNames.remove(ZipkinAutoConfiguration.SENDER_BEAN_NAME);
101+
if (beanNames.size() != 1
102+
|| !beanNames.contains(ZipkinAutoConfiguration.SENDER_BEAN_NAME)) {
103+
beanNames.remove(ZipkinAutoConfiguration.SENDER_BEAN_NAME);
104+
}
102105
Sender sender = (Sender) beanFactory.getBean(beanNames.get(0));
103106
// historical constraint. Note: AsyncReporter supports memory bounds
104107
return AsyncReporter.builder(sender).queuedMaxSpans(1000)
@@ -145,18 +148,23 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
145148
context.getBeanFactory());
146149
DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) context
147150
.getBeanFactory();
148-
int foundSenders = listableBeanFactory
149-
.getBeanNamesForType(Sender.class).length;
151+
String[] foundSenders = listableBeanFactory.getBeanNamesForType(Sender.class);
152+
int foundSendersSize = foundSenders.length;
150153

151154
// Previously we supported 1 Sender bean at a time
152155
// which could be overridden by another auto-configuration.
153156
// Now we support both the overridden bean and our default zipkinSender bean.
154157
// Since this config is adapting the old config we're searching for exactly 1
155158
// `Sender` bean before `ZipkinAutoConfiguration` kicks in.
156-
if (foundSenders != 1) {
159+
if (foundSendersSize != 1) {
157160
return ConditionOutcome.noMatch(
158161
"None or multiple Sender beans found - no reason to apply backwards compatibility");
159162
}
163+
else if (foundSenders[0].equals(ZipkinAutoConfiguration.SENDER_BEAN_NAME)) {
164+
return ConditionOutcome.noMatch("A single, ["
165+
+ ZipkinAutoConfiguration.SENDER_BEAN_NAME
166+
+ "] named bean found - no reason to apply backwards compatibility");
167+
}
160168
int foundReporters = listableBeanFactory
161169
.getBeanNamesForType(Reporter.class).length;
162170
// Check if we need to provide a Reporter bean for the overridden Sender bean

spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin2/ZipkinBackwardsCompatibilityAutoConfigurationTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616

1717
package org.springframework.cloud.sleuth.zipkin2;
1818

19+
import java.util.List;
20+
1921
import org.junit.Test;
22+
import zipkin2.Call;
2023
import zipkin2.codec.BytesEncoder;
24+
import zipkin2.codec.Encoding;
25+
import zipkin2.reporter.AsyncReporter;
2126
import zipkin2.reporter.InMemoryReporterMetrics;
2227
import zipkin2.reporter.Reporter;
2328
import zipkin2.reporter.ReporterMetrics;
29+
import zipkin2.reporter.Sender;
2430

2531
import org.springframework.boot.autoconfigure.AutoConfigurations;
2632
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2733
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Configuration;
2836

2937
import static org.assertj.core.api.Assertions.assertThat;
3038

@@ -71,4 +79,50 @@ public void shouldNotLoadBackwardsCompatibilityConfigWhenSleuthDisabled() {
7179
});
7280
}
7381

82+
@Test
83+
public void shouldAllowOverridingSenderOnlyWithoutOverridingTheReporter() {
84+
this.contextRunner.withUserConfiguration(MyConfig.class).run(context -> {
85+
assertThat(context.getBean(ZipkinProperties.class)).isNotNull();
86+
assertThat(context.getBean(Reporter.class)).isInstanceOf(AsyncReporter.class);
87+
assertThat(context.getBean(Sender.class)).isInstanceOf(MySender.class);
88+
assertThat(context.getBean(BytesEncoder.class)).isNotNull();
89+
assertThat(context.getBean(ReporterMetrics.class))
90+
.isInstanceOf(InMemoryReporterMetrics.class);
91+
});
92+
}
93+
94+
@Configuration
95+
protected static class MyConfig {
96+
97+
@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME)
98+
Sender mySender() {
99+
return new MySender();
100+
}
101+
102+
}
103+
104+
static class MySender extends Sender {
105+
106+
@Override
107+
public Encoding encoding() {
108+
return Encoding.JSON;
109+
}
110+
111+
@Override
112+
public int messageMaxBytes() {
113+
return Integer.MAX_VALUE;
114+
}
115+
116+
@Override
117+
public int messageSizeInBytes(List<byte[]> encodedSpans) {
118+
return encoding().listSizeInBytes(encodedSpans);
119+
}
120+
121+
@Override
122+
public Call<Void> sendSpans(List<byte[]> encodedSpans) {
123+
return Call.create(null);
124+
}
125+
126+
}
127+
74128
}

0 commit comments

Comments
 (0)