com.despegar.jdbc.galera.utils.PoolMetrics Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of galera-java-client Show documentation
Show all versions of galera-java-client Show documentation
A simple Java client for MariaDB Galera Cluster and Percona XtraDB Cluster. It is designed to be an
alternative to connect JVM app to
MariaDB/Percona galera nodes without HAProxy
package com.despegar.jdbc.galera.utils;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.despegar.jdbc.galera.listener.GaleraClientListener;
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
import java.util.SortedMap;
import static com.despegar.jdbc.galera.utils.PoolNameHelper.getFullPoolName;
public class PoolMetrics {
private static final Logger LOG = LoggerFactory.getLogger(PoolMetrics.class);
public static final String METRIC_NAME_POOL_WAIT = ".pool.Wait";
public static final String METRIC_NAME_POOL_USAGE = ".pool.Usage";
public static final String METRIC_NAME_TOTAL_CONN = ".pool.TotalConnections";
public static final String METRIC_NAME_IDLE_CONN = ".pool.IdleConnections";
public static final String METRIC_NAME_ACTIVE_CONN = ".pool.ActiveConnections";
public static final String METRIC_NAME_PENDING_CONN = ".pool.PendingConnections";
public static void reportMetrics(MetricRegistry metricRegistry, Set nodes, GaleraClientListener listener, Optional poolName) {
if (metricRegistry == null || nodes == null) {
return;
}
for (String nodeName : nodes) {
final String poolFullName = getFullPoolName(poolName, nodeName);
Optional waitPercentile95 = getTimerPercentile95(metricRegistry, poolFullName + METRIC_NAME_POOL_WAIT);
Optional usagePercentile95 = getHistogramPercentile95(metricRegistry, poolFullName + METRIC_NAME_POOL_USAGE);
Optional totalConnections = getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_TOTAL_CONN);
Optional idleConnections = getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_IDLE_CONN);
Optional activeConnections = getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_ACTIVE_CONN);
Optional waitingForConnections = getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_PENDING_CONN);
listener.onDiscoveryPoolMetrics(poolFullName, waitPercentile95, usagePercentile95, totalConnections, idleConnections, activeConnections,
waitingForConnections);
}
}
private static Optional getGaugeValue(MetricRegistry metricRegistry, String metricName) {
SortedMap gauges = metricRegistry.getGauges();
if (gauges != null && gauges.containsKey(metricName)) {
return Optional.of((Integer) gauges.get(metricName).getValue());
}
return Optional.absent();
}
private static Optional getTimerPercentile95(MetricRegistry metricRegistry, String metricName) {
SortedMap timers = metricRegistry.getTimers();
if (timers != null && timers.containsKey(metricName)) {
return Optional.of(timers.get(metricName).getSnapshot().get95thPercentile());
}
return Optional.absent();
}
private static Optional getHistogramPercentile95(MetricRegistry metricRegistry, String metricName) {
SortedMap histograms = metricRegistry.getHistograms();
if (histograms != null && histograms.containsKey(metricName)) {
return Optional.of(histograms.get(metricName).getSnapshot().get95thPercentile());
}
return Optional.absent();
}
}