From 6ddd0094981ccab323c0e560bd857604df64cbd4 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 2 Jan 2017 14:39:26 +0100 Subject: [PATCH] Added test for non web apps, fixed the autoconfig in that case without this change the non web apps can't start with this change the missing bean gets registered also in terms of non boot apps if there is no port or address set we're providing some default values. In terms of the service name one can always use the override via the spring.zipkin.service.name property fixes #32 --- .../web/TraceHttpAutoConfiguration.java | 60 ++++++++++++++++ .../web/TraceWebAutoConfiguration.java | 35 ++-------- .../main/resources/META-INF/spring.factories | 1 + .../web/TraceNoWebEnvironmentTests.java | 68 +++++++++++++++++++ 4 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java create mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceNoWebEnvironmentTests.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java new file mode 100644 index 000000000..0d5f20bac --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java @@ -0,0 +1,60 @@ +/* + * Copyright 2013-2016 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.instrument.web; + +import java.util.regex.Pattern; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.sleuth.TraceKeys; +import org.springframework.cloud.sleuth.Tracer; +import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} + * related to HTTP based communication. + * + * @author Marcin Grzejszczak + * @since 1.0.12 + */ +@Configuration +@ConditionalOnBean(Tracer.class) +@AutoConfigureAfter(TraceAutoConfiguration.class) +@EnableConfigurationProperties({ TraceKeys.class, SleuthWebProperties.class }) +public class TraceHttpAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public HttpTraceKeysInjector httpTraceKeysInjector(Tracer tracer, TraceKeys traceKeys) { + return new HttpTraceKeysInjector(tracer, traceKeys); + } + + @Bean + @ConditionalOnMissingBean + public HttpSpanExtractor httpSpanExtractor(SleuthWebProperties sleuthWebProperties) { + return new ZipkinHttpSpanExtractor(Pattern.compile(sleuthWebProperties.getSkipPattern())); + } + + @Bean + @ConditionalOnMissingBean + public HttpSpanInjector httpSpanInjector() { + return new ZipkinHttpSpanInjector(); + } +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java index 549f8baee..1705814fb 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAutoConfiguration.java @@ -15,12 +15,6 @@ */ package org.springframework.cloud.sleuth.instrument.web; -import static javax.servlet.DispatcherType.ASYNC; -import static javax.servlet.DispatcherType.ERROR; -import static javax.servlet.DispatcherType.FORWARD; -import static javax.servlet.DispatcherType.INCLUDE; -import static javax.servlet.DispatcherType.REQUEST; - import java.util.regex.Pattern; import org.springframework.beans.factory.BeanFactory; @@ -38,13 +32,18 @@ import org.springframework.cloud.sleuth.SpanNamer; import org.springframework.cloud.sleuth.SpanReporter; import org.springframework.cloud.sleuth.TraceKeys; import org.springframework.cloud.sleuth.Tracer; -import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.util.StringUtils; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import static javax.servlet.DispatcherType.ASYNC; +import static javax.servlet.DispatcherType.ERROR; +import static javax.servlet.DispatcherType.FORWARD; +import static javax.servlet.DispatcherType.INCLUDE; +import static javax.servlet.DispatcherType.REQUEST; + /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration * Auto-configuration} enables tracing to HTTP requests. @@ -59,8 +58,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @ConditionalOnProperty(value = "spring.sleuth.web.enabled", matchIfMissing = true) @ConditionalOnWebApplication @ConditionalOnBean(Tracer.class) -@AutoConfigureAfter(TraceAutoConfiguration.class) -@EnableConfigurationProperties({TraceKeys.class, SleuthWebProperties.class}) +@AutoConfigureAfter(TraceHttpAutoConfiguration.class) public class TraceWebAutoConfiguration { /** @@ -86,13 +84,6 @@ public class TraceWebAutoConfiguration { return new TraceSpringDataBeanPostProcessor(beanFactory); } - @Bean - @ConditionalOnMissingBean - public HttpTraceKeysInjector httpTraceKeysInjector(Tracer tracer, - TraceKeys traceKeys) { - return new HttpTraceKeysInjector(tracer, traceKeys); - } - @Bean public FilterRegistrationBean traceWebFilter(TraceFilter traceFilter) { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( @@ -112,18 +103,6 @@ public class TraceWebAutoConfiguration { spanReporter, spanExtractor, httpTraceKeysInjector); } - @Bean - @ConditionalOnMissingBean - public HttpSpanExtractor httpSpanExtractor(SleuthWebProperties sleuthWebProperties) { - return new ZipkinHttpSpanExtractor(Pattern.compile(sleuthWebProperties.getSkipPattern())); - } - - @Bean - @ConditionalOnMissingBean - public HttpSpanInjector httpSpanInjector() { - return new ZipkinHttpSpanInjector(); - } - @Configuration @ConditionalOnClass(ManagementServerProperties.class) @ConditionalOnMissingBean(SkipPatternProvider.class) diff --git a/spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories b/spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories index 54f0d68a8..0759e9413 100644 --- a/spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories @@ -10,6 +10,7 @@ org.springframework.cloud.sleuth.instrument.async.AsyncCustomAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.async.AsyncDefaultAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.hystrix.SleuthHystrixAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAutoConfiguration,\ +org.springframework.cloud.sleuth.instrument.web.TraceHttpAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.web.client.TraceWebClientAutoConfiguration,\ org.springframework.cloud.sleuth.instrument.web.client.TraceWebAsyncClientAutoConfiguration,\ diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceNoWebEnvironmentTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceNoWebEnvironmentTests.java new file mode 100644 index 000000000..afecfdede --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceNoWebEnvironmentTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013-2016 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.instrument.web; + +import org.junit.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import static org.assertj.core.api.BDDAssertions.then; + +/** + * @author Marcin Grzejszczak + */ +public class TraceNoWebEnvironmentTests { + + // issue #32 + @Test + public void should_work_when_using_web_client_without_the_web_environment() { + SpringApplication springApplication = new SpringApplication(Config.class); + springApplication.setWebEnvironment(false); + + try (ConfigurableApplicationContext context = springApplication.run()) { + Config.SomeFeignClient client = context.getBean(Config.SomeFeignClient.class); + client.createSomeTestRequest(); + } + catch (Exception e) { + then(e.getCause().getClass()).isNotEqualTo(NoSuchBeanDefinitionException.class); + } + } + + @Configuration + @EnableAutoConfiguration + @EnableFeignClients + @EnableCircuitBreaker + public static class Config { + + + @FeignClient(name = "google", url = "https://www.google.com/") + public interface SomeFeignClient { + + @RequestMapping(value = "/", method = RequestMethod.GET) + String createSomeTestRequest(); + + } + } +}