Updated the feign builder tracing logic

without this change a default Feign.Builder doesn't have its feign.Client instrumented
with this change we're using reflection to instrument that Client

fixes gh-1870
This commit is contained in:
Marcin Grzejszczak
2021-03-16 15:53:44 +01:00
parent 658490d870
commit 4ba698c040
3 changed files with 70 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.cloud.sleuth.instrument.web.client.feign.FeignContext
import org.springframework.cloud.sleuth.instrument.web.client.feign.OkHttpFeignClientBeanPostProcessor;
import org.springframework.cloud.sleuth.instrument.web.client.feign.SleuthFeignBuilder;
import org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignAspect;
import org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignBuilderBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@@ -68,6 +69,11 @@ public class TraceFeignClientAutoConfiguration {
return new FeignContextBeanPostProcessor(beanFactory);
}
@Bean
static TraceFeignBuilderBeanPostProcessor traceFeignBuilderBeanPostProcessor(BeanFactory beanFactory) {
return new TraceFeignBuilderBeanPostProcessor(beanFactory);
}
}
@Configuration(proxyBeanMethods = false)

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2013-2021 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
*
* https://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 java.lang.reflect.Field;
import feign.Client;
import feign.Feign;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
/**
* {@link BeanPostProcessor} that ensures that each {@link Feign.Builder} has
* a trace representation of a {@link Client}.
*
* @since 3.0.2
* @author Marcin Grzejszczak
*/
public class TraceFeignBuilderBeanPostProcessor implements BeanPostProcessor {
private final BeanFactory beanFactory;
public TraceFeignBuilderBeanPostProcessor(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Feign.Builder) {
Field client = ReflectionUtils.findField(Feign.Builder.class, "client");
ReflectionUtils.makeAccessible(client);
Feign.Builder delegate = (Feign.Builder) bean;
Client clientInDelegate = (Client) ReflectionUtils.getField(client, delegate);
if (clientInDelegate instanceof LazyClient || clientInDelegate instanceof LazyTracingFeignClient) {
return bean;
}
delegate.client(new LazyClient(this.beanFactory, clientInDelegate));
return bean;
}
return bean;
}
}

View File

@@ -70,8 +70,11 @@ final class TraceFeignObjectWrapper {
private Object loadBalancerClientFactory;
private final TraceFeignBuilderBeanPostProcessor traceFeignBuilderBeanPostProcessor;
TraceFeignObjectWrapper(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.traceFeignBuilderBeanPostProcessor = new TraceFeignBuilderBeanPostProcessor(beanFactory);
}
Object wrap(Object bean) {
@@ -87,7 +90,7 @@ final class TraceFeignObjectWrapper {
}
return new LazyTracingFeignClient(this.beanFactory, (Client) bean);
}
return bean;
return this.traceFeignBuilderBeanPostProcessor.postProcessAfterInitialization(bean, null);
}
private Object instrumentedFeignLoadBalancerClient(Object bean) {