From 92f191063cbb314a2ed348c2920e2435ac6838f2 Mon Sep 17 00:00:00 2001 From: Ethan Joachim Eldridge Date: Sat, 30 Apr 2016 11:28:21 -0400 Subject: [PATCH 1/2] Add metricsPrefix field to HikariConfig Adding `metricsPrefix` field to HikariConfig and a default of "pool" because that's what it is currently. This is work towards #615 Setting the metricsPrefix by default to the poolname + "pool" so that it matches what's going on in the CodahaleHealthChecker and it will be easier to update that and keep the old behavior for other people who don't need to set a custom prefix on there metrics. --- .../java/com/zaxxer/hikari/HikariConfig.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index 31dff494d..ee8ebd82a 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -54,6 +54,7 @@ public class HikariConfig implements HikariConfigMXBean private static final long IDLE_TIMEOUT = MINUTES.toMillis(10); private static final long MAX_LIFETIME = MINUTES.toMillis(30); private static final int DEFAULT_POOL_SIZE = 10; + private static final String METRICS_PREFIX = "pool"; private static boolean unitTest; @@ -80,6 +81,7 @@ public class HikariConfig implements HikariConfigMXBean private String poolName; private String transactionIsolationName; private String username; + private String metricsPrefix; private boolean isAutoCommit; private boolean isReadOnly; private boolean isInitializationFailFast; @@ -719,6 +721,27 @@ public void setUsername(String username) this.username = username; } + /** + * Get the default metricsPrefix used metrics + * + * @return the username + */ + public String getMetricsPrefix() + { + return metricsPrefix; + } + + + /** + * Set the default prefix used for metrics. + * + * @param metricsPrefix the prefix for metrics + */ + public void setMetricsPrefix(String metricsPrefix) + { + this.metricsPrefix = metricsPrefix; + } + /** * Get the thread factory used to create threads. * @@ -757,6 +780,11 @@ else if (isRegisterMbeans && poolName.contains(":")) { dataSourceJndiName = getNullIfEmpty(dataSourceJndiName); driverClassName = getNullIfEmpty(driverClassName); jdbcUrl = getNullIfEmpty(jdbcUrl); + metricsPrefix = getNullIfEmpty(metricsPrefix); + + if (metricsPrefix == null) { + metricsPrefix = poolName + "." + METRICS_PREFIX; + } // Check Data Source Options if (dataSource != null) { From 11055d1281a7fa8acc3bcd9b15ad9031d5d8291d Mon Sep 17 00:00:00 2001 From: Ethan Joachim Eldridge Date: Sat, 30 Apr 2016 12:51:32 -0400 Subject: [PATCH 2/2] Use metricsPrefix rather than poolname + "pool" Updated metrics related classes to use the metricsPrefix from HikariConfig rather than a combination of the poolname (from config) plus the hard coded word "pool". This gives more control to users who need to set the prefixes of their metrics to something specific such as the person who raised #615 --- .../hikari/metrics/MetricsTrackerFactory.java | 4 +-- .../dropwizard/CodaHaleMetricsTracker.java | 32 +++++++++---------- .../dropwizard/CodahaleHealthChecker.java | 6 ++-- .../CodahaleMetricsTrackerFactory.java | 4 +-- .../com/zaxxer/hikari/pool/HikariPool.java | 2 +- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/metrics/MetricsTrackerFactory.java b/src/main/java/com/zaxxer/hikari/metrics/MetricsTrackerFactory.java index 380635b58..248d2d3ec 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/MetricsTrackerFactory.java +++ b/src/main/java/com/zaxxer/hikari/metrics/MetricsTrackerFactory.java @@ -21,9 +21,9 @@ public interface MetricsTrackerFactory /** * Create an instance of a MetricsTracker. * - * @param poolName the name of the pool + * @param metricsPrefix the name used to prefix metrics * @param poolStats a PoolStats instance to use * @return a MetricsTracker implementation instance */ - MetricsTracker create(String poolName, PoolStats poolStats); + MetricsTracker create(String metricsPrefix, PoolStats poolStats); } 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 b34f72e23..b04582f39 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java +++ b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodaHaleMetricsTracker.java @@ -28,21 +28,21 @@ public final class CodaHaleMetricsTracker extends MetricsTracker { - private final String poolName; + private final String metricsPrefix; private final Timer connectionObtainTimer; private final Histogram connectionUsage; private final Meter connectionTimeoutMeter; private final MetricRegistry registry; - public CodaHaleMetricsTracker(final String poolName, final PoolStats poolStats, final MetricRegistry registry) + public CodaHaleMetricsTracker(final String metricsPrefix, final PoolStats poolStats, final MetricRegistry registry) { - this.poolName = poolName; + this.metricsPrefix = metricsPrefix; 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(metricsPrefix, "Wait")); + this.connectionUsage = registry.histogram(MetricRegistry.name(metricsPrefix, "Usage")); + this.connectionTimeoutMeter = registry.meter(MetricRegistry.name(metricsPrefix, "ConnectionTimeoutRate")); - registry.register(MetricRegistry.name(poolName, "pool", "TotalConnections"), + registry.register(MetricRegistry.name(metricsPrefix, "TotalConnections"), new Gauge() { @Override public Integer getValue() { @@ -50,7 +50,7 @@ public Integer getValue() { } }); - registry.register(MetricRegistry.name(poolName, "pool", "IdleConnections"), + registry.register(MetricRegistry.name(metricsPrefix, "IdleConnections"), new Gauge() { @Override public Integer getValue() { @@ -58,7 +58,7 @@ public Integer getValue() { } }); - registry.register(MetricRegistry.name(poolName, "pool", "ActiveConnections"), + registry.register(MetricRegistry.name(metricsPrefix, "ActiveConnections"), new Gauge() { @Override public Integer getValue() { @@ -66,7 +66,7 @@ public Integer getValue() { } }); - registry.register(MetricRegistry.name(poolName, "pool", "PendingConnections"), + registry.register(MetricRegistry.name(metricsPrefix, "PendingConnections"), new Gauge() { @Override public Integer getValue() { @@ -79,12 +79,12 @@ public Integer getValue() { @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(metricsPrefix, "Wait")); + registry.remove(MetricRegistry.name(metricsPrefix, "Usage")); + registry.remove(MetricRegistry.name(metricsPrefix, "TotalConnections")); + registry.remove(MetricRegistry.name(metricsPrefix, "IdleConnections")); + registry.remove(MetricRegistry.name(metricsPrefix, "ActiveConnections")); + registry.remove(MetricRegistry.name(metricsPrefix, "PendingConnections")); } /** {@inheritDoc} */ diff --git a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleHealthChecker.java b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleHealthChecker.java index df5ad71f2..bbb9f5d61 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleHealthChecker.java +++ b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleHealthChecker.java @@ -61,7 +61,7 @@ public static void registerHealthChecks(final HikariPool pool, final HikariConfi final MetricRegistry metricRegistry = (MetricRegistry) hikariConfig.getMetricRegistry(); final long checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs", String.valueOf(hikariConfig.getConnectionTimeout()))); - registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "ConnectivityCheck"), new ConnectivityHealthCheck(pool, checkTimeoutMs)); + registry.register(MetricRegistry.name(hikariConfig.getMetricsPrefix(), "ConnectivityCheck"), new ConnectivityHealthCheck(pool, checkTimeoutMs)); final long expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty("expected99thPercentileMs", "0")); if (metricRegistry != null && expected99thPercentile > 0) { @@ -69,13 +69,13 @@ public static void registerHealthChecks(final HikariPool pool, final HikariConfi @Override public boolean matches(String name, Metric metric) { - return name.equals(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Wait")); + return name.equals(MetricRegistry.name(hikariConfig.getMetricsPrefix(), "Wait")); } }); if (!timers.isEmpty()) { final Timer timer = timers.entrySet().iterator().next().getValue(); - registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Connection99Percent"), new Connection99Percent(timer, expected99thPercentile)); + registry.register(MetricRegistry.name(hikariConfig.getMetricsPrefix(), "Connection99Percent"), new Connection99Percent(timer, expected99thPercentile)); } } } diff --git a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleMetricsTrackerFactory.java b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleMetricsTrackerFactory.java index 53d0aab25..53e5ca4fb 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleMetricsTrackerFactory.java +++ b/src/main/java/com/zaxxer/hikari/metrics/dropwizard/CodahaleMetricsTrackerFactory.java @@ -36,8 +36,8 @@ public MetricRegistry getRegistry() } @Override - public MetricsTracker create(String poolName, PoolStats poolStats) + public MetricsTracker create(String metricsPrefix, PoolStats poolStats) { - return new CodaHaleMetricsTracker(poolName, poolStats, registry); + return new CodaHaleMetricsTracker(metricsPrefix, poolStats, registry); } } diff --git a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java index 7bb3c3c42..44a26c3d9 100644 --- a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java @@ -286,7 +286,7 @@ public void setMetricRegistry(Object metricRegistry) public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory) { if (metricsTrackerFactory != null) { - this.metricsTracker = new MetricsTrackerDelegate(metricsTrackerFactory.create(config.getPoolName(), getPoolStats())); + this.metricsTracker = new MetricsTrackerDelegate(metricsTrackerFactory.create(config.getMetricsPrefix(), getPoolStats())); } else { this.metricsTracker = new NopMetricsTrackerDelegate();