com.despegar.jdbc.galera.metrics.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.metrics;
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.GaleraNode;
import com.despegar.jdbc.galera.listener.GaleraClientListener;
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.SortedMap;
import static com.despegar.jdbc.galera.utils.PoolNameHelper.getFullPoolName;
import static com.despegar.jdbc.galera.utils.PoolNameHelper.nodeNameWithoutPort;
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, Map nodes, GaleraClientListener listener, Optional poolName) {
if (metricRegistry == null || nodes == null) {
return;
}
for (String nodeName : nodes.keySet()) {
final String poolFullName = getFullPoolName(poolName, nodeName);
HikariMetrics hikariMetrics = HikariMetrics.newBuilder()
.waitPercentile95(getTimerPercentile95(metricRegistry, poolFullName + METRIC_NAME_POOL_WAIT))
.usagePercentile95(getHistogramPercentile95(metricRegistry, poolFullName + METRIC_NAME_POOL_USAGE))
.totalConnections(getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_TOTAL_CONN))
.idleConnections(getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_IDLE_CONN))
.activeConnections(getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_ACTIVE_CONN))
.waitingForConnections(getGaugeValue(metricRegistry, poolFullName + METRIC_NAME_PENDING_CONN)).build();
Optional threadsConnected = getThreadsConnected(nodes.get(nodeName));
listener.onDiscoveryPoolMetrics(nodeNameWithoutPort(nodeName), poolFullName, hikariMetrics, threadsConnected);
}
}
private static Optional getThreadsConnected(GaleraNode galeraNode) {
Optional threadsConnected = Optional.absent();
try {
threadsConnected = Optional.fromNullable(galeraNode.status().threadsConnectedCount());
} catch (Exception e) {
LOG.warn("Error getting threadsConnected metric", e);
}
return threadsConnected;
}
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();
}
}