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

Update micrometer #2223

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<jndi.version>0.11.4.1</jndi.version>
<maven.release.version>3.0.1</maven.release.version>
<metrics.version>3.2.5</metrics.version>
<micrometer.version>1.5.10</micrometer.version>
<simpleclient.version>0.9.0</simpleclient.version>
<micrometer.version>1.13.2</micrometer.version>
<prometheus-metrics.version>1.3.1</prometheus-metrics.version>
<mockito.version>3.7.7</mockito.version>
<pax.exam.version>4.13.5</pax.exam.version>
<pax.url.version>2.5.4</pax.url.version>
Expand Down Expand Up @@ -199,8 +199,8 @@
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${simpleclient.version}</version>
<artifactId>prometheus-metrics-core</artifactId>
<version>${prometheus-metrics.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@
package com.zaxxer.hikari.metrics.prometheus;

import com.zaxxer.hikari.metrics.PoolStats;
import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricMetadata;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

class HikariCPCollector extends Collector
class HikariCPCollector implements MultiCollector
{

private static final List<String> LABEL_NAMES = Collections.singletonList("pool");

private final Map<String, PoolStats> poolStatsMap = new ConcurrentHashMap<>();

@Override
public List<MetricFamilySamples> collect()
public MetricSnapshots collect()
{
return Arrays.asList(
return new MetricSnapshots(Arrays.asList(
createGauge("hikaricp_active_connections", "Active connections",
PoolStats::getActiveConnections),
createGauge("hikaricp_idle_connections", "Idle connections",
Expand All @@ -50,7 +48,7 @@ public List<MetricFamilySamples> collect()
PoolStats::getMaxConnections),
createGauge("hikaricp_min_connections", "Min connections",
PoolStats::getMinConnections)
);
));
}

void add(String name, PoolStats poolStats)
Expand All @@ -63,14 +61,13 @@ void remove(String name)
poolStatsMap.remove(name);
}

private GaugeMetricFamily createGauge(String metric, String help,
Function<PoolStats, Integer> metricValueFunction)
private GaugeSnapshot createGauge(String metric, String help,
Function<PoolStats, Integer> metricValueFunction)
{
var metricFamily = new GaugeMetricFamily(metric, help, LABEL_NAMES);
poolStatsMap.forEach((k, v) -> metricFamily.addMetric(
Collections.singletonList(k),
metricValueFunction.apply(v)
Collection<GaugeSnapshot.GaugeDataPointSnapshot> gaugeDataPointSnapshots = new ArrayList<>();
poolStatsMap.forEach((k, v) -> gaugeDataPointSnapshots.add(
new GaugeSnapshot.GaugeDataPointSnapshot(metricValueFunction.apply(v), Labels.of("pool", k), null)
));
return metricFamily;
return new GaugeSnapshot(new MetricMetadata(metric, help), gaugeDataPointSnapshots);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package com.zaxxer.hikari.metrics.prometheus;

import com.zaxxer.hikari.metrics.IMetricsTracker;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;
import io.prometheus.metrics.core.datapoints.CounterDataPoint;
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
import io.prometheus.metrics.core.metrics.Histogram;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.core.metrics.Counter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -36,11 +38,11 @@
*/
class PrometheusHistogramMetricsTracker implements IMetricsTracker
{
private static final Counter CONNECTION_TIMEOUT_COUNTER = Counter.build()
private static final Counter CONNECTION_TIMEOUT_COUNTER = Counter.builder()
.name("hikaricp_connection_timeout_total")
.labelNames("pool")
.help("Connection timeout total count")
.create();
.register();

private static final Histogram ELAPSED_ACQUIRED_HISTOGRAM =
registerHistogram("hikaricp_connection_acquired_nanos", "Connection acquired time (ns)", 1_000);
Expand All @@ -51,42 +53,42 @@ class PrometheusHistogramMetricsTracker implements IMetricsTracker
private static final Histogram ELAPSED_CREATION_HISTOGRAM =
registerHistogram("hikaricp_connection_creation_millis", "Connection creation (ms)", 1);

private final Counter.Child connectionTimeoutCounterChild;
private final CounterDataPoint connectionTimeoutCounterChild;

private static Histogram registerHistogram(String name, String help, double bucketStart) {
return Histogram.build()
return Histogram.builder()
.name(name)
.labelNames("pool")
.help(help)
.exponentialBuckets(bucketStart, 2.0, 11)
.create();
.classicExponentialUpperBounds(bucketStart, 2.0, 11)
.register();
}

private final static Map<CollectorRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();
private final static Map<PrometheusRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();

private final String poolName;
private final HikariCPCollector hikariCPCollector;

private final Histogram.Child elapsedAcquiredHistogramChild;
private final Histogram.Child elapsedBorrowedHistogramChild;
private final Histogram.Child elapsedCreationHistogramChild;
private final DistributionDataPoint elapsedAcquiredHistogramChild;
private final DistributionDataPoint elapsedBorrowedHistogramChild;
private final DistributionDataPoint elapsedCreationHistogramChild;

PrometheusHistogramMetricsTracker(String poolName, CollectorRegistry collectorRegistry, HikariCPCollector hikariCPCollector) {
PrometheusHistogramMetricsTracker(String poolName, PrometheusRegistry collectorRegistry, HikariCPCollector hikariCPCollector) {
registerMetrics(collectorRegistry);
this.poolName = poolName;
this.hikariCPCollector = hikariCPCollector;
this.connectionTimeoutCounterChild = CONNECTION_TIMEOUT_COUNTER.labels(poolName);
this.elapsedAcquiredHistogramChild = ELAPSED_ACQUIRED_HISTOGRAM.labels(poolName);
this.elapsedBorrowedHistogramChild = ELAPSED_BORROWED_HISTOGRAM.labels(poolName);
this.elapsedCreationHistogramChild = ELAPSED_CREATION_HISTOGRAM.labels(poolName);
this.connectionTimeoutCounterChild = CONNECTION_TIMEOUT_COUNTER.labelValues(poolName);
this.elapsedAcquiredHistogramChild = ELAPSED_ACQUIRED_HISTOGRAM.labelValues(poolName);
this.elapsedBorrowedHistogramChild = ELAPSED_BORROWED_HISTOGRAM.labelValues(poolName);
this.elapsedCreationHistogramChild = ELAPSED_CREATION_HISTOGRAM.labelValues(poolName);
}

private void registerMetrics(CollectorRegistry collectorRegistry) {
private void registerMetrics(PrometheusRegistry collectorRegistry) {
if (registrationStatuses.putIfAbsent(collectorRegistry, REGISTERED) == null) {
CONNECTION_TIMEOUT_COUNTER.register(collectorRegistry);
ELAPSED_ACQUIRED_HISTOGRAM.register(collectorRegistry);
ELAPSED_BORROWED_HISTOGRAM.register(collectorRegistry);
ELAPSED_CREATION_HISTOGRAM.register(collectorRegistry);
collectorRegistry.register(CONNECTION_TIMEOUT_COUNTER);
collectorRegistry.register(ELAPSED_ACQUIRED_HISTOGRAM);
collectorRegistry.register(ELAPSED_BORROWED_HISTOGRAM);
collectorRegistry.register(ELAPSED_CREATION_HISTOGRAM);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
import com.zaxxer.hikari.metrics.PoolStats;
import com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory.RegistrationStatus;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory.RegistrationStatus.*;
import static com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory.RegistrationStatus.REGISTERED;

/**
* <pre>{@code
Expand All @@ -36,25 +36,25 @@
*/
public class PrometheusHistogramMetricsTrackerFactory implements MetricsTrackerFactory {

private final static Map<CollectorRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();
private final static Map<PrometheusRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();

private final HikariCPCollector collector = new HikariCPCollector();

private final CollectorRegistry collectorRegistry;
private final PrometheusRegistry collectorRegistry;

/**
* Default Constructor. The Hikari metrics are registered to the default
* collector registry ({@code CollectorRegistry.defaultRegistry}).
*/
public PrometheusHistogramMetricsTrackerFactory() {
this(CollectorRegistry.defaultRegistry);
this(new PrometheusRegistry());
}

/**
* Constructor that allows to pass in a {@link CollectorRegistry} to which the
* Constructor that allows to pass in a {@link PrometheusRegistry} to which the
* Hikari metrics are registered.
*/
public PrometheusHistogramMetricsTrackerFactory(CollectorRegistry collectorRegistry) {
public PrometheusHistogramMetricsTrackerFactory(PrometheusRegistry collectorRegistry) {
this.collectorRegistry = collectorRegistry;
}

Expand All @@ -65,9 +65,9 @@ public IMetricsTracker create(String poolName, PoolStats poolStats) {
return new PrometheusHistogramMetricsTracker(poolName, this.collectorRegistry, this.collector);
}

private void registerCollector(Collector collector, CollectorRegistry collectorRegistry) {
private void registerCollector(MultiCollector collector, PrometheusRegistry collectorRegistry) {
if (registrationStatuses.putIfAbsent(collectorRegistry, REGISTERED) == null) {
collector.register(collectorRegistry);
collectorRegistry.register(collector);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory.RegistrationStatus;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import io.prometheus.metrics.core.datapoints.CounterDataPoint;
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
import io.prometheus.metrics.core.metrics.Counter;
import io.prometheus.metrics.core.metrics.Summary;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -30,11 +32,11 @@

class PrometheusMetricsTracker implements IMetricsTracker
{
private final static Counter CONNECTION_TIMEOUT_COUNTER = Counter.build()
private final static Counter CONNECTION_TIMEOUT_COUNTER = Counter.builder()
.name("hikaricp_connection_timeout_total")
.labelNames("pool")
.help("Connection timeout total count")
.create();
.register();

private final static Summary ELAPSED_ACQUIRED_SUMMARY =
createSummary("hikaricp_connection_acquired_nanos", "Connection acquired time (ns)");
Expand All @@ -45,35 +47,35 @@ class PrometheusMetricsTracker implements IMetricsTracker
private final static Summary ELAPSED_CREATION_SUMMARY =
createSummary("hikaricp_connection_creation_millis", "Connection creation (ms)");

private final static Map<CollectorRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();
private final static Map<PrometheusRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();

private final String poolName;
private final HikariCPCollector hikariCPCollector;

private final Counter.Child connectionTimeoutCounterChild;
private final CounterDataPoint connectionTimeoutCounterChild;

private final Summary.Child elapsedAcquiredSummaryChild;
private final Summary.Child elapsedUsageSummaryChild;
private final Summary.Child elapsedCreationSummaryChild;
private final DistributionDataPoint elapsedAcquiredSummaryChild;
private final DistributionDataPoint elapsedUsageSummaryChild;
private final DistributionDataPoint elapsedCreationSummaryChild;

PrometheusMetricsTracker(String poolName, CollectorRegistry collectorRegistry, HikariCPCollector hikariCPCollector)
PrometheusMetricsTracker(String poolName, PrometheusRegistry collectorRegistry, HikariCPCollector hikariCPCollector)
{
registerMetrics(collectorRegistry);
this.poolName = poolName;
this.hikariCPCollector = hikariCPCollector;
this.connectionTimeoutCounterChild = CONNECTION_TIMEOUT_COUNTER.labels(poolName);
this.elapsedAcquiredSummaryChild = ELAPSED_ACQUIRED_SUMMARY.labels(poolName);
this.elapsedUsageSummaryChild = ELAPSED_USAGE_SUMMARY.labels(poolName);
this.elapsedCreationSummaryChild = ELAPSED_CREATION_SUMMARY.labels(poolName);
this.connectionTimeoutCounterChild = CONNECTION_TIMEOUT_COUNTER.labelValues(poolName);
this.elapsedAcquiredSummaryChild = ELAPSED_ACQUIRED_SUMMARY.labelValues(poolName);
this.elapsedUsageSummaryChild = ELAPSED_USAGE_SUMMARY.labelValues(poolName);
this.elapsedCreationSummaryChild = ELAPSED_CREATION_SUMMARY.labelValues(poolName);
}

private void registerMetrics(CollectorRegistry collectorRegistry)
private void registerMetrics(PrometheusRegistry collectorRegistry)
{
if (registrationStatuses.putIfAbsent(collectorRegistry, REGISTERED) == null) {
CONNECTION_TIMEOUT_COUNTER.register(collectorRegistry);
ELAPSED_ACQUIRED_SUMMARY.register(collectorRegistry);
ELAPSED_USAGE_SUMMARY.register(collectorRegistry);
ELAPSED_CREATION_SUMMARY.register(collectorRegistry);
collectorRegistry.register(CONNECTION_TIMEOUT_COUNTER);
collectorRegistry.register(ELAPSED_ACQUIRED_SUMMARY);
collectorRegistry.register(ELAPSED_USAGE_SUMMARY);
collectorRegistry.register(ELAPSED_CREATION_SUMMARY);
}
}

Expand Down Expand Up @@ -103,16 +105,16 @@ public void recordConnectionTimeout()

private static Summary createSummary(String name, String help)
{
return Summary.build()
return Summary.builder()
.name(name)
.labelNames("pool")
.help(help)
.quantile(0.5, 0.05)
.quantile(0.95, 0.01)
.quantile(0.99, 0.001)
.maxAgeSeconds(TimeUnit.MINUTES.toSeconds(5))
.ageBuckets(5)
.create();
.numberOfAgeBuckets(5)
.register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
import com.zaxxer.hikari.metrics.PoolStats;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -43,11 +44,11 @@
public class PrometheusMetricsTrackerFactory implements MetricsTrackerFactory
{

private final static Map<CollectorRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();
private final static Map<PrometheusRegistry, RegistrationStatus> registrationStatuses = new ConcurrentHashMap<>();

private final HikariCPCollector collector = new HikariCPCollector();

private final CollectorRegistry collectorRegistry;
private final PrometheusRegistry collectorRegistry;

enum RegistrationStatus
{
Expand All @@ -60,14 +61,14 @@ enum RegistrationStatus
*/
public PrometheusMetricsTrackerFactory()
{
this(CollectorRegistry.defaultRegistry);
this(PrometheusRegistry.defaultRegistry);
}

/**
* Constructor that allows to pass in a {@link CollectorRegistry} to which the
* Constructor that allows to pass in a {@link PrometheusRegistry} to which the
* Hikari metrics are registered.
*/
public PrometheusMetricsTrackerFactory(CollectorRegistry collectorRegistry)
public PrometheusMetricsTrackerFactory(PrometheusRegistry collectorRegistry)
{
this.collectorRegistry = collectorRegistry;
}
Expand All @@ -80,10 +81,10 @@ public IMetricsTracker create(String poolName, PoolStats poolStats)
return new PrometheusMetricsTracker(poolName, this.collectorRegistry, this.collector);
}

private void registerCollector(Collector collector, CollectorRegistry collectorRegistry)
private void registerCollector(MultiCollector collector, PrometheusRegistry collectorRegistry)
{
if (registrationStatuses.putIfAbsent(collectorRegistry, REGISTERED) == null) {
collector.register(collectorRegistry);
collectorRegistry.register(collector);
}
}
}
Loading