diff --git a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java index b34f72e2..a774822b 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java +++ b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java @@ -34,15 +34,24 @@ public final class CodaHaleMetricsTracker extends MetricsTracker private final Meter connectionTimeoutMeter; private final MetricRegistry registry; + private static final String METRIC_CATEGORY = "pool"; + private static final String METRIC_NAME_WAIT = "Wait"; + private static final String METRIC_NAME_USAGE = "Usage"; + private static final String METRIC_NAME_TIMEOUT_RATE = "ConnectionTimeoutRate"; + private static final String METRIC_NAME_TOTAL_CONNECTIONS = "TotalConnections"; + private static final String METRIC_NAME_IDLE_CONNECTIONS = "IdleConnections"; + private static final String METRIC_NAME_ACTIVE_CONNECTIONS = "ActiveConnections"; + private static final String METRIC_NAME_PENDING_CONNECTIONS = "PendingConnections"; + public CodaHaleMetricsTracker(final String poolName, final PoolStats poolStats, final MetricRegistry registry) { this.poolName = poolName; this.registry = registry; - this.connectionObtainTimer = registry.timer(MetricRegistry.name(poolName, "pool", "Wait")); - this.connectionUsage = registry.histogram(MetricRegistry.name(poolName, "pool", "Usage")); - this.connectionTimeoutMeter = registry.meter(MetricRegistry.name(poolName, "pool", "ConnectionTimeoutRate")); + this.connectionObtainTimer = registry.timer(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT)); + this.connectionUsage = registry.histogram(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_USAGE)); + this.connectionTimeoutMeter = registry.meter(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TIMEOUT_RATE)); - registry.register(MetricRegistry.name(poolName, "pool", "TotalConnections"), + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS), new Gauge() { @Override public Integer getValue() { @@ -50,7 +59,7 @@ public final class CodaHaleMetricsTracker extends MetricsTracker } }); - registry.register(MetricRegistry.name(poolName, "pool", "IdleConnections"), + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS), new Gauge() { @Override public Integer getValue() { @@ -58,7 +67,7 @@ public final class CodaHaleMetricsTracker extends MetricsTracker } }); - registry.register(MetricRegistry.name(poolName, "pool", "ActiveConnections"), + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS), new Gauge() { @Override public Integer getValue() { @@ -66,7 +75,7 @@ public final class CodaHaleMetricsTracker extends MetricsTracker } }); - registry.register(MetricRegistry.name(poolName, "pool", "PendingConnections"), + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS), new Gauge() { @Override public Integer getValue() { @@ -79,12 +88,13 @@ public final class CodaHaleMetricsTracker extends MetricsTracker @Override public void close() { - registry.remove(MetricRegistry.name(poolName, "pool", "Wait")); - registry.remove(MetricRegistry.name(poolName, "pool", "Usage")); - registry.remove(MetricRegistry.name(poolName, "pool", "TotalConnections")); - registry.remove(MetricRegistry.name(poolName, "pool", "IdleConnections")); - registry.remove(MetricRegistry.name(poolName, "pool", "ActiveConnections")); - registry.remove(MetricRegistry.name(poolName, "pool", "PendingConnections")); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_USAGE)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TIMEOUT_RATE)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS)); + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS)); } /** {@inheritDoc} */ diff --git a/src/test/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTrackerTest.java b/src/test/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTrackerTest.java new file mode 100644 index 00000000..5245f57a --- /dev/null +++ b/src/test/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTrackerTest.java @@ -0,0 +1,39 @@ +package com.zaxxer.hikari.metrics.dropwizard; + +import com.codahale.metrics.MetricRegistry; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class CodaHaleMetricsTrackerTest { + + @Mock + public MetricRegistry mockMetricRegistry; + + private CodaHaleMetricsTracker testee; + + @Before + public void setup(){ + testee = new CodaHaleMetricsTracker("mypool", null, mockMetricRegistry); + } + + @Test + public void close() throws Exception { + testee.close(); + + verify(mockMetricRegistry).remove("mypool.pool.Wait"); + verify(mockMetricRegistry).remove("mypool.pool.Usage"); + verify(mockMetricRegistry).remove("mypool.pool.ConnectionTimeoutRate"); + verify(mockMetricRegistry).remove("mypool.pool.TotalConnections"); + verify(mockMetricRegistry).remove("mypool.pool.IdleConnections"); + verify(mockMetricRegistry).remove("mypool.pool.ActiveConnections"); + verify(mockMetricRegistry).remove("mypool.pool.PendingConnections"); + } + +}