From cc07a8f777151e4d22fbe4ef0ef61dd1e483323f Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 24 Feb 2017 16:58:27 +0100 Subject: [PATCH] Added support for Feign + OkHttpClient + Sleuth without this change we get exceptions related to not being able to extend a final class with this change we're not wrapping any final types and we have a custom bean post processor for okhttp fixes #513 --- .../main/asciidoc/spring-cloud-sleuth.adoc | 5 -- spring-cloud-sleuth-core/pom.xml | 5 ++ .../sleuth/instrument/web/TraceWebAspect.java | 5 +- .../OkHttpFeignClientBeanPostProcessor.java | 62 +++++++++++++++++++ .../web/client/feign/TraceFeignAspect.java | 2 +- .../TraceFeignClientAutoConfiguration.java | 11 ++++ .../feign/issues/issue393/Issue393Tests.java | 8 +++ 7 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/OkHttpFeignClientBeanPostProcessor.java diff --git a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc index dd72e2c68..467c8eed6 100644 --- a/docs/src/main/asciidoc/spring-cloud-sleuth.adoc +++ b/docs/src/main/asciidoc/spring-cloud-sleuth.adoc @@ -596,11 +596,6 @@ include::../../../../spring-cloud-sleuth-core/src/test/java/org/springframework/ Spring Cloud Sleuth integrates with http://projects.spring.io/spring-integration/[Spring Integration]. It creates spans for publish and subscribe events. To disable Spring Integration instrumentation, set `spring.sleuth.integration.enabled` to false. -Spring Cloud Sleuth up till version 1.0.4 is sending invalid tracing headers when using messaging. Those headers are actually -the same as the ones sent in HTTP (they contain a `-`) in its name. For the sake of -backwards compatibility in 1.0.4 we've started sending both valid and invalid headers. Please upgrade to 1.0.4 because -in Spring Cloud Sleuth 1.1 we will remove the support for the deprecated headers. - You can provide the `spring.sleuth.integration.patterns` pattern to explicitly provide the names of channels that you want to include for tracing. By default all channels are included. diff --git a/spring-cloud-sleuth-core/pom.xml b/spring-cloud-sleuth-core/pom.xml index 67ce344b1..80e1909cb 100644 --- a/spring-cloud-sleuth-core/pom.xml +++ b/spring-cloud-sleuth-core/pom.xml @@ -95,6 +95,11 @@ httpclient true + + io.github.openfeign + feign-okhttp + true + org.aspectj aspectjrt diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java index dcfe7e47c..18f010dcc 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java @@ -34,12 +34,12 @@ import org.springframework.web.context.request.async.WebAsyncTask; * Aspect that adds tracing to *

*

*

@@ -57,7 +57,6 @@ import org.springframework.web.context.request.async.WebAsyncTask; * @author Spencer Gibb * @since 1.0.0 * - * @see org.springframework.web.bind.annotation.RestController * @see org.springframework.stereotype.Controller * @see org.springframework.web.client.RestOperations * @see org.springframework.cloud.sleuth.TraceCallable diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/OkHttpFeignClientBeanPostProcessor.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/OkHttpFeignClientBeanPostProcessor.java new file mode 100644 index 000000000..9379a26de --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/OkHttpFeignClientBeanPostProcessor.java @@ -0,0 +1,62 @@ +/* + * 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.client.feign; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.config.BeanPostProcessor; + +import feign.okhttp.OkHttpClient; + +/** + * Post processor that wraps takes care of the OkHttp Feign Client instrumentation + * + * @author Marcin Grzejszczak + * + * @since 1.1.3 + */ +final class OkHttpFeignClientBeanPostProcessor implements BeanPostProcessor { + + private final BeanFactory beanFactory; + private TraceFeignObjectWrapper traceFeignObjectWrapper; + + OkHttpFeignClientBeanPostProcessor(BeanFactory beanFactory) { + this.beanFactory = beanFactory; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) + throws BeansException { + if (bean instanceof OkHttpClient) { + return getTraceFeignObjectWrapper().wrap(bean); + } + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) + throws BeansException { + return bean; + } + + private TraceFeignObjectWrapper getTraceFeignObjectWrapper() { + if (this.traceFeignObjectWrapper == null) { + this.traceFeignObjectWrapper = this.beanFactory.getBean(TraceFeignObjectWrapper.class); + } + return this.traceFeignObjectWrapper; + } +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignAspect.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignAspect.java index 2a8dd6395..8f0dcd8f5 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignAspect.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignAspect.java @@ -39,7 +39,7 @@ class TraceFeignAspect { this.beanFactory = beanFactory; } - @Around("execution (* feign.Client.*(..))") + @Around("execution (* feign.Client.*(..)) && !within(is(FinalType))") public Object feignClientWasCalled(final ProceedingJoinPoint pjp) throws Throwable { Object[] args = pjp.getArgs(); Request request = (Request) args[0]; diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignClientAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignClientAutoConfiguration.java index b42fbf584..6c866afa3 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignClientAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/feign/TraceFeignClientAutoConfiguration.java @@ -33,6 +33,7 @@ import org.springframework.context.annotation.Scope; import feign.Client; import feign.Feign; +import feign.okhttp.OkHttpClient; /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration @@ -75,6 +76,16 @@ public class TraceFeignClientAutoConfiguration { } } + @Configuration + @ConditionalOnClass(OkHttpClient.class) + protected static class OkHttpClientFeignBeanPostProcessorConfiguration { + + @Bean + OkHttpFeignClientBeanPostProcessor okHttpFeignClientBeanPostProcessor(BeanFactory beanFactory) { + return new OkHttpFeignClientBeanPostProcessor(beanFactory); + } + } + @Bean TraceFeignObjectWrapper traceFeignObjectWrapper(BeanFactory beanFactory) { return new TraceFeignObjectWrapper(beanFactory); diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/feign/issues/issue393/Issue393Tests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/feign/issues/issue393/Issue393Tests.java index b221d9d6d..d3a104f24 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/feign/issues/issue393/Issue393Tests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/client/feign/issues/issue393/Issue393Tests.java @@ -41,6 +41,8 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; +import feign.okhttp.OkHttpClient; + import static org.assertj.core.api.BDDAssertions.then; /** @@ -89,6 +91,12 @@ class Application { return new DemoController(myNameRemote); } + // issue #513 + @Bean + public OkHttpClient myOkHttpClient() { + return new OkHttpClient(); + } + @Bean public feign.Logger.Level feignLoggerLevel() { return feign.Logger.Level.BASIC;