Move zuul metrics to micrometer.

fixes gh-2317
parent 0ce21c4b
......@@ -187,6 +187,11 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
......
......@@ -64,6 +64,8 @@ import com.netflix.zuul.http.ZuulServlet;
import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory;
import io.micrometer.core.instrument.MeterRegistry;
/**
* @author Spencer Gibb
* @author Dave Syer
......@@ -197,14 +199,13 @@ public class ZuulServerAutoConfiguration {
}
@Configuration
//FIXME: 2.0.0
// @ConditionalOnClass(CounterService.class)
@ConditionalOnClass(MeterRegistry.class)
protected static class ZuulCounterFactoryConfiguration {
@Bean
// @ConditionalOnBean(CounterService.class)
public CounterFactory counterFactory(/*CounterService counterService*/) {
return new DefaultCounterFactory(/*counterService*/);
@ConditionalOnBean(MeterRegistry.class)
public CounterFactory counterFactory(MeterRegistry meterRegistry) {
return new DefaultCounterFactory(meterRegistry);
}
}
......
......@@ -18,22 +18,23 @@ package org.springframework.cloud.netflix.zuul.metrics;
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
*/
public class DefaultCounterFactory extends CounterFactory {
//FIXME: 2.0.0
// private final CounterService counterService;
private final MeterRegistry meterRegistry;
public DefaultCounterFactory(/*CounterService counterService*/) {
/*this.counterService = counterService;*/
public DefaultCounterFactory(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
@Override
public void increment(String name) {
// counterService.increment(name);
this.meterRegistry.counter(name).increment();
}
}
......@@ -19,21 +19,28 @@ package org.springframework.cloud.netflix.zuul.metrics;
import com.netflix.zuul.monitoring.CounterFactory;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class DefaultCounterFactoryTests {
private static final String NAME = "my-super-metric-name";
// private final CounterService counterService = mock(CounterService.class);
private final CounterFactory factory = new DefaultCounterFactory(/*counterService*/);
@Test
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);
//FIXME 2.0.0 verify(counterService).increment(NAME);
verify(counter).increment();
}
}
\ No newline at end of file
......@@ -17,11 +17,13 @@
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.cloud.test.ClassPathExclusions;
import org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration;
import org.springframework.cloud.netflix.zuul.ZuulServerMarkerConfiguration;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
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.TracerFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ClassPathExclusions({ "spring-boot-starter-actuator-*.jar",
......@@ -59,7 +57,6 @@ public class ZuulEmptyMetricsApplicationTests {
}
@Test
@Ignore //FIXME: 2.0.0
public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent()
throws Exception {
CounterFactory factory = this.context.getBean(CounterFactory.class);
......
......@@ -17,10 +17,8 @@
package org.springframework.cloud.netflix.zuul.metrics;
import java.util.HashMap;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
......@@ -34,12 +32,12 @@ import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.monitoring.CounterFactory;
import com.netflix.zuul.monitoring.TracerFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
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)
@SpringBootTest(classes = {
ZuulMetricsApplicationTests.ZuulMetricsApplicationTestsConfiguration.class,
......@@ -47,12 +45,12 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@DirtiesContext
public class ZuulMetricsApplicationTests {
private static final Map<String, Long> counters = new HashMap<>();
@Autowired
CounterFactory counterFactory;
private CounterFactory counterFactory;
@Autowired
private TracerFactory tracerFactory;
@Autowired
TracerFactory tracerFactory;
private MeterRegistry meterRegistry;
@Test
public void shouldSetupDefaultCounterFactoryIfCounterServiceIsPresent()
......@@ -66,18 +64,19 @@ public class ZuulMetricsApplicationTests {
}
@Test
@Ignore //FIXME: 2.0.0
public void shouldIncrementCounters() throws Exception {
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");
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
......@@ -91,28 +90,9 @@ public class ZuulMetricsApplicationTests {
@Configuration
static class ZuulMetricsApplicationTestsConfiguration {
//FIXME: 2.0.0
/*@Bean
public CounterService counterService() {
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) {
@Bean
public MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
};
}*/
}
}
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