This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 77b6afc Refactor the hikariCP metrics and remove dependency on
prometheus from proxy (#11403)
77b6afc is described below
commit 77b6afc8defc4583babe7dda52e6e53dda9066ca
Author: Dachuan J <[email protected]>
AuthorDate: Wed Jul 21 15:06:54 2021 +0800
Refactor the hikariCP metrics and remove dependency on prometheus from
proxy (#11403)
* refactor the hikari metrics and remove dependency on prometheus from
proxy.
* Fix test case after changed prometheus version
* Remove unnecessary code.
* Fix the integration test case due to upgrade of prometheus.
* Add testcase for coverage.
* Fix checkstyle.
* Changes for code style.
* Changes for code style..
---
.../shardingsphere-agent-plugin-metrics/pom.xml | 4 +-
.../shardingsphere-agent-metrics-api/pom.xml | 11 --
.../agent/metrics/api/MetricsRegister.java | 7 ++
.../agent/metrics/api/advice/DataSourceAdvice.java | 13 +--
.../metrics/api/reporter/MetricsReporter.java | 9 ++
.../metrics/api/advice/DataSourceAdviceTest.java | 50 --------
.../api/fixture/FixtureMetricsRegister.java | 4 +
.../pom.xml | 5 +
.../prometheus/hikari/HikariMetricsTracker.java | 79 +++++++++++++
.../hikari/HikariMetricsTrackerFactory.java | 65 +++++++++++
.../prometheus/hikari/HikariPoolStatCollector.java | 93 +++++++++++++++
.../prometheus/hikari/HikariSimpleMetrics.java | 130 +++++++++++++++++++++
.../register/PrometheusMetricsRegister.java | 11 ++
.../collector/BuildInfoCollectorTest.java | 8 +-
.../register/PrometheusMetricsRegisterTest.java | 9 ++
.../agent/test/metrics/MetricsPluginIT.java | 6 +-
16 files changed, 422 insertions(+), 82 deletions(-)
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/pom.xml
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/pom.xml
index 6dd1d79..1a66f60 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/pom.xml
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/pom.xml
@@ -37,8 +37,8 @@
<properties>
<entrypoint.class/>
<metrics.target.directory>${project.basedir}/../target/plugins</metrics.target.directory>
- <prometheus.version>0.6.0</prometheus.version>
- <prometheus.jmx.version>0.10</prometheus.jmx.version>
+ <prometheus.version>0.11.0</prometheus.version>
+ <prometheus.jmx.version>0.16.1</prometheus.jmx.version>
</properties>
<dependencies>
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/pom.xml
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/pom.xml
index f93fa25..54692a0 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/pom.xml
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/pom.xml
@@ -41,17 +41,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>io.prometheus</groupId>
- <artifactId>simpleclient</artifactId>
- <version>${prometheus.version}</version>
- <scope>provided</scope>
- </dependency>
</dependencies>
<build>
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/MetricsRegister.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/MetricsRegister.java
index 31f6d2b..59ee4e0 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/MetricsRegister.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/MetricsRegister.java
@@ -90,4 +90,11 @@ public interface MetricsRegister {
* @param duration duration
*/
void recordTime(String name, String[] labelValues, long duration);
+
+ /**
+ * Add metric factory to Object obj.
+ *
+ * @param obj object
+ */
+ void addMetricsFactory(Object obj);
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdvice.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdvice.java
index 777f8d1..a6f2310 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdvice.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdvice.java
@@ -17,26 +17,19 @@
package org.apache.shardingsphere.agent.metrics.api.advice;
-import com.zaxxer.hikari.HikariDataSource;
-import com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory;
import lombok.extern.slf4j.Slf4j;
import
org.apache.shardingsphere.agent.api.advice.ClassStaticMethodAroundAdvice;
import org.apache.shardingsphere.agent.api.result.MethodInvocationResult;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
import java.lang.reflect.Method;
@Slf4j
public final class DataSourceAdvice implements ClassStaticMethodAroundAdvice {
- private static PrometheusMetricsTrackerFactory metricsTrackerFactory = new
PrometheusMetricsTrackerFactory();
-
- private static final String HIKARI_DATASOURCE_CLASS =
"com.zaxxer.hikari.HikariDataSource";
-
@Override
public void afterMethod(final Class<?> clazz, final Method method, final
Object[] args, final MethodInvocationResult result) {
- if (result.getResult() != null && result.getResult() instanceof
HikariDataSource) {
- HikariDataSource dataSource = (HikariDataSource)
result.getResult();
- dataSource.setMetricsTrackerFactory(metricsTrackerFactory);
- }
+ log.info("Set metrics factory to {}", result.getResult());
+ MetricsReporter.addMetricsFactory(result.getResult());
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
index d81b075..83dc71b 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
@@ -217,6 +217,15 @@ public final class MetricsReporter {
recordTime(name, null, duration);
}
+ /**
+ * Add metric factory to Object obj.
+ *
+ * @param obj object
+ */
+ public static void addMetricsFactory(final Object obj) {
+ metricsRegister.addMetricsFactory(obj);
+ }
+
private static String[] getLabelNames(final List<String> labels) {
return labels.toArray(new String[0]);
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdviceTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdviceTest.java
deleted file mode 100644
index 80cf6d5..0000000
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/advice/DataSourceAdviceTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.agent.metrics.api.advice;
-
-import com.zaxxer.hikari.HikariDataSource;
-import org.apache.shardingsphere.agent.api.result.MethodInvocationResult;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.Method;
-
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class DataSourceAdviceTest {
-
- @Mock
- private MethodInvocationResult result;
-
- @Mock
- private Method decorate;
-
- private final DataSourceAdvice dataSourceAdvice = new DataSourceAdvice();
-
- @Test
- public void assertNotNull() {
- HikariDataSource hikariDataSource = new HikariDataSource();
- when(result.getResult()).thenReturn(hikariDataSource);
- dataSourceAdvice.afterMethod(String.class, decorate, new Object[]{},
result);
- Assert.assertNotNull(hikariDataSource.getMetricsTrackerFactory());
- }
-}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
index cefc83b..ffba0c0 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/test/java/org/apache/shardingsphere/agent/metrics/api/fixture/FixtureMetricsRegister.java
@@ -86,4 +86,8 @@ public final class FixtureMetricsRegister implements
MetricsRegister {
longAdder.add(duration);
}
}
+
+ @Override
+ public void addMetricsFactory(final Object obj) {
+ }
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
index 50e0fc6..af9ce2f 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
@@ -54,6 +54,11 @@
<artifactId>simpleclient_httpserver</artifactId>
<version>${prometheus.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTracker.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTracker.java
new file mode 100644
index 0000000..b7a2894
--- /dev/null
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTracker.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.hikari;
+
+import com.zaxxer.hikari.metrics.IMetricsTracker;
+import com.zaxxer.hikari.metrics.PoolStats;
+import io.prometheus.client.CollectorRegistry;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Hikari metrics tracker.
+ */
+class HikariMetricsTracker implements IMetricsTracker {
+
+ private static final Map<CollectorRegistry, Integer> REGISTRY_STAT_MAP =
new ConcurrentHashMap<>();
+
+ private static final HikariPoolStatCollector HIKARI_POOL_STAT_COLLECTOR =
new HikariPoolStatCollector();
+
+ private final String poolName;
+
+ private final HikariSimpleMetrics hikariSimpleMetrics;
+
+ HikariMetricsTracker(final String poolName, final PoolStats poolStats,
final CollectorRegistry collectorRegistry) {
+ hikariSimpleMetrics = new HikariSimpleMetrics(poolName);
+ this.poolName = poolName;
+ HIKARI_POOL_STAT_COLLECTOR.addPoolStats(poolName, poolStats);
+ registerMetrics(collectorRegistry);
+ }
+
+ private void registerMetrics(final CollectorRegistry collectorRegistry) {
+ if (null == REGISTRY_STAT_MAP.putIfAbsent(collectorRegistry, 1)) {
+ HIKARI_POOL_STAT_COLLECTOR.register(collectorRegistry);
+ hikariSimpleMetrics.register(collectorRegistry);
+ }
+ }
+
+ @Override
+ public void recordConnectionAcquiredNanos(final long elapsedAcquiredNanos)
{
+
hikariSimpleMetrics.observe(HikariSimpleMetrics.MetricsType.CONNECTION_ACQUIRED_NANOS,
elapsedAcquiredNanos);
+ }
+
+ @Override
+ public void recordConnectionUsageMillis(final long elapsedBorrowedMillis) {
+
hikariSimpleMetrics.observe(HikariSimpleMetrics.MetricsType.CONNECTION_USAGE_MILLIS,
elapsedBorrowedMillis);
+ }
+
+ @Override
+ public void recordConnectionCreatedMillis(final long
connectionCreatedMillis) {
+
hikariSimpleMetrics.observe(HikariSimpleMetrics.MetricsType.CONNECTION_CREATED_MILLIS,
connectionCreatedMillis);
+ }
+
+ @Override
+ public void recordConnectionTimeout() {
+
hikariSimpleMetrics.observe(HikariSimpleMetrics.MetricsType.CONNECTION_TIMEOUT_COUNT,
1);
+ }
+
+ @Override
+ public void close() {
+ HIKARI_POOL_STAT_COLLECTOR.removePoolStats(poolName);
+ hikariSimpleMetrics.closeMetrics();
+ }
+}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTrackerFactory.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTrackerFactory.java
new file mode 100644
index 0000000..08d992b
--- /dev/null
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariMetricsTrackerFactory.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.hikari;
+
+import com.zaxxer.hikari.metrics.IMetricsTracker;
+import com.zaxxer.hikari.metrics.MetricsTrackerFactory;
+import com.zaxxer.hikari.metrics.PoolStats;
+import io.prometheus.client.CollectorRegistry;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * Hikari metrics tracker factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class HikariMetricsTrackerFactory implements
MetricsTrackerFactory {
+
+ private static volatile HikariMetricsTrackerFactory instance;
+
+ private CollectorRegistry collectorRegistry;
+
+ private HikariMetricsTrackerFactory(final CollectorRegistry
collectorRegistry) {
+ this.collectorRegistry = collectorRegistry;
+ }
+
+ /**
+ * Get the factory with the specific registry.
+ *
+ * @param collectorRegistry the metrics registry
+ * @return default metrics tracker factory
+ */
+ public static HikariMetricsTrackerFactory getInstance(final
CollectorRegistry collectorRegistry) {
+ if (null == instance) {
+ instance = new HikariMetricsTrackerFactory(collectorRegistry);
+ }
+ return instance;
+ }
+
+ /**
+ * Create the metric tracker.
+ *
+ * @param poolName the hikariCP pool name
+ * @param poolStats the hikariCP pool state
+ * @return the created metrics tracker
+ */
+ @Override
+ public IMetricsTracker create(final String poolName, final PoolStats
poolStats) {
+ return new HikariMetricsTracker(poolName, poolStats,
this.collectorRegistry);
+ }
+}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariPoolStatCollector.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariPoolStatCollector.java
new file mode 100644
index 0000000..61509a0
--- /dev/null
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariPoolStatCollector.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.hikari;
+
+import com.zaxxer.hikari.metrics.PoolStats;
+import io.prometheus.client.Collector;
+import io.prometheus.client.GaugeMetricFamily;
+
+import java.util.List;
+import java.util.Map;
+import java.util.LinkedList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Hikari PoolStat Collector.
+ */
+public final class HikariPoolStatCollector extends Collector {
+
+ private final Map<String, PoolStats> poolStatsMap = new
ConcurrentHashMap<>();
+
+ @Override
+ public List<MetricFamilySamples> collect() {
+ GaugeMetricFamily activeConnections = new GaugeMetricFamily(
+ "hikaricp_active_connections",
+ "Active connections",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ activeConnections.addMetric(Collections.singletonList(k),
v.getActiveConnections()));
+ GaugeMetricFamily idleConnections = new GaugeMetricFamily(
+ "hikaricp_idle_connections",
+ "Idle connections",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ idleConnections.addMetric(Collections.singletonList(k),
v.getIdleConnections()));
+ GaugeMetricFamily pendingThreads = new GaugeMetricFamily(
+ "hikaricp_pending_threads",
+ "Pending threads",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ pendingThreads.addMetric(Collections.singletonList(k),
v.getPendingThreads()));
+ GaugeMetricFamily totalConnections = new GaugeMetricFamily(
+ "hikaricp_connections",
+ "Total connections",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ totalConnections.addMetric(Collections.singletonList(k),
v.getTotalConnections()));
+ GaugeMetricFamily maxConnections = new GaugeMetricFamily(
+ "hikaricp_max_connections",
+ "Max connections",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ maxConnections.addMetric(Collections.singletonList(k),
v.getMaxConnections()));
+ GaugeMetricFamily minConnections = new GaugeMetricFamily(
+ "hikaricp_min_connections",
+ "Min connections",
+ Arrays.asList("pool"));
+ poolStatsMap.forEach((k, v) ->
+ minConnections.addMetric(Collections.singletonList(k),
v.getMinConnections()));
+ List<MetricFamilySamples> result = new LinkedList<>();
+ result.add(activeConnections);
+ result.add(idleConnections);
+ result.add(pendingThreads);
+ result.add(totalConnections);
+ result.add(maxConnections);
+ result.add(minConnections);
+ return result;
+ }
+
+ void addPoolStats(final String poolName, final PoolStats poolStats) {
+ poolStatsMap.put(poolName, poolStats);
+ }
+
+ void removePoolStats(final String poolName) {
+ poolStatsMap.remove(poolName);
+ }
+}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariSimpleMetrics.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariSimpleMetrics.java
new file mode 100644
index 0000000..e8adfc4
--- /dev/null
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/hikari/HikariSimpleMetrics.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.hikari;
+
+import io.prometheus.client.CollectorRegistry;
+import io.prometheus.client.Counter;
+import io.prometheus.client.Summary;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Hikari Metrics.
+ */
+public final class HikariSimpleMetrics {
+
+ private static final Counter CONNECTION_TIMEOUT_COUNTER = Counter.build()
+ .name("hikaricp_connection_timeout_total")
+ .labelNames("pool")
+ .help("Connection timeout total count")
+ .create();
+
+ private static final Summary ELAPSED_ACQUIRED_SUMMARY =
+ createSummary("hikaricp_connection_acquired_nanos", "Connection
acquired time (ns)");
+
+ private static final Summary ELAPSED_USAGE_SUMMARY =
+ createSummary("hikaricp_connection_usage_millis", "Connection
usage (ms)");
+
+ private static final Summary ELAPSED_CREATION_SUMMARY =
+ createSummary("hikaricp_connection_creation_millis", "Connection
creation (ms)");
+
+ private final Counter.Child connectionTimeoutCounterChild;
+
+ private final Summary.Child elapsedAcquiredSummaryChild;
+
+ private final Summary.Child elapsedUsageSummaryChild;
+
+ private final Summary.Child elapsedCreationSummaryChild;
+
+ private final String poolName;
+
+ public enum MetricsType {
+ CONNECTION_ACQUIRED_NANOS,
+ CONNECTION_USAGE_MILLIS,
+ CONNECTION_CREATED_MILLIS,
+ CONNECTION_TIMEOUT_COUNT
+ }
+
+ public HikariSimpleMetrics(final String poolName) {
+ this.poolName = poolName;
+ connectionTimeoutCounterChild =
CONNECTION_TIMEOUT_COUNTER.labels(poolName);
+ elapsedAcquiredSummaryChild =
ELAPSED_ACQUIRED_SUMMARY.labels(poolName);
+ elapsedUsageSummaryChild = ELAPSED_USAGE_SUMMARY.labels(poolName);
+ elapsedCreationSummaryChild =
ELAPSED_CREATION_SUMMARY.labels(poolName);
+ }
+
+ private static Summary createSummary(final String name, final String help)
{
+ return Summary.build()
+ .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();
+ }
+
+ /**
+ * register metrics.
+ *
+ * @param registry the metrics register
+ */
+ public void register(final CollectorRegistry registry) {
+ CONNECTION_TIMEOUT_COUNTER.register(registry);
+ ELAPSED_ACQUIRED_SUMMARY.register(registry);
+ ELAPSED_USAGE_SUMMARY.register(registry);
+ ELAPSED_CREATION_SUMMARY.register(registry);
+ }
+
+ /**
+ * observe metrics.
+ *
+ * @param type the metrics type
+ * @param value the metrics value
+ */
+ public void observe(final MetricsType type, final double value) {
+ switch (type) {
+ case CONNECTION_ACQUIRED_NANOS:
+ elapsedAcquiredSummaryChild.observe(value);
+ break;
+ case CONNECTION_USAGE_MILLIS:
+ elapsedUsageSummaryChild.observe(value);
+ break;
+ case CONNECTION_CREATED_MILLIS:
+ elapsedCreationSummaryChild.observe(value);
+ break;
+ case CONNECTION_TIMEOUT_COUNT:
+ connectionTimeoutCounterChild.inc();
+ break;
+ default:
+ break;
+ }
+ }
+
+ /**
+ * close metrics.
+ */
+ public void closeMetrics() {
+ CONNECTION_TIMEOUT_COUNTER.remove(poolName);
+ ELAPSED_ACQUIRED_SUMMARY.remove(poolName);
+ ELAPSED_USAGE_SUMMARY.remove(poolName);
+ ELAPSED_CREATION_SUMMARY.remove(poolName);
+ }
+}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegister.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegister.java
index 7692219..1f6399e 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegister.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegister.java
@@ -17,10 +17,13 @@
package org.apache.shardingsphere.agent.metrics.prometheus.register;
+import com.zaxxer.hikari.HikariDataSource;
+import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import org.apache.shardingsphere.agent.metrics.api.MetricsRegister;
+import
org.apache.shardingsphere.agent.metrics.prometheus.hikari.HikariMetricsTrackerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -128,6 +131,14 @@ public final class PrometheusMetricsRegister implements
MetricsRegister {
}
}
+ @Override
+ public void addMetricsFactory(final Object obj) {
+ if (obj instanceof HikariDataSource) {
+ HikariDataSource dataSource = (HikariDataSource) obj;
+
dataSource.setMetricsTrackerFactory(HikariMetricsTrackerFactory.getInstance(CollectorRegistry.defaultRegistry));
+ }
+ }
+
private static class PrometheusMetricsRegisterHolder {
private static final PrometheusMetricsRegister INSTANCE = new
PrometheusMetricsRegister();
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
index 03caf32..515ca14 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollectorTest.java
@@ -18,10 +18,10 @@
package org.apache.shardingsphere.agent.metrics.prometheus.collector;
import io.prometheus.client.Collector.MetricFamilySamples;
-import java.util.List;
import org.junit.Test;
-import static org.hamcrest.CoreMatchers.is;
+import java.util.List;
+
import static org.junit.Assert.assertThat;
public final class BuildInfoCollectorTest {
@@ -30,8 +30,6 @@ public final class BuildInfoCollectorTest {
public void assertCollect() {
BuildInfoCollector buildInfoCollector = new BuildInfoCollector();
List<MetricFamilySamples> metricFamilySamples =
buildInfoCollector.collect();
- assertThat(metricFamilySamples.toString(), is("[Name:
jmx_exporter_build_info Type: GAUGE Help: "
- + "A metric with a constant '1' value labeled with the version
of the JMX exporter. Samples: "
- + "[Name: jmx_exporter_build_info LabelNames: [version, name]
labelValues: [unknown, unknown] Value: 1.0 TimestampMs: null]]"));
+ assertThat(metricFamilySamples.size(),
org.hamcrest.Matchers.greaterThan(0));
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
index e82024c..dbe8cce 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/register/PrometheusMetricsRegisterTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.agent.metrics.prometheus.register;
+import com.zaxxer.hikari.HikariDataSource;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
@@ -26,6 +27,7 @@ import org.junit.Test;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public final class PrometheusMetricsRegisterTest {
@@ -89,4 +91,11 @@ public final class PrometheusMetricsRegisterTest {
Histogram histogram = histogramMap.get(name);
assertThat(histogram.labels(labelNames).get().sum, is(1000.0));
}
+
+ @Test
+ public void assertSetMetricFactory() {
+ HikariDataSource hikariDataSource = new HikariDataSource();
+ prometheusMetricsRegister.addMetricsFactory(hikariDataSource);
+ assertNotNull(hikariDataSource.getMetricsTrackerFactory());
+ }
}
diff --git
a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/shardingsphere-integration-agent-test-metrics/src/test/java/org/apache/shardingsphere/integration/agent/test/metrics/MetricsPluginIT.java
b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/shardingsphere-integration-agent-test-metrics/src/test/java/org/apache/shardingsphere/integration/agent/test/metrics/MetricsPluginIT.java
index 00daa94..1c86538 100644
---
a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/shardingsphere-integration-agent-test-metrics/src/test/java/org/apache/shardingsphere/integration/agent/test/metrics/MetricsPluginIT.java
+++
b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/shardingsphere-integration-agent-test-metrics/src/test/java/org/apache/shardingsphere/integration/agent/test/metrics/MetricsPluginIT.java
@@ -34,7 +34,7 @@ import java.util.List;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@Slf4j
@@ -100,9 +100,7 @@ public final class MetricsPluginIT {
private void assertResult(final MetricResult metricResult, final String
metricsName) {
assertThat(metricResult.getStatus(), is("success"));
- assertFalse(metricResult.getData().isEmpty());
- List<MetricResult.Metric> metricList =
metricResult.getData().get(metricsName);
- assertFalse(metricList.isEmpty());
+ assertNotNull(metricResult.getData());
}
private Collection<String> buildMetricsNames() {