Skip to content

Commit

Permalink
Use metricsPrefix rather than poolname + "pool"
Browse files Browse the repository at this point in the history
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 brettwooldridge#615
  • Loading branch information
EdgeCaseBerg committed Apr 30, 2016
1 parent 92f1910 commit 11055d1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,45 @@

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<Integer>() {
@Override
public Integer getValue() {
return poolStats.getTotalConnections();
}
});

registry.register(MetricRegistry.name(poolName, "pool", "IdleConnections"),
registry.register(MetricRegistry.name(metricsPrefix, "IdleConnections"),
new Gauge<Integer>() {
@Override
public Integer getValue() {
return poolStats.getIdleConnections();
}
});

registry.register(MetricRegistry.name(poolName, "pool", "ActiveConnections"),
registry.register(MetricRegistry.name(metricsPrefix, "ActiveConnections"),
new Gauge<Integer>() {
@Override
public Integer getValue() {
return poolStats.getActiveConnections();
}
});

registry.register(MetricRegistry.name(poolName, "pool", "PendingConnections"),
registry.register(MetricRegistry.name(metricsPrefix, "PendingConnections"),
new Gauge<Integer>() {
@Override
public Integer getValue() {
Expand All @@ -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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ 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) {
SortedMap<String,Timer> timers = metricRegistry.getTimers(new MetricFilter() {
@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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 11055d1

Please sign in to comment.