Fix MetricsTagProvider for non web apps.

Guard creating and creation of classes that use it with @ConditionalOnClass(name = "javax.servlet.http.HttpServletRequest") fixes gh-1302
parent ec726e86
...@@ -18,8 +18,6 @@ import java.util.Collection; ...@@ -18,8 +18,6 @@ import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache; import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestExecution;
...@@ -43,17 +41,22 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt ...@@ -43,17 +41,22 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt
* Boot (Actuator) provides a more general purpose abstraction for dimensional metrics * Boot (Actuator) provides a more general purpose abstraction for dimensional metrics
* systems, this can be moved there and rewritten against that abstraction. * systems, this can be moved there and rewritten against that abstraction.
*/ */
@Autowired private final MonitorRegistry registry;
MonitorRegistry registry;
@Autowired private final Collection<MetricsTagProvider> tagProviders;
Collection<MetricsTagProvider> tagProviders;
@Autowired private final ServoMonitorCache servoMonitorCache;
ServoMonitorCache servoMonitorCache;
@Value("${netflix.metrics.restClient.metricName:restclient}") private final String metricName;
String metricName;
public MetricsClientHttpRequestInterceptor(MonitorRegistry registry,
Collection<MetricsTagProvider> tagProviders,
ServoMonitorCache servoMonitorCache, String metricName) {
this.registry = registry;
this.tagProviders = tagProviders;
this.servoMonitorCache = servoMonitorCache;
this.metricName = metricName;
}
@Override @Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, public ClientHttpResponse intercept(HttpRequest request, byte[] body,
...@@ -68,8 +71,8 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt ...@@ -68,8 +71,8 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt
finally { finally {
SmallTagMap.Builder builder = SmallTagMap.builder(); SmallTagMap.Builder builder = SmallTagMap.builder();
for (MetricsTagProvider tagProvider : tagProviders) { for (MetricsTagProvider tagProvider : tagProviders) {
for (Map.Entry<String, String> tag : tagProvider.clientHttpRequestTags( for (Map.Entry<String, String> tag : tagProvider
request, response).entrySet()) { .clientHttpRequestTags(request, response).entrySet()) {
builder.add(Tags.newTag(tag.getKey(), tag.getValue())); builder.add(Tags.newTag(tag.getKey(), tag.getValue()));
} }
} }
...@@ -78,8 +81,8 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt ...@@ -78,8 +81,8 @@ public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInt
.builder(metricName); .builder(metricName);
monitorConfigBuilder.withTags(builder); monitorConfigBuilder.withTags(builder);
servoMonitorCache.getTimer(monitorConfigBuilder.build()).record( servoMonitorCache.getTimer(monitorConfigBuilder.build())
System.nanoTime() - startTime, TimeUnit.NANOSECONDS); .record(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
} }
} }
} }
...@@ -15,14 +15,17 @@ package org.springframework.cloud.netflix.metrics; ...@@ -15,14 +15,17 @@ package org.springframework.cloud.netflix.metrics;
import java.util.ArrayList; import java.util.ArrayList;
import com.netflix.servo.MonitorRegistry;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
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.cloud.netflix.metrics.servo.ServoMonitorCache;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -34,6 +37,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter ...@@ -34,6 +37,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import com.netflix.servo.monitor.Monitors; import com.netflix.servo.monitor.Monitors;
import java.util.Collection;
/** /**
* @author Jon Schneider * @author Jon Schneider
*/ */
...@@ -71,11 +76,18 @@ public class MetricsInterceptorConfiguration { ...@@ -71,11 +76,18 @@ public class MetricsInterceptorConfiguration {
@Configuration @Configuration
@ConditionalOnBean({ RestTemplate.class }) @ConditionalOnBean({ RestTemplate.class })
@ConditionalOnClass(name = "javax.servlet.http.HttpServletRequest")
static class MetricsRestTemplateConfiguration { static class MetricsRestTemplateConfiguration {
@Value("${netflix.metrics.restClient.metricName:restclient}")
String metricName;
@Bean @Bean
MetricsClientHttpRequestInterceptor spectatorLoggingClientHttpRequestInterceptor() { MetricsClientHttpRequestInterceptor spectatorLoggingClientHttpRequestInterceptor(
return new MetricsClientHttpRequestInterceptor(); MonitorRegistry registry, Collection<MetricsTagProvider> tagProviders,
ServoMonitorCache servoMonitorCache) {
return new MetricsClientHttpRequestInterceptor(registry, tagProviders,
servoMonitorCache, this.metricName);
} }
@Bean @Bean
......
...@@ -95,8 +95,12 @@ public class ServoMetricsAutoConfiguration { ...@@ -95,8 +95,12 @@ public class ServoMetricsAutoConfiguration {
return new ServoMetricServices(monitorRegistry); return new ServoMetricServices(monitorRegistry);
} }
@Configuration
@ConditionalOnClass(name = "javax.servlet.http.HttpServletRequest")
protected static class MetricsTagConfiguration {
@Bean @Bean
public MetricsTagProvider defaultMetricsTagProvider() { public MetricsTagProvider defaultMetricsTagProvider() {
return new DefaultMetricsTagProvider(); return new DefaultMetricsTagProvider();
} }
}
} }
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