Commit c7c191c0 by Dave Syer

Ensure Servo metrics are exported the same as default metrics

Adds an `@ExportMetricReader` which was missing before and led to people losing their metric exports when they had Servo on the classpath. Also allow servo metrics autoconfiguration to be disabled via a flag netflix.metrics.servo.enabled=false (as an alternative to excluding the class in `@EnableAutoConfiguration`).
parent e0bc9d11
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2014 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 * 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 * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * 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 * 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 * 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. * specific language governing permissions and limitations under the License.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
package org.springframework.cloud.netflix.metrics.servo; package org.springframework.cloud.netflix.metrics.servo;
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
import org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration;
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics; import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.boot.actuate.metrics.CounterService;
...@@ -22,6 +23,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore; ...@@ -22,6 +23,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.metrics.DefaultMetricsTagProvider; import org.springframework.cloud.netflix.metrics.DefaultMetricsTagProvider;
import org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration; import org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration;
import org.springframework.cloud.netflix.metrics.MetricsTagProvider; import org.springframework.cloud.netflix.metrics.MetricsTagProvider;
...@@ -45,6 +47,7 @@ import com.netflix.servo.monitor.Monitors; ...@@ -45,6 +47,7 @@ import com.netflix.servo.monitor.Monitors;
@ConditionalOnMissingClass("com.netflix.spectator.api.Registry") @ConditionalOnMissingClass("com.netflix.spectator.api.Registry")
@AutoConfigureBefore(MetricRepositoryAutoConfiguration.class) @AutoConfigureBefore(MetricRepositoryAutoConfiguration.class)
@Import(MetricsInterceptorConfiguration.class) @Import(MetricsInterceptorConfiguration.class)
@ConditionalOnProperty(name = "spring.metrics.servo.enabled", matchIfMissing = true)
public class ServoMetricsAutoConfiguration { public class ServoMetricsAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
...@@ -61,8 +64,9 @@ public class ServoMetricsAutoConfiguration { ...@@ -61,8 +64,9 @@ public class ServoMetricsAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public MonitorRegistry monitorRegistry(ServoMetricsConfigBean servoMetricsConfig) { public MonitorRegistry monitorRegistry(ServoMetricsConfigBean servoMetricsConfig) {
System.setProperty(DefaultMonitorRegistry.class.getCanonicalName() + ".registryClass", servoMetricsConfig System.setProperty(
.getRegistryClass()); DefaultMonitorRegistry.class.getCanonicalName() + ".registryClass",
servoMetricsConfig.getRegistryClass());
return DefaultMonitorRegistry.getInstance(); return DefaultMonitorRegistry.getInstance();
} }
...@@ -72,8 +76,16 @@ public class ServoMetricsAutoConfiguration { ...@@ -72,8 +76,16 @@ public class ServoMetricsAutoConfiguration {
} }
@Bean @Bean
public MetricReaderPublicMetrics servoPublicMetrics(MonitorRegistry monitorRegistry, ServoMetricNaming servoMetricNaming) { @ExportMetricReader
ServoMetricReader reader = new ServoMetricReader(monitorRegistry, servoMetricNaming); public ServoMetricReader servoMetricReader(MonitorRegistry monitorRegistry,
ServoMetricNaming servoMetricNaming) {
ServoMetricReader reader = new ServoMetricReader(monitorRegistry,
servoMetricNaming);
return reader;
}
@Bean
public MetricReaderPublicMetrics servoPublicMetrics(ServoMetricReader reader) {
return new MetricReaderPublicMetrics(reader); return new MetricReaderPublicMetrics(reader);
} }
......
...@@ -22,9 +22,31 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -22,9 +22,31 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
*/ */
@ConfigurationProperties("netflix.metrics.servo") @ConfigurationProperties("netflix.metrics.servo")
public class ServoMetricsConfigBean { public class ServoMetricsConfigBean {
/**
* Enable the Netflix Servo metrics services. If this flag is off Servo can still be
* used by Netflix OSS components, but the Spring Boot metrics collection will be done
* with the default services.
*/
boolean enabled = true;
/**
* Fully qualified class name for monitor registry used by Servo.
*/
String registryClass = "com.netflix.servo.BasicMonitorRegistry"; String registryClass = "com.netflix.servo.BasicMonitorRegistry";
public String getRegistryClass() { public String getRegistryClass() {
return registryClass; return this.registryClass;
}
public boolean getEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setRegistryClass(String registryClass) {
this.registryClass = registryClass;
} }
} }
/*
* Copyright 2013-2015 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.netflix.metrics.servo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration()
public class NoServoMetricsAutoConfigurationTests {
@Autowired(required = false)
private ServoMetricNaming naming;
@Autowired(required = false)
@ExportMetricReader
private MetricReader reader;
@Test
public void test() {
assertNull(this.naming);
assertNotNull(this.reader);
}
@EnableAutoConfiguration(exclude = ServoMetricsAutoConfiguration.class)
@Configuration
protected static class TestConfiguration {
}
}
...@@ -11,13 +11,14 @@ import org.springframework.cloud.netflix.metrics.SimpleMonitorRegistry; ...@@ -11,13 +11,14 @@ import org.springframework.cloud.netflix.metrics.SimpleMonitorRegistry;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.netflix.servo.monitor.MonitorConfig; import com.netflix.servo.monitor.MonitorConfig;
import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class ServoMetricReaderTests { public class ServoMetricReaderTests {
@Test @Test
public void singleCompositeMonitorYieldsMultipleActuatorMetrics() { public void singleCompositeMonitorYieldsMultipleActuatorMetrics() {
SimpleMonitorRegistry registry = new SimpleMonitorRegistry(); SimpleMonitorRegistry registry = new SimpleMonitorRegistry();
ServoMetricReader reader = new ServoMetricReader(registry, new DimensionalServoMetricNaming()); ServoMetricReader reader = new ServoMetricReader(registry,
new DimensionalServoMetricNaming());
MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName"); MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName");
ServoMonitorCache servoMonitorCache = new ServoMonitorCache(registry); ServoMonitorCache servoMonitorCache = new ServoMonitorCache(registry);
...@@ -42,4 +43,3 @@ public class ServoMetricReaderTests { ...@@ -42,4 +43,3 @@ public class ServoMetricReaderTests {
metricNames.get(3)); metricNames.get(3));
} }
} }
/*
* Copyright 2013-2015 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.netflix.metrics.servo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
/**
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration()
public class ServoMetricsAutoConfigurationTests {
@Autowired(required = false)
private ServoMetricNaming naming;
@Autowired(required = false)
@ExportMetricReader
private MetricReader reader;
@Test
public void test() {
assertNotNull(this.naming);
assertNotNull(this.reader);
}
@EnableAutoConfiguration
@Configuration
protected static class TestConfiguration {
}
}
...@@ -184,7 +184,8 @@ public abstract class ZuulProxyTestBase { ...@@ -184,7 +184,8 @@ public abstract class ZuulProxyTestBase {
assertEquals("Received {key=[overridden]}", result.getBody()); assertEquals("Received {key=[overridden]}", result.getBody());
} }
protected static abstract class AbstractZuulProxyApplication extends DelegatingWebMvcConfiguration { protected static abstract class AbstractZuulProxyApplication
extends DelegatingWebMvcConfiguration {
@RequestMapping("/testing123") @RequestMapping("/testing123")
public String testing123() { public String testing123() {
...@@ -202,7 +203,7 @@ public abstract class ZuulProxyTestBase { ...@@ -202,7 +203,7 @@ public abstract class ZuulProxyTestBase {
} }
@RequestMapping(value = "/local/{id}", method = RequestMethod.GET) @RequestMapping(value = "/local/{id}", method = RequestMethod.GET)
public ResponseEntity get(@PathVariable String id) { public ResponseEntity<?> get(@PathVariable String id) {
if ("notfound".equalsIgnoreCase(id)) { if ("notfound".equalsIgnoreCase(id)) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
......
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