Add pool metrics (#1110)

pull/1115/head
Manabu Matsuzaki 7 years ago committed by Brett Wooldridge
parent 4852678a7b
commit 3313174891

@ -34,13 +34,15 @@ public abstract class PoolStats
protected volatile int idleConnections;
protected volatile int activeConnections;
protected volatile int pendingThreads;
protected volatile int maxConnections;
protected volatile int minConnections;
public PoolStats(final long timeoutMs)
{
this.timeoutMs = timeoutMs;
this.reloadAt = new AtomicLong();
}
public int getTotalConnections()
{
if (shouldLoad()) {
@ -77,6 +79,22 @@ public abstract class PoolStats
return pendingThreads;
}
public int getMaxConnections() {
if (shouldLoad()) {
update();
}
return maxConnections;
}
public int getMinConnections() {
if (shouldLoad()) {
update();
}
return minConnections;
}
protected abstract void update();
private boolean shouldLoad()

@ -44,6 +44,8 @@ public final class CodaHaleMetricsTracker implements IMetricsTracker
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";
private static final String METRIC_NAME_MAX_CONNECTIONS = "MaxConnections";
private static final String METRIC_NAME_MIN_CONNECTIONS = "MinConnections";
public CodaHaleMetricsTracker(final String poolName, final PoolStats poolStats, final MetricRegistry registry)
{
@ -85,6 +87,22 @@ public final class CodaHaleMetricsTracker implements IMetricsTracker
return poolStats.getPendingThreads();
}
});
registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MAX_CONNECTIONS),
new Gauge<Integer>() {
@Override
public Integer getValue() {
return poolStats.getMaxConnections();
}
});
registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MIN_CONNECTIONS),
new Gauge<Integer>() {
@Override
public Integer getValue() {
return poolStats.getMinConnections();
}
});
}
/** {@inheritDoc} */
@ -99,6 +117,8 @@ public final class CodaHaleMetricsTracker implements IMetricsTracker
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));
registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MAX_CONNECTIONS));
registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MIN_CONNECTIONS));
}
/** {@inheritDoc} */

@ -21,6 +21,8 @@ public class MicrometerMetricsTracker implements IMetricsTracker
private static final String METRIC_NAME_IDLE_CONNECTIONS = "hikaricp.connections.idle";
private static final String METRIC_NAME_ACTIVE_CONNECTIONS = "hikaricp.connections.active";
private static final String METRIC_NAME_PENDING_CONNECTIONS = "hikaricp.connections.pending";
private static final String METRIC_NAME_MAX_CONNECTIONS = "hikaricp.connections.max";
private static final String METRIC_NAME_MIN_CONNECTIONS = "hikaricp.connections.min";
private final Timer connectionObtainTimer;
private final Counter connectionTimeoutCounter;
@ -35,6 +37,10 @@ public class MicrometerMetricsTracker implements IMetricsTracker
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final Gauge pendingConnectionGauge;
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final Gauge maxConnectionGauge;
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final Gauge minConnectionGauge;
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private final PoolStats poolStats;
MicrometerMetricsTracker(final String poolName, final PoolStats poolStats, final MeterRegistry meterRegistry)
@ -84,6 +90,16 @@ public class MicrometerMetricsTracker implements IMetricsTracker
.tags(METRIC_CATEGORY, poolName)
.register(meterRegistry);
this.maxConnectionGauge = Gauge.builder(METRIC_NAME_MAX_CONNECTIONS, poolStats, PoolStats::getMaxConnections)
.description("Max connections")
.tags(METRIC_CATEGORY, poolName)
.register(meterRegistry);
this.minConnectionGauge = Gauge.builder(METRIC_NAME_MIN_CONNECTIONS, poolStats, PoolStats::getMinConnections)
.description("Min connections")
.tags(METRIC_CATEGORY, poolName)
.register(meterRegistry);
}
/** {@inheritDoc} */

@ -42,7 +42,11 @@ class HikariCPCollector extends Collector {
createGauge("hikaricp_pending_threads", "Pending threads",
PoolStats::getPendingThreads),
createGauge("hikaricp_connections", "The number of current connections",
PoolStats::getTotalConnections)
PoolStats::getTotalConnections),
createGauge("hikaricp_max_connections", "Max connections",
PoolStats::getMaxConnections),
createGauge("hikaricp_min_connections", "Min connections",
PoolStats::getMinConnections)
);
}

@ -638,6 +638,8 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
this.idleConnections = HikariPool.this.getIdleConnections();
this.totalConnections = HikariPool.this.getTotalConnections();
this.activeConnections = HikariPool.this.getActiveConnections();
this.maxConnections = config.getMaximumPoolSize();
this.minConnections = config.getMinimumIdle();
}
};
}

@ -35,5 +35,7 @@ public class CodaHaleMetricsTrackerTest {
verify(mockMetricRegistry).remove("mypool.pool.IdleConnections");
verify(mockMetricRegistry).remove("mypool.pool.ActiveConnections");
verify(mockMetricRegistry).remove("mypool.pool.PendingConnections");
verify(mockMetricRegistry).remove("mypool.pool.MaxConnections");
verify(mockMetricRegistry).remove("mypool.pool.MinConnections");
}
}

@ -45,6 +45,8 @@ public class HikariCPCollectorTest {
assertThat(getValue("hikaricp_idle_connections", "noConnection"), is(0.0));
assertThat(getValue("hikaricp_pending_threads", "noConnection"), is(0.0));
assertThat(getValue("hikaricp_connections", "noConnection"), is(0.0));
assertThat(getValue("hikaricp_max_connections", "noConnection"), is(10.0));
assertThat(getValue("hikaricp_min_connections", "noConnection"), is(0.0));
}
finally {
StubConnection.slowCreate = false;
@ -65,6 +67,8 @@ public class HikariCPCollectorTest {
assertThat(getValue("hikaricp_idle_connections", poolName), is(0.0));
assertThat(getValue("hikaricp_pending_threads", poolName), is(0.0));
assertThat(getValue("hikaricp_connections", poolName), is(0.0));
assertThat(getValue("hikaricp_max_connections", poolName), is(10.0));
assertThat(getValue("hikaricp_min_connections", poolName), is(0.0));
}
finally {
StubConnection.slowCreate = false;
@ -88,6 +92,8 @@ public class HikariCPCollectorTest {
assertThat(getValue("hikaricp_idle_connections", "connection1"), is(0.0));
assertThat(getValue("hikaricp_pending_threads", "connection1"), is(0.0));
assertThat(getValue("hikaricp_connections", "connection1"), is(1.0));
assertThat(getValue("hikaricp_max_connections", "connection1"), is(1.0));
assertThat(getValue("hikaricp_min_connections", "connection1"), is(1.0));
}
finally {
StubConnection.slowCreate = false;
@ -111,6 +117,8 @@ public class HikariCPCollectorTest {
assertThat(getValue("hikaricp_idle_connections", "connectionClosed"), is(1.0));
assertThat(getValue("hikaricp_pending_threads", "connectionClosed"), is(0.0));
assertThat(getValue("hikaricp_connections", "connectionClosed"), is(1.0));
assertThat(getValue("hikaricp_max_connections", "connectionClosed"), is(1.0));
assertThat(getValue("hikaricp_min_connections", "connectionClosed"), is(1.0));
}
finally {
StubConnection.slowCreate = false;

Loading…
Cancel
Save