Fixes the way span adjusters are injected

without this you can't start the context when seuth is disabled
with this change we can autowire an empty list

fixes #600
This commit is contained in:
Marcin Grzejszczak
2017-06-01 14:11:26 +02:00
parent 95e14c0a68
commit bf27382ca8
6 changed files with 214 additions and 134 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.sleuth.stream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@@ -67,6 +68,8 @@ import org.springframework.scheduling.support.PeriodicTrigger;
@ConditionalOnProperty(value = "spring.sleuth.stream.enabled", matchIfMissing = true)
public class SleuthStreamAutoConfiguration {
@Autowired(required = false) List<SpanAdjuster> spanAdjusters = new ArrayList<>();
@Bean
@ConditionalOnMissingBean
public Sampler defaultTraceSampler(SamplerProperties config) {
@@ -82,9 +85,9 @@ public class SleuthStreamAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public StreamSpanReporter sleuthStreamSpanReporter(HostLocator endpointLocator,
SpanMetricReporter spanMetricReporter, Environment environment,
List<SpanAdjuster> spanAdjusters) {
return new StreamSpanReporter(endpointLocator, spanMetricReporter, environment, spanAdjusters);
SpanMetricReporter spanMetricReporter, Environment environment) {
return new StreamSpanReporter(endpointLocator, spanMetricReporter, environment,
this.spanAdjusters);
}
@Bean(name = StreamSpanReporter.POLLER)

View File

@@ -15,80 +15,79 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Matcin Wielgus
*/
public class DiscoveryClientEndpointLocatorConfigurationTest {
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
EmptyConfiguration.class).run("--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(ServerPropertiesHostLocator.class);
}
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
EmptyConfiguration.class).run("--spring.jmx.enabled=false",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(ServerPropertiesHostLocator.class);
}
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class)
.run("--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(ServerPropertiesHostLocator.class);
}
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(ServerPropertiesHostLocator.class);
}
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocator() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithCustomLocator.class)
.run("--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
}
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocator() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
}
}
@Test
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run(
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(DiscoveryClientHostLocator.class);
}
}
@Test
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isInstanceOf(DiscoveryClientHostLocator.class);
}
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class,
ConfigurationWithCustomLocator.class).run(
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
}
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
try (ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class,
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true",
"--spring.main.web_environment=false")) {
assertThat(ctxt.getBean(HostLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
}
}
@Configuration
@EnableAutoConfiguration
public static class EmptyConfiguration {
}
@Configuration
@EnableAutoConfiguration
public static class EmptyConfiguration {
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithDiscoveryClient {
@Bean
public DiscoveryClient getDiscoveryClient() {
return Mockito.mock(DiscoveryClient.class);
}
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithDiscoveryClient {
@Bean public DiscoveryClient getDiscoveryClient() {
return Mockito.mock(DiscoveryClient.class);
}
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithCustomLocator {
static HostLocator locator = Mockito.mock(HostLocator.class);
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithCustomLocator {
static HostLocator locator = Mockito.mock(HostLocator.class);
@Bean
public HostLocator getEndpointLocator() {
return locator;
}
}
@Bean public HostLocator getEndpointLocator() {
return locator;
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2013-2017 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
*
* http://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.stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = StreamWithDisabledSleuthTests.Config.class)
@TestPropertySource(properties = "spring.sleuth.enabled=false")
public class StreamWithDisabledSleuthTests {
@Test public void shouldStartContext() {
}
@EnableAutoConfiguration
static class Config {
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.sleuth.zipkin;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@@ -62,6 +63,8 @@ import org.springframework.web.client.RestTemplate;
@AutoConfigureBefore(TraceAutoConfiguration.class)
public class ZipkinAutoConfiguration {
@Autowired(required = false) List<SpanAdjuster> spanAdjusters = new ArrayList<>();
@Bean
@ConditionalOnMissingBean
public ZipkinSpanReporter reporter(SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin,
@@ -86,8 +89,8 @@ public class ZipkinAutoConfiguration {
@Bean
public SpanReporter zipkinSpanListener(ZipkinSpanReporter reporter, EndpointLocator endpointLocator,
Environment environment, List<SpanAdjuster> spanAdjusters) {
return new ZipkinSpanListener(reporter, endpointLocator, environment, spanAdjusters);
Environment environment) {
return new ZipkinSpanListener(reporter, endpointLocator, environment, this.spanAdjusters);
}
@Configuration

View File

@@ -16,74 +16,75 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class DiscoveryClientEndpointLocatorConfigurationTest {
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
ConfigurableApplicationContext ctxt = new SpringApplication(
EmptyConfiguration.class).run();
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(ServerPropertiesEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocator() {
ConfigurableApplicationContext ctxt = new SpringApplication(
EmptyConfiguration.class).run("--spring.jmx.enabled=false");
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(ServerPropertiesEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run();
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(ServerPropertiesEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldDefaultToServerPropertiesEndpointLocatorEvenWhenDiscoveryClientPresent() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false");
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(ServerPropertiesEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocator() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithCustomLocator.class).run();
assertThat(ctxt.getBean(EndpointLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
ctxt.close();
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocator() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false");
assertThat(ctxt.getBean(EndpointLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
ctxt.close();
}
@Test
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.zipkin.locator.discovery.enabled=true");
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(FallbackHavingEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldBeFallbackHavingEndpointLocatorWhenAskedTo() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true");
assertThat(ctxt.getBean(EndpointLocator.class))
.isInstanceOf(FallbackHavingEndpointLocator.class);
ctxt.close();
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class,ConfigurationWithCustomLocator.class).run("--spring.zipkin.locator.discovery.enabled=true");
assertThat(ctxt.getBean(EndpointLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
ctxt.close();
}
@Test
public void endpointLocatorShouldRespectExistingEndpointLocatorEvenWhenAskedToBeDiscovery() {
ConfigurableApplicationContext ctxt = new SpringApplication(
ConfigurationWithDiscoveryClient.class,
ConfigurationWithCustomLocator.class).run("--spring.jmx.enabled=false",
"--spring.zipkin.locator.discovery.enabled=true");
assertThat(ctxt.getBean(EndpointLocator.class))
.isSameAs(ConfigurationWithCustomLocator.locator);
ctxt.close();
}
@Configuration
@EnableAutoConfiguration
public static class EmptyConfiguration {
}
@Configuration
@EnableAutoConfiguration
public static class EmptyConfiguration {
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithDiscoveryClient {
@Bean
public DiscoveryClient getDiscoveryClient() {
return Mockito.mock(DiscoveryClient.class);
}
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithDiscoveryClient {
@Bean public DiscoveryClient getDiscoveryClient() {
return Mockito.mock(DiscoveryClient.class);
}
}
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithCustomLocator {
static EndpointLocator locator = Mockito.mock(EndpointLocator.class);
@Configuration
@EnableAutoConfiguration
public static class ConfigurationWithCustomLocator {
static EndpointLocator locator = Mockito.mock(EndpointLocator.class);
@Bean
public EndpointLocator getEndpointLocator() {
return locator;
}
}
@Bean public EndpointLocator getEndpointLocator() {
return locator;
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2013-2017 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
*
* http://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.zipkin;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ZipkinWithDisabledSleuthTests.Config.class)
@TestPropertySource(properties = "spring.sleuth.enabled=false")
public class ZipkinWithDisabledSleuthTests {
@Test public void shouldStartContext() {
}
@EnableAutoConfiguration
static class Config {
}
}