Move zuul metrics to micrometer.

fixes gh-2317
parent 0ce21c4b
...@@ -187,6 +187,11 @@ ...@@ -187,6 +187,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
......
...@@ -64,6 +64,8 @@ import com.netflix.zuul.http.ZuulServlet; ...@@ -64,6 +64,8 @@ import com.netflix.zuul.http.ZuulServlet;
import com.netflix.zuul.monitoring.CounterFactory; import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory; import com.netflix.zuul.monitoring.TracerFactory;
import io.micrometer.core.instrument.MeterRegistry;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer * @author Dave Syer
...@@ -197,14 +199,13 @@ public class ZuulServerAutoConfiguration { ...@@ -197,14 +199,13 @@ public class ZuulServerAutoConfiguration {
} }
@Configuration @Configuration
//FIXME: 2.0.0 @ConditionalOnClass(MeterRegistry.class)
// @ConditionalOnClass(CounterService.class)
protected static class ZuulCounterFactoryConfiguration { protected static class ZuulCounterFactoryConfiguration {
@Bean @Bean
// @ConditionalOnBean(CounterService.class) @ConditionalOnBean(MeterRegistry.class)
public CounterFactory counterFactory(/*CounterService counterService*/) { public CounterFactory counterFactory(MeterRegistry meterRegistry) {
return new DefaultCounterFactory(/*counterService*/); return new DefaultCounterFactory(meterRegistry);
} }
} }
......
...@@ -18,22 +18,23 @@ package org.springframework.cloud.netflix.zuul.metrics; ...@@ -18,22 +18,23 @@ package org.springframework.cloud.netflix.zuul.metrics;
import com.netflix.zuul.monitoring.CounterFactory; import com.netflix.zuul.monitoring.CounterFactory;
import io.micrometer.core.instrument.MeterRegistry;
/** /**
* A counter based monitoring factory that uses {@link CounterService} to increment counters. * A counter based monitoring factory that uses {@link MeterRegistry} to increment counters.
* *
* @author Anastasiia Smirnova * @author Anastasiia Smirnova
*/ */
public class DefaultCounterFactory extends CounterFactory { public class DefaultCounterFactory extends CounterFactory {
//FIXME: 2.0.0 private final MeterRegistry meterRegistry;
// private final CounterService counterService;
public DefaultCounterFactory(/*CounterService counterService*/) { public DefaultCounterFactory(MeterRegistry meterRegistry) {
/*this.counterService = counterService;*/ this.meterRegistry = meterRegistry;
} }
@Override @Override
public void increment(String name) { public void increment(String name) {
// counterService.increment(name); this.meterRegistry.counter(name).increment();
} }
} }
...@@ -19,21 +19,28 @@ package org.springframework.cloud.netflix.zuul.metrics; ...@@ -19,21 +19,28 @@ package org.springframework.cloud.netflix.zuul.metrics;
import com.netflix.zuul.monitoring.CounterFactory; import com.netflix.zuul.monitoring.CounterFactory;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.Test; import org.junit.Test;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class DefaultCounterFactoryTests { public class DefaultCounterFactoryTests {
private static final String NAME = "my-super-metric-name"; private static final String NAME = "my-super-metric-name";
// private final CounterService counterService = mock(CounterService.class);
private final CounterFactory factory = new DefaultCounterFactory(/*counterService*/);
@Test @Test
public void shouldIncrement() throws Exception { public void shouldIncrement() throws Exception {
MeterRegistry meterRegistry = mock(MeterRegistry.class);
CounterFactory factory = new DefaultCounterFactory(meterRegistry);
Counter counter = mock(Counter.class);
when(meterRegistry.counter(NAME)).thenReturn(counter);
factory.increment(NAME); factory.increment(NAME);
//FIXME 2.0.0 verify(counterService).increment(NAME); verify(counter).increment();
} }
} }
\ No newline at end of file
...@@ -17,11 +17,13 @@ ...@@ -17,11 +17,13 @@
package org.springframework.cloud.netflix.zuul.metrics; package org.springframework.cloud.netflix.zuul.metrics;
import org.junit.Ignore; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration; import org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration;
import org.springframework.cloud.netflix.zuul.ZuulServerMarkerConfiguration; import org.springframework.cloud.netflix.zuul.ZuulServerMarkerConfiguration;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -29,10 +31,6 @@ import org.springframework.context.annotation.Configuration; ...@@ -29,10 +31,6 @@ import org.springframework.context.annotation.Configuration;
import com.netflix.zuul.monitoring.CounterFactory; import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory; import com.netflix.zuul.monitoring.TracerFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ClassPathExclusions({ "spring-boot-starter-actuator-*.jar", @ClassPathExclusions({ "spring-boot-starter-actuator-*.jar",
...@@ -59,7 +57,6 @@ public class ZuulEmptyMetricsApplicationTests { ...@@ -59,7 +57,6 @@ public class ZuulEmptyMetricsApplicationTests {
} }
@Test @Test
@Ignore //FIXME: 2.0.0
public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent() public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent()
throws Exception { throws Exception {
CounterFactory factory = this.context.getBean(CounterFactory.class); CounterFactory factory = this.context.getBean(CounterFactory.class);
......
...@@ -17,10 +17,8 @@ ...@@ -17,10 +17,8 @@
package org.springframework.cloud.netflix.zuul.metrics; package org.springframework.cloud.netflix.zuul.metrics;
import java.util.HashMap; import org.junit.Test;
import java.util.Map; import org.junit.runner.RunWith;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
...@@ -34,12 +32,12 @@ import com.netflix.zuul.exception.ZuulException; ...@@ -34,12 +32,12 @@ import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.monitoring.CounterFactory; import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory; import com.netflix.zuul.monitoring.TracerFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = { @SpringBootTest(classes = {
ZuulMetricsApplicationTests.ZuulMetricsApplicationTestsConfiguration.class, ZuulMetricsApplicationTests.ZuulMetricsApplicationTestsConfiguration.class,
...@@ -47,12 +45,12 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen ...@@ -47,12 +45,12 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@DirtiesContext @DirtiesContext
public class ZuulMetricsApplicationTests { public class ZuulMetricsApplicationTests {
private static final Map<String, Long> counters = new HashMap<>();
@Autowired @Autowired
CounterFactory counterFactory; private CounterFactory counterFactory;
@Autowired
private TracerFactory tracerFactory;
@Autowired @Autowired
TracerFactory tracerFactory; private MeterRegistry meterRegistry;
@Test @Test
public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent() public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent()
...@@ -66,18 +64,19 @@ public class ZuulMetricsApplicationTests { ...@@ -66,18 +64,19 @@ public class ZuulMetricsApplicationTests {
} }
@Test @Test
@Ignore //FIXME: 2.0.0
public void shouldIncrementCounters() throws Exception { public void shouldIncrementCounters() throws Exception {
new ZuulException("any", 500, "cause"); new ZuulException("any", 500, "cause");
new ZuulException("any", 500, "cause"); new ZuulException("any", 500, "cause");
assertEquals((long) counters.get("ZUUL::EXCEPTION:cause:500"), 2L); Double count = meterRegistry.counter("ZUUL::EXCEPTION:cause:500").count();
assertEquals(count.longValue(), 2L);
new ZuulException("any", 404, "cause2"); new ZuulException("any", 404, "cause2");
new ZuulException("any", 404, "cause2"); new ZuulException("any", 404, "cause2");
new ZuulException("any", 404, "cause2"); new ZuulException("any", 404, "cause2");
assertEquals((long) counters.get("ZUUL::EXCEPTION:cause2:404"), 3L); count = meterRegistry.counter("ZUUL::EXCEPTION:cause2:404").count();
assertEquals(count.longValue(), 3L);
} }
// Don't use @SpringBootApplication because we don't want to component scan // Don't use @SpringBootApplication because we don't want to component scan
...@@ -91,28 +90,9 @@ public class ZuulMetricsApplicationTests { ...@@ -91,28 +90,9 @@ public class ZuulMetricsApplicationTests {
@Configuration @Configuration
static class ZuulMetricsApplicationTestsConfiguration { static class ZuulMetricsApplicationTestsConfiguration {
//FIXME: 2.0.0 @Bean
/*@Bean public MeterRegistry meterRegistry() {
public CounterService counterService() { return new SimpleMeterRegistry();
return new CounterService() { }
// not thread safe, but we are ok with it in tests
@Override
public void increment(String metricName) {
Long counter = counters.get(metricName);
if (counter == null) {
counter = 0L;
}
counters.put(metricName, ++counter);
}
@Override
public void decrement(String metricName) {
}
@Override
public void reset(String metricName) {
}
};
}*/
} }
} }
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