|
| 1 | +package com.zaxxer.hikari.metrics.dropwizard; |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit; |
| 4 | + |
| 5 | +import com.zaxxer.hikari.metrics.IMetricsTracker; |
| 6 | +import com.zaxxer.hikari.metrics.PoolStats; |
| 7 | +import io.dropwizard.metrics5.Gauge; |
| 8 | +import io.dropwizard.metrics5.Histogram; |
| 9 | +import io.dropwizard.metrics5.Meter; |
| 10 | +import io.dropwizard.metrics5.MetricRegistry; |
| 11 | +import io.dropwizard.metrics5.Timer; |
| 12 | + |
| 13 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_CATEGORY; |
| 14 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_ACTIVE_CONNECTIONS; |
| 15 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_CONNECT; |
| 16 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_IDLE_CONNECTIONS; |
| 17 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_MAX_CONNECTIONS; |
| 18 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_MIN_CONNECTIONS; |
| 19 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_PENDING_CONNECTIONS; |
| 20 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_TIMEOUT_RATE; |
| 21 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_TOTAL_CONNECTIONS; |
| 22 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_USAGE; |
| 23 | +import static com.zaxxer.hikari.metrics.dropwizard.DropwizardCommon.METRIC_NAME_WAIT; |
| 24 | + |
| 25 | +public class Dropwizard5MetricsTracker implements IMetricsTracker |
| 26 | +{ |
| 27 | + private final String poolName; |
| 28 | + private final Timer connectionObtainTimer; |
| 29 | + private final Histogram connectionUsage; |
| 30 | + private final Histogram connectionCreation; |
| 31 | + private final Meter connectionTimeoutMeter; |
| 32 | + private final MetricRegistry registry; |
| 33 | + |
| 34 | + Dropwizard5MetricsTracker(final String poolName, final PoolStats poolStats, final MetricRegistry registry) |
| 35 | + { |
| 36 | + this.poolName = poolName; |
| 37 | + this.registry = registry; |
| 38 | + this.connectionObtainTimer = registry.timer(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT)); |
| 39 | + this.connectionUsage = registry.histogram(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_USAGE)); |
| 40 | + this.connectionCreation = registry.histogram(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_CONNECT)); |
| 41 | + this.connectionTimeoutMeter = registry.meter(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TIMEOUT_RATE)); |
| 42 | + |
| 43 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS), |
| 44 | + (Gauge<Integer>) poolStats::getTotalConnections); |
| 45 | + |
| 46 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS), |
| 47 | + (Gauge<Integer>) poolStats::getIdleConnections); |
| 48 | + |
| 49 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS), |
| 50 | + (Gauge<Integer>) poolStats::getActiveConnections); |
| 51 | + |
| 52 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS), |
| 53 | + (Gauge<Integer>) poolStats::getPendingThreads); |
| 54 | + |
| 55 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MAX_CONNECTIONS), |
| 56 | + (Gauge<Integer>) poolStats::getMaxConnections); |
| 57 | + |
| 58 | + registry.register(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MIN_CONNECTIONS), |
| 59 | + (Gauge<Integer>) poolStats::getMinConnections); |
| 60 | + } |
| 61 | + |
| 62 | + /** {@inheritDoc} */ |
| 63 | + @Override |
| 64 | + public void close() |
| 65 | + { |
| 66 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT)); |
| 67 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_USAGE)); |
| 68 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_CONNECT)); |
| 69 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TIMEOUT_RATE)); |
| 70 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS)); |
| 71 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS)); |
| 72 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS)); |
| 73 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS)); |
| 74 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MAX_CONNECTIONS)); |
| 75 | + registry.remove(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_MIN_CONNECTIONS)); |
| 76 | + } |
| 77 | + |
| 78 | + /** {@inheritDoc} */ |
| 79 | + @Override |
| 80 | + public void recordConnectionAcquiredNanos(final long elapsedAcquiredNanos) |
| 81 | + { |
| 82 | + connectionObtainTimer.update(elapsedAcquiredNanos, TimeUnit.NANOSECONDS); |
| 83 | + } |
| 84 | + |
| 85 | + /** {@inheritDoc} */ |
| 86 | + @Override |
| 87 | + public void recordConnectionUsageMillis(final long elapsedBorrowedMillis) |
| 88 | + { |
| 89 | + connectionUsage.update(elapsedBorrowedMillis); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void recordConnectionTimeout() |
| 94 | + { |
| 95 | + connectionTimeoutMeter.mark(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void recordConnectionCreatedMillis(final long connectionCreatedMillis) |
| 100 | + { |
| 101 | + connectionCreation.update(connectionCreatedMillis); |
| 102 | + } |
| 103 | + |
| 104 | + public Timer getConnectionAcquisitionTimer() |
| 105 | + { |
| 106 | + return connectionObtainTimer; |
| 107 | + } |
| 108 | + |
| 109 | + public Histogram getConnectionDurationHistogram() |
| 110 | + { |
| 111 | + return connectionUsage; |
| 112 | + } |
| 113 | + |
| 114 | + public Histogram getConnectionCreationHistogram() |
| 115 | + { |
| 116 | + return connectionCreation; |
| 117 | + } |
| 118 | +} |
0 commit comments