Merge pull request #976 from DianeDuan/dropwizard-metrics
BAEL-548: Intro to Dropwizard Metrics
This commit is contained in:
commit
5cc5d7dc12
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>metrics</artifactId>
|
||||
|
||||
|
||||
<properties>
|
||||
<dep.ver.metrics>3.1.0</dep.ver.metrics>
|
||||
<dep.ver.servlet>3.1.0</dep.ver.servlet>
|
||||
<dep.ver.junit>4.12</dep.ver.junit>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
<version>${dep.ver.metrics}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-healthchecks</artifactId>
|
||||
<version>${dep.ver.metrics}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-servlets</artifactId>
|
||||
<version>${dep.ver.metrics}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-servlet</artifactId>
|
||||
<version>${dep.ver.metrics}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${dep.ver.servlet}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${dep.ver.junit}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.metrics.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.codahale.metrics.DerivativeGauge;
|
||||
import com.codahale.metrics.Gauge;
|
||||
|
||||
public class ActiveUserCountGauge extends DerivativeGauge<List<Long>, Integer> {
|
||||
public ActiveUserCountGauge(Gauge<List<Long>> base) {
|
||||
super(base);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer transform(List<Long> value) {
|
||||
return value.size();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.metrics.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.codahale.metrics.CachedGauge;
|
||||
|
||||
public class ActiveUsersGauge extends CachedGauge<List<Long>> {
|
||||
public ActiveUsersGauge(long timeout, TimeUnit timeoutUnit) {
|
||||
super(timeout, timeoutUnit);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Long> loadValue() {
|
||||
return getActiveUserCount();
|
||||
}
|
||||
|
||||
private List<Long> getActiveUserCount() {
|
||||
// mock reading from database and count the active users, return a fixed value
|
||||
List<Long> result = new ArrayList<Long>();
|
||||
result.add(12L);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.metrics.core;
|
||||
|
||||
import com.codahale.metrics.RatioGauge;
|
||||
|
||||
public class AttendanceRatioGauge extends RatioGauge {
|
||||
private int attendanceCount;
|
||||
private int courseCount;
|
||||
|
||||
public AttendanceRatioGauge(int attendanceCount, int courseCount) {
|
||||
this.attendanceCount = attendanceCount;
|
||||
this.courseCount = courseCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Ratio getRatio() {
|
||||
return Ratio.of(attendanceCount, courseCount);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.metrics.healthchecks;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
|
||||
public class DatabaseHealthCheck extends HealthCheck {
|
||||
@Override
|
||||
protected Result check() throws Exception {
|
||||
return Result.healthy();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.metrics.healthchecks;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
|
||||
public class UserCenterHealthCheck extends HealthCheck {
|
||||
@Override
|
||||
protected Result check() throws Exception {
|
||||
return Result.healthy();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.metrics.servlet;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.servlet.InstrumentedFilterContextListener;
|
||||
|
||||
public class MyInstrumentedFilterContextListener extends InstrumentedFilterContextListener {
|
||||
public static final MetricRegistry REGISTRY = new MetricRegistry();
|
||||
|
||||
@Override
|
||||
protected MetricRegistry getMetricRegistry() {
|
||||
return REGISTRY;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.metrics.servlets;
|
||||
|
||||
import com.baeldung.metrics.healthchecks.DatabaseHealthCheck;
|
||||
import com.codahale.metrics.health.HealthCheckRegistry;
|
||||
import com.codahale.metrics.servlets.HealthCheckServlet;
|
||||
|
||||
public class MyHealthCheckServletContextListener extends HealthCheckServlet.ContextListener {
|
||||
public static final HealthCheckRegistry HEALTH_CHECK_REGISTRY = new HealthCheckRegistry();
|
||||
|
||||
static {
|
||||
HEALTH_CHECK_REGISTRY.register("db", new DatabaseHealthCheck());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HealthCheckRegistry getHealthCheckRegistry() {
|
||||
return HEALTH_CHECK_REGISTRY;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.metrics.servlets;
|
||||
|
||||
import com.codahale.metrics.Counter;
|
||||
import com.codahale.metrics.Histogram;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.servlets.MetricsServlet;
|
||||
|
||||
public class MyMetricsServletContextListener extends MetricsServlet.ContextListener {
|
||||
private static final MetricRegistry METRIC_REGISTRY = new MetricRegistry();
|
||||
|
||||
static {
|
||||
Counter counter = METRIC_REGISTRY.counter("m01-counter");
|
||||
counter.inc();
|
||||
|
||||
Histogram histogram = METRIC_REGISTRY.histogram("m02-histogram");
|
||||
histogram.update(5);
|
||||
histogram.update(20);
|
||||
histogram.update(100);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MetricRegistry getMetricRegistry() {
|
||||
return METRIC_REGISTRY;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
package com.baeldung.metrics.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.codahale.metrics.*;
|
||||
|
||||
public class MetricsTest {
|
||||
@Test
|
||||
public void whenMarkMeter_thenCorrectRates() throws InterruptedException {
|
||||
Meter meter = new Meter();
|
||||
|
||||
long initCount = meter.getCount();
|
||||
assertThat(initCount, equalTo(0L));
|
||||
|
||||
meter.mark();
|
||||
assertThat(meter.getCount(), equalTo(1L));
|
||||
|
||||
meter.mark(20);
|
||||
assertThat(meter.getCount(), equalTo(21L));
|
||||
|
||||
// not use assert for these rate values because they change every time when this test is run
|
||||
double meanRate = meter.getMeanRate();
|
||||
double oneMinRate = meter.getOneMinuteRate();
|
||||
double fiveMinRate = meter.getFiveMinuteRate();
|
||||
double fifteenMinRate = meter.getFifteenMinuteRate();
|
||||
System.out.println(meanRate);
|
||||
System.out.println(oneMinRate);
|
||||
System.out.println(fiveMinRate);
|
||||
System.out.println(fifteenMinRate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInitRatioGauge_thenCorrectRatio() {
|
||||
Gauge<Double> ratioGauge = new AttendanceRatioGauge(15, 20);
|
||||
assertThat(ratioGauge.getValue(), equalTo(0.75));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseCacheGauge_thenCorrectGauge() {
|
||||
Gauge<List<Long>> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES);
|
||||
List<Long> expected = new ArrayList<Long>();
|
||||
expected.add(12L);
|
||||
assertThat(activeUsersGauge.getValue(), equalTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseDerivativeGauge_thenCorrectGaugeFromBase() {
|
||||
Gauge<List<Long>> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES);
|
||||
Gauge<Integer> activeUserCountGauge = new ActiveUserCountGauge(activeUsersGauge);
|
||||
assertThat(activeUserCountGauge.getValue(), equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIncDecCounter_thenCorrectCount() {
|
||||
Counter counter = new Counter();
|
||||
|
||||
long initCount = counter.getCount();
|
||||
assertThat(initCount, equalTo(0L));
|
||||
|
||||
counter.inc();
|
||||
assertThat(counter.getCount(), equalTo(1L));
|
||||
|
||||
counter.inc(11);
|
||||
assertThat(counter.getCount(), equalTo(12L));
|
||||
|
||||
counter.dec();
|
||||
assertThat(counter.getCount(), equalTo(11L));
|
||||
|
||||
counter.dec(6);
|
||||
assertThat(counter.getCount(), equalTo(5L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateHistogram_thenCorrectDistributionData() {
|
||||
Histogram histogram = new Histogram(new UniformReservoir());
|
||||
|
||||
histogram.update(5);
|
||||
long count1 = histogram.getCount();
|
||||
assertThat(count1, equalTo(1L));
|
||||
Snapshot snapshot1 = histogram.getSnapshot();
|
||||
assertThat(snapshot1.getValues().length, equalTo(1));
|
||||
assertThat(snapshot1.getValues()[0], equalTo(5L));
|
||||
assertThat(snapshot1.getMax(), equalTo(5L));
|
||||
assertThat(snapshot1.getMin(), equalTo(5L));
|
||||
assertThat(snapshot1.getMean(), equalTo(5.0));
|
||||
assertThat(snapshot1.getMedian(), equalTo(5.0));
|
||||
assertThat(snapshot1.getStdDev(), equalTo(0.0));
|
||||
assertThat(snapshot1.get75thPercentile(), equalTo(5.0));
|
||||
assertThat(snapshot1.get95thPercentile(), equalTo(5.0));
|
||||
assertThat(snapshot1.get98thPercentile(), equalTo(5.0));
|
||||
assertThat(snapshot1.get99thPercentile(), equalTo(5.0));
|
||||
assertThat(snapshot1.get999thPercentile(), equalTo(5.0));
|
||||
|
||||
histogram.update(20);
|
||||
long count2 = histogram.getCount();
|
||||
assertThat(count2, equalTo(2L));
|
||||
Snapshot snapshot2 = histogram.getSnapshot();
|
||||
assertThat(snapshot2.getValues().length, equalTo(2));
|
||||
assertThat(snapshot2.getValues()[0], equalTo(5L));
|
||||
assertThat(snapshot2.getValues()[1], equalTo(20L));
|
||||
assertThat(snapshot2.getMax(), equalTo(20L));
|
||||
assertThat(snapshot2.getMin(), equalTo(5L));
|
||||
assertThat(snapshot2.getMean(), equalTo(12.5));
|
||||
assertThat(snapshot2.getMedian(), equalTo(12.5));
|
||||
assertEquals(10.6, snapshot2.getStdDev(), 0.1);
|
||||
assertThat(snapshot2.get75thPercentile(), equalTo(20.0));
|
||||
assertThat(snapshot2.get95thPercentile(), equalTo(20.0));
|
||||
assertThat(snapshot2.get98thPercentile(), equalTo(20.0));
|
||||
assertThat(snapshot2.get99thPercentile(), equalTo(20.0));
|
||||
assertThat(snapshot2.get999thPercentile(), equalTo(20.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseTimer_thenCorrectTimerContexts() throws InterruptedException {
|
||||
Timer timer = new Timer();
|
||||
|
||||
Timer.Context context1 = timer.time();
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
|
||||
long elapsed1 = context1.stop();
|
||||
assertEquals(5000000000L, elapsed1, 1000000);
|
||||
assertThat(timer.getCount(), equalTo(1L));
|
||||
assertEquals(0.2, timer.getMeanRate(), 0.1);
|
||||
|
||||
Timer.Context context2 = timer.time();
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
context2.close();
|
||||
assertThat(timer.getCount(), equalTo(2L));
|
||||
assertEquals(0.3, timer.getMeanRate(), 0.1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.metrics.core;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.codahale.metrics.*;
|
||||
|
||||
public class ReporterTest {
|
||||
@Test
|
||||
public void whenConsoleReporter_thenOutputReport() {
|
||||
MetricRegistry metricRegistry = new MetricRegistry();
|
||||
|
||||
Meter meter = metricRegistry.meter("meter");
|
||||
meter.mark();
|
||||
meter.mark(200);
|
||||
Histogram histogram = metricRegistry.histogram("histogram");
|
||||
histogram.update(12);
|
||||
histogram.update(17);
|
||||
histogram.update(20);
|
||||
Counter counter = metricRegistry.counter("counter");
|
||||
counter.inc();
|
||||
counter.dec();
|
||||
counter.inc();
|
||||
counter.inc();
|
||||
counter.inc();
|
||||
counter.inc();
|
||||
|
||||
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
|
||||
reporter.start(5, TimeUnit.MICROSECONDS);
|
||||
reporter.report();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.metrics.healthchecks;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
import com.codahale.metrics.health.HealthCheckRegistry;
|
||||
|
||||
public class HealthCheckTest {
|
||||
@Test
|
||||
public void whenUseHealthCheck_thenHealthChecked() {
|
||||
HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();
|
||||
|
||||
healthCheckRegistry.register("db", new DatabaseHealthCheck());
|
||||
healthCheckRegistry.register("uc", new UserCenterHealthCheck());
|
||||
|
||||
assertThat(healthCheckRegistry.getNames().size(), equalTo(2));
|
||||
|
||||
Map<String, HealthCheck.Result> results = healthCheckRegistry.runHealthChecks();
|
||||
for (Map.Entry<String, HealthCheck.Result> entry : results.entrySet()) {
|
||||
assertThat(entry.getValue().isHealthy(), equalTo(true));
|
||||
}
|
||||
|
||||
healthCheckRegistry.unregister("uc");
|
||||
assertThat(healthCheckRegistry.getNames().size(), equalTo(1));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue