Commit 36e4c7cf by Dave Syer

Make metrics aspect conditional on Spring AOP

Also changes pointcut to use the interface so you don't need proxyTargetClass=true. Fixes gh-636
parent ef5785bf
...@@ -13,10 +13,12 @@ ...@@ -13,10 +13,12 @@
package org.springframework.cloud.netflix.metrics; package org.springframework.cloud.netflix.metrics;
import org.aspectj.lang.JoinPoint;
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.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -47,13 +49,22 @@ public class MetricsInterceptorConfiguration { ...@@ -47,13 +49,22 @@ public class MetricsInterceptorConfiguration {
} }
@Configuration @Configuration
@ConditionalOnClass(JoinPoint.class)
@ConditionalOnProperty(value = "spring.aop.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnBean({ RestTemplate.class }) @ConditionalOnBean({ RestTemplate.class })
static class MetricsRestTemplateConfiguration { static class MetricsRestTemplateAspectConfiguration {
@Bean @Bean
RestTemplateUrlTemplateCapturingAspect restTemplateUrlTemplateCapturingAspect() { RestTemplateUrlTemplateCapturingAspect restTemplateUrlTemplateCapturingAspect() {
return new RestTemplateUrlTemplateCapturingAspect(); return new RestTemplateUrlTemplateCapturingAspect();
} }
}
@Configuration
@ConditionalOnBean({ RestTemplate.class })
static class MetricsRestTemplateConfiguration {
@Bean @Bean
MetricsClientHttpRequestInterceptor spectatorLoggingClientHttpRequestInterceptor() { MetricsClientHttpRequestInterceptor spectatorLoggingClientHttpRequestInterceptor() {
return new MetricsClientHttpRequestInterceptor(); return new MetricsClientHttpRequestInterceptor();
...@@ -64,14 +75,17 @@ public class MetricsInterceptorConfiguration { ...@@ -64,14 +75,17 @@ public class MetricsInterceptorConfiguration {
final MetricsClientHttpRequestInterceptor interceptor) { final MetricsClientHttpRequestInterceptor interceptor) {
return new BeanPostProcessor() { return new BeanPostProcessor() {
@Override @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) { public Object postProcessBeforeInitialization(Object bean,
String beanName) {
return bean; return bean;
} }
@Override @Override
public Object postProcessAfterInitialization(Object bean, String beanName) { public Object postProcessAfterInitialization(Object bean,
if (bean instanceof RestTemplate) String beanName) {
if (bean instanceof RestTemplate) {
((RestTemplate) bean).getInterceptors().add(interceptor); ((RestTemplate) bean).getInterceptors().add(interceptor);
}
return bean; return bean;
} }
}; };
......
...@@ -25,7 +25,7 @@ import org.aspectj.lang.annotation.Aspect; ...@@ -25,7 +25,7 @@ import org.aspectj.lang.annotation.Aspect;
*/ */
@Aspect @Aspect
public class RestTemplateUrlTemplateCapturingAspect { public class RestTemplateUrlTemplateCapturingAspect {
@Around("execution(* org.springframework.web.client.RestTemplate.*(String, ..))") @Around("execution(* org.springframework.web.client.RestOperations+.*(String, ..))")
void captureUrlTemplate(ProceedingJoinPoint joinPoint) throws Throwable { void captureUrlTemplate(ProceedingJoinPoint joinPoint) throws Throwable {
try { try {
String urlTemplate = (String) joinPoint.getArgs()[0]; String urlTemplate = (String) joinPoint.getArgs()[0];
......
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