|
|
|
@ -2,33 +2,32 @@ package com.zaxxer.hikari.metrics.micrometer;
|
|
|
|
|
|
|
|
|
|
import com.zaxxer.hikari.metrics.IMetricsTracker;
|
|
|
|
|
import com.zaxxer.hikari.metrics.PoolStats;
|
|
|
|
|
import io.micrometer.core.instrument.DistributionSummary;
|
|
|
|
|
import io.micrometer.core.instrument.Counter;
|
|
|
|
|
import io.micrometer.core.instrument.Gauge;
|
|
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
|
|
|
import io.micrometer.core.instrument.Timer;
|
|
|
|
|
import io.micrometer.core.instrument.stats.hist.Histogram;
|
|
|
|
|
import io.micrometer.core.instrument.stats.quantile.WindowSketchQuantiles;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import static io.micrometer.core.instrument.stats.hist.CumulativeHistogram.buckets;
|
|
|
|
|
import static io.micrometer.core.instrument.stats.hist.CumulativeHistogram.linear;
|
|
|
|
|
|
|
|
|
|
public class MicrometerMetricsTracker implements IMetricsTracker
|
|
|
|
|
{
|
|
|
|
|
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_CONNECT = "ConnectionCreation";
|
|
|
|
|
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";
|
|
|
|
|
private static final String METRIC_NAME_WAIT = "hikaricp.connections.acquire";
|
|
|
|
|
private static final String METRIC_NAME_USAGE = "hikaricp.connections.usage";
|
|
|
|
|
private static final String METRIC_NAME_CONNECT = "hikaricp.connections.creation";
|
|
|
|
|
|
|
|
|
|
private static final String METRIC_NAME_TIMEOUT_RATE = "hikaricp.connections.timeout";
|
|
|
|
|
private static final String METRIC_NAME_TOTAL_CONNECTIONS = "hikaricp.connections";
|
|
|
|
|
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 final Timer connectionObtainTimer;
|
|
|
|
|
private final DistributionSummary connectionTimeoutMeter;
|
|
|
|
|
private final DistributionSummary connectionUsage;
|
|
|
|
|
private final DistributionSummary connectionCreation;
|
|
|
|
|
private final Counter connectionTimeoutCounter;
|
|
|
|
|
private final Timer connectionUsage;
|
|
|
|
|
private final Timer connectionCreation;
|
|
|
|
|
@SuppressWarnings({"FieldCanBeLocal", "unused"})
|
|
|
|
|
private final Gauge totalConnectionGauge;
|
|
|
|
|
@SuppressWarnings({"FieldCanBeLocal", "unused"})
|
|
|
|
@ -40,51 +39,49 @@ public class MicrometerMetricsTracker implements IMetricsTracker
|
|
|
|
|
|
|
|
|
|
MicrometerMetricsTracker(final String poolName, final PoolStats poolStats, final MeterRegistry meterRegistry)
|
|
|
|
|
{
|
|
|
|
|
this.connectionObtainTimer = meterRegistry
|
|
|
|
|
.timerBuilder(METRIC_NAME_WAIT)
|
|
|
|
|
this.connectionObtainTimer = Timer.builder(METRIC_NAME_WAIT)
|
|
|
|
|
.description("Connection acquire time")
|
|
|
|
|
.quantiles(WindowSketchQuantiles.quantiles(0.5, 0.95).create())
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.connectionCreation = meterRegistry
|
|
|
|
|
.summaryBuilder(METRIC_NAME_CONNECT)
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
this.connectionCreation = Timer.builder(METRIC_NAME_CONNECT)
|
|
|
|
|
.description("Connection creation time")
|
|
|
|
|
.quantiles(WindowSketchQuantiles.quantiles(0.5, 0.95).create())
|
|
|
|
|
.histogram(buckets(linear(0, 10, 20), TimeUnit.MILLISECONDS))
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
this.connectionUsage = meterRegistry
|
|
|
|
|
.summaryBuilder(METRIC_NAME_USAGE)
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.connectionUsage = Timer.builder(METRIC_NAME_USAGE)
|
|
|
|
|
.description("Connection usage time")
|
|
|
|
|
.quantiles(WindowSketchQuantiles.quantiles(0.5, 0.95).create())
|
|
|
|
|
.histogram(buckets(linear(0, 10, 20), TimeUnit.MILLISECONDS))
|
|
|
|
|
.create();
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.connectionTimeoutMeter = meterRegistry
|
|
|
|
|
.summaryBuilder(METRIC_NAME_TIMEOUT_RATE)
|
|
|
|
|
this.connectionTimeoutCounter = Counter.builder(METRIC_NAME_TIMEOUT_RATE)
|
|
|
|
|
.description("Connection timeout total count")
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.quantiles(WindowSketchQuantiles.quantiles(0.5, 0.95).create())
|
|
|
|
|
.histogram(buckets(linear(0, 10, 20), TimeUnit.MILLISECONDS))
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.totalConnectionGauge = meterRegistry
|
|
|
|
|
.gaugeBuilder(METRIC_NAME_TOTAL_CONNECTIONS, Integer.class, (i) -> poolStats.getTotalConnections())
|
|
|
|
|
this.totalConnectionGauge = Gauge.builder(METRIC_NAME_TOTAL_CONNECTIONS, poolStats, PoolStats::getTotalConnections)
|
|
|
|
|
.description("Total connections")
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.idleConnectionGauge = meterRegistry
|
|
|
|
|
.gaugeBuilder(METRIC_NAME_IDLE_CONNECTIONS, Integer.class, (i) -> poolStats.getIdleConnections())
|
|
|
|
|
this.idleConnectionGauge = Gauge.builder(METRIC_NAME_IDLE_CONNECTIONS, poolStats, PoolStats::getIdleConnections)
|
|
|
|
|
.description("Idle connections")
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.activeConnectionGauge = meterRegistry
|
|
|
|
|
.gaugeBuilder(METRIC_NAME_ACTIVE_CONNECTIONS, Integer.class, (i) -> poolStats.getActiveConnections())
|
|
|
|
|
this.activeConnectionGauge = Gauge.builder(METRIC_NAME_ACTIVE_CONNECTIONS, poolStats, PoolStats::getActiveConnections)
|
|
|
|
|
.description("Active connections")
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
this.pendingConnectionGauge = meterRegistry
|
|
|
|
|
.gaugeBuilder(METRIC_NAME_PENDING_CONNECTIONS, Integer.class, (i) -> poolStats.getPendingThreads())
|
|
|
|
|
this.pendingConnectionGauge = Gauge.builder(METRIC_NAME_PENDING_CONNECTIONS, poolStats, PoolStats::getPendingThreads)
|
|
|
|
|
.description("Pending threads")
|
|
|
|
|
.tags(METRIC_CATEGORY, poolName)
|
|
|
|
|
.create();
|
|
|
|
|
.register(meterRegistry);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@ -98,18 +95,18 @@ public class MicrometerMetricsTracker implements IMetricsTracker
|
|
|
|
|
@Override
|
|
|
|
|
public void recordConnectionUsageMillis(final long elapsedBorrowedMillis)
|
|
|
|
|
{
|
|
|
|
|
connectionUsage.record(elapsedBorrowedMillis);
|
|
|
|
|
connectionUsage.record(elapsedBorrowedMillis, TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void recordConnectionTimeout()
|
|
|
|
|
{
|
|
|
|
|
connectionTimeoutMeter.count();
|
|
|
|
|
connectionTimeoutCounter.increment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void recordConnectionCreatedMillis(long connectionCreatedMillis)
|
|
|
|
|
{
|
|
|
|
|
connectionCreation.record(connectionCreatedMillis);
|
|
|
|
|
connectionCreation.record(connectionCreatedMillis, TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|