Commit 892eea75 by Spencer Gibb

Lazily look up MetricsClientHttpRequestInterceptor in BPP.

fixes gh-912
parent 3f9e76b8
...@@ -14,12 +14,15 @@ ...@@ -14,12 +14,15 @@
package org.springframework.cloud.netflix.metrics; package org.springframework.cloud.netflix.metrics;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.metrics.reader.MetricReader; import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -73,24 +76,37 @@ public class MetricsInterceptorConfiguration { ...@@ -73,24 +76,37 @@ public class MetricsInterceptorConfiguration {
} }
@Bean @Bean
BeanPostProcessor spectatorRestTemplateInterceptorPostProcessor( BeanPostProcessor spectatorRestTemplateInterceptorPostProcessor() {
final MetricsClientHttpRequestInterceptor interceptor) { return new MetricsInterceptorPostProcessor();
return new BeanPostProcessor() { }
private static class MetricsInterceptorPostProcessor implements
BeanPostProcessor, ApplicationContextAware {
private ApplicationContext context;
private MetricsClientHttpRequestInterceptor interceptor;
@Override @Override
public Object postProcessBeforeInitialization(Object bean, public Object postProcessBeforeInitialization(Object bean, String beanName) {
String beanName) {
return bean; return bean;
} }
@Override @Override
public Object postProcessAfterInitialization(Object bean, public Object postProcessAfterInitialization(Object bean, String beanName) {
String beanName) {
if (bean instanceof RestTemplate) { if (bean instanceof RestTemplate) {
if (this.interceptor == null) {
this.interceptor = this.context
.getBean(MetricsClientHttpRequestInterceptor.class);
}
((RestTemplate) bean).getInterceptors().add(interceptor); ((RestTemplate) bean).getInterceptors().add(interceptor);
} }
return bean; return bean;
} }
};
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
} }
} }
} }
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.cloud.netflix.metrics; package org.springframework.cloud.netflix.metrics;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
...@@ -23,12 +25,16 @@ import org.springframework.boot.test.WebIntegrationTest; ...@@ -23,12 +25,16 @@ import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory; import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory;
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactoryTests; import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactoryTests;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory; import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertTrue; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -44,15 +50,23 @@ public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTest ...@@ -44,15 +50,23 @@ public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTest
@Override @Override
public void requestFactoryIsRibbon() { public void requestFactoryIsRibbon() {
ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory(); ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory();
assertTrue("wrong RequestFactory type: " + requestFactory.getClass(), assertThat("wrong RequestFactory type: " + requestFactory.getClass(),
requestFactory instanceof InterceptingClientHttpRequestFactory); requestFactory, is(instanceOf(InterceptingClientHttpRequestFactory.class)));
InterceptingClientHttpRequestFactory intercepting = (InterceptingClientHttpRequestFactory) requestFactory; InterceptingClientHttpRequestFactory intercepting = (InterceptingClientHttpRequestFactory) requestFactory;
Object interceptorsField = ReflectionTestUtils.getField(intercepting,
"interceptors");
assertThat("wrong interceptors type: " + interceptorsField.getClass(), interceptorsField, is(instanceOf(List.class)));
@SuppressWarnings("unchecked")
List<ClientHttpRequestInterceptor> interceptors = (List<ClientHttpRequestInterceptor>) interceptorsField;
assertThat("interceptors is wrong size", interceptors, hasSize(1));
assertThat("wrong interceptor type", interceptors.get(0),
is(instanceOf(MetricsClientHttpRequestInterceptor.class)));
Object realRequestFactory = ReflectionTestUtils.getField(intercepting, Object realRequestFactory = ReflectionTestUtils.getField(intercepting,
"requestFactory"); "requestFactory");
assertTrue("wrong RequestFactory type: " + realRequestFactory.getClass(), assertThat("wrong RequestFactory type: " + realRequestFactory.getClass(),
realRequestFactory instanceof RibbonClientHttpRequestFactory); realRequestFactory, is(instanceOf(RibbonClientHttpRequestFactory.class)));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment