Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable Metrics prefix #624

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/zaxxer/hikari/HikariConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
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