From f085aae3b6b34cf7601a14676dbd4b53bea78097 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 | 46 +++++++++++++ .../web/TraceWebAutoConfiguration.java | 15 ++-- .../main/resources/META-INF/spring.factories | 1 + .../web/TraceNoWebEnvironmentTests.java | 68 +++++++++++++++++++ 4 files changed, 119 insertions(+), 11 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..b638cf4a2 --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceHttpAutoConfiguration.java @@ -0,0 +1,46 @@ +/* + * 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.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 }) +public class TraceHttpAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public HttpTraceKeysInjector httpTraceKeysInjector(Tracer tracer, TraceKeys traceKeys) { + return new HttpTraceKeysInjector(tracer, traceKeys); + } +} 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 f017aa6ee..8645462f8 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,8 +15,8 @@ */ package org.springframework.cloud.sleuth.instrument.web; -import javax.servlet.http.HttpServletRequest; import java.util.regex.Pattern; +import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.BeanFactory; import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties; @@ -34,7 +34,6 @@ 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; @@ -49,7 +48,7 @@ import static javax.servlet.DispatcherType.REQUEST; /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} - * enables tracing to HTTP requests. + * enables tracing to HTTP requests for Web based applications. * * @author Tomasz Nurkewicz, 4financeIT * @author Michal Chmielarz, 4financeIT @@ -61,8 +60,8 @@ import static javax.servlet.DispatcherType.REQUEST; @ConditionalOnProperty(value = "spring.sleuth.web.enabled", matchIfMissing = true) @ConditionalOnWebApplication @ConditionalOnBean(Tracer.class) -@AutoConfigureAfter(TraceAutoConfiguration.class) -@EnableConfigurationProperties({TraceKeys.class, SleuthWebProperties.class}) +@AutoConfigureAfter(TraceHttpAutoConfiguration.class) +@EnableConfigurationProperties({ SleuthWebProperties.class }) public class TraceWebAutoConfiguration { /** @@ -86,12 +85,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(Tracer tracer, TraceKeys traceKeys, SkipPatternProvider skipPatternProvider, SpanReporter spanReporter, 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(); + + } + } +}