This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 573755f Decouple shardingsphere-agent-metrics-prometheus and HikariCP
(#16214)
573755f is described below
commit 573755ffb37422d48f17d7981452c5bebd58624f
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Mar 20 06:55:45 2022 +0800
Decouple shardingsphere-agent-metrics-prometheus and HikariCP (#16214)
* Revise scope of HikariCP's dependency
* Revise scope of HikariCP's dependency
* Decouple shardingsphere-agent-metrics-prometheus and HikariCP
* Decouple shardingsphere-agent-metrics-prometheus and HikariCP
* Refactor BuildInfoCollector
* Refactor MetaDataInfoCollector
* For java doc
* Refactor PrometheusPluginDefinitionService
* Refactor PrometheusPluginBootService
* Refactor PrometheusPluginBootServiceTest
* Refactor PrometheusWrapperFactoryTest
* Refactor PrometheusWrapperFactory
---
examples/pom.xml | 12 +-
.../agent/metrics/api/util/MetricsUtil.java | 18 ++-
.../pom.xml | 51 +++---
.../prometheus/collector/BuildInfoCollector.java | 33 ++--
.../collector/MetaDataInfoCollector.java | 60 ++++---
.../prometheus/collector/ProxyInfoCollector.java | 22 +--
.../PrometheusPluginDefinitionService.java | 31 +---
.../service/PrometheusPluginBootService.java | 66 +++-----
.../wrapper/PrometheusWrapperFactory.java | 176 +++++++--------------
.../collector/BuildInfoCollectorTest.java | 9 +-
.../collector/MetaDataInfoCollectorTest.java | 7 +-
.../collector/ProxyInfoCollectorTest.java | 7 +-
.../PrometheusPluginDefinitionServiceTest.java | 13 +-
.../service/PrometheusPluginBootServiceTest.java | 38 ++---
.../prometheus/wrapper/CounterWrapperTest.java | 6 +-
.../wrapper/PrometheusWrapperFactoryTest.java | 20 +--
.../prometheus/wrapper/SummaryWrapperTest.java | 5 +-
.../src/test/resources/prometheus/metrics.yaml | 2 +-
.../shardingsphere-database-datetime/pom.xml | 10 +-
.../shardingsphere-jdbc-core/pom.xml | 11 +-
.../jdbc/core/connection/ConnectionManager.java | 3 +-
.../shardingsphere-transaction-xa-core/pom.xml | 9 +-
22 files changed, 245 insertions(+), 364 deletions(-)
diff --git a/examples/pom.xml b/examples/pom.xml
index 3b62d08..6c9b102 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -330,11 +330,7 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- <version>${hikari-cp.version}</version>
- </dependency>
+
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
@@ -353,6 +349,12 @@
</dependency>
<dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ <version>${hikari-cp.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
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/util/MetricsUtil.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/util/MetricsUtil.java
index 2faf91e..981e980 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/util/MetricsUtil.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/util/MetricsUtil.java
@@ -17,23 +17,27 @@
package org.apache.shardingsphere.agent.metrics.api.util;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
/**
- * Metrics Utils.
+ * Metrics Util.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MetricsUtil {
/**
- * check if it has proxy class.
+ * Check whether class existed.
*
- * @param className the class name
- * @return true if it has proxy class by the class name, otherwise false
+ * @param className class name
+ * @return class existed or not
*/
- public static boolean classNotExist(final String className) {
+ public static boolean isClassExisted(final String className) {
try {
Class.forName(className);
} catch (ClassNotFoundException ex) {
- return true;
+ return false;
}
- return false;
+ return true;
}
}
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 39cfbe4..e2cc913 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
@@ -31,38 +31,14 @@
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-agent-core</artifactId>
+ <artifactId>shardingsphere-agent-metrics-api</artifactId>
<version>${project.version}</version>
- <scope>provided</scope>
</dependency>
+
<dependency>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-agent-metrics-api</artifactId>
+ <artifactId>shardingsphere-agent-core</artifactId>
<version>${project.version}</version>
- </dependency>
- <dependency>
- <groupId>io.prometheus</groupId>
- <artifactId>simpleclient</artifactId>
- <version>${prometheus.version}</version>
- </dependency>
- <dependency>
- <groupId>io.prometheus</groupId>
- <artifactId>simpleclient_hotspot</artifactId>
- <version>${prometheus.version}</version>
- </dependency>
- <dependency>
- <groupId>io.prometheus.jmx</groupId>
- <artifactId>collector</artifactId>
- <version>${prometheus.jmx.version}</version>
- </dependency>
- <dependency>
- <groupId>io.prometheus</groupId>
- <artifactId>simpleclient_httpserver</artifactId>
- <version>${prometheus.version}</version>
- </dependency>
- <dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
@@ -83,6 +59,27 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
+
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient</artifactId>
+ <version>${prometheus.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_hotspot</artifactId>
+ <version>${prometheus.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_httpserver</artifactId>
+ <version>${prometheus.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.prometheus.jmx</groupId>
+ <artifactId>collector</artifactId>
+ <version>${prometheus.jmx.version}</version>
+ </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/collector/BuildInfoCollector.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
index e5edea7..1ef9bdc 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
@@ -24,39 +24,38 @@ import
org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
import
org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
import java.util.Arrays;
-import java.util.LinkedList;
+import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
- * Build info collector.
+ * Build information collector.
*/
@Slf4j
public final class BuildInfoCollector extends Collector {
- private static final String PROXY_BOOTSTRAP_CLASS_STR =
"org.apache.shardingsphere.proxy.Bootstrap";
+ private static final String PROXY_BOOTSTRAP_CLASS =
"org.apache.shardingsphere.proxy.Bootstrap";
private static final PrometheusWrapperFactory FACTORY = new
PrometheusWrapperFactory();
@Override
public List<MetricFamilySamples> collect() {
- List<MetricFamilySamples> result = new LinkedList<>();
Optional<GaugeMetricFamily> artifactInfo =
FACTORY.createGaugeMetricFamily(MetricIds.BUILD_INFO);
- Package pluginPkg = getClass().getPackage();
- final String pluginVersion = pluginPkg.getImplementationVersion();
- final String pluginName = pluginPkg.getImplementationTitle();
- artifactInfo.ifPresent(m ->
- m.addMetric(Arrays.asList(null != pluginVersion ?
pluginVersion : "unknown", null != pluginName ? pluginName : "unknown"), 1L));
+ if (!artifactInfo.isPresent()) {
+ return Collections.emptyList();
+ }
+ addMetric(artifactInfo.get(), getClass().getPackage());
try {
- Package proxyPkg =
Class.forName(PROXY_BOOTSTRAP_CLASS_STR).getPackage();
- final String proxyVersion = proxyPkg.getImplementationVersion();
- final String proxyName = proxyPkg.getImplementationTitle();
- artifactInfo.ifPresent(m ->
- m.addMetric(Arrays.asList(null != proxyVersion ?
proxyVersion : "unknown", null != proxyName ? proxyName : "unknown"), 1L));
- } catch (ClassNotFoundException ex) {
+ addMetric(artifactInfo.get(),
Class.forName(PROXY_BOOTSTRAP_CLASS).getPackage());
+ } catch (final ClassNotFoundException ignored) {
log.warn("No proxy class find");
}
- artifactInfo.ifPresent(result::add);
- return result;
+ return Collections.singletonList(artifactInfo.get());
+ }
+
+ private void addMetric(final GaugeMetricFamily artifactInfo, final Package
pkg) {
+ String version = null == pkg.getImplementationVersion() ? "unknown" :
pkg.getImplementationVersion();
+ String name = null == pkg.getImplementationTitle() ? "unknown" :
pkg.getImplementationTitle();
+ artifactInfo.addMetric(Arrays.asList(version, name), 1L);
}
}
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/collector/MetaDataInfoCollector.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/MetaDataInfoCollector.java
index add6b8f..a00f501 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/MetaDataInfoCollector.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/MetaDataInfoCollector.java
@@ -17,25 +17,26 @@
package org.apache.shardingsphere.agent.metrics.prometheus.collector;
-import com.zaxxer.hikari.HikariDataSource;
import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.api.util.MetricsUtil;
import
org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
+import
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import javax.sql.DataSource;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
-import java.util.Set;
/**
* Meta data information collector.
@@ -47,42 +48,57 @@ public final class MetaDataInfoCollector extends Collector {
private static final String ACTUAL_DB_COUNT = "database_count";
- private static final PrometheusWrapperFactory FACTORY = new
PrometheusWrapperFactory();
+ private static final String PROXY_CONTEXT_CLASS =
"org.apache.shardingsphere.proxy.backend.context.ProxyContext";
- private static final String PROXY_CONTEXT_CLASS_STR =
"org.apache.shardingsphere.proxy.backend.context.ProxyContext";
+ private static final PrometheusWrapperFactory FACTORY = new
PrometheusWrapperFactory();
@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> result = new LinkedList<>();
Optional<GaugeMetricFamily> metaDataInfo =
FACTORY.createGaugeMetricFamily(MetricIds.METADATA_INFO);
- if (MetricsUtil.classNotExist(PROXY_CONTEXT_CLASS_STR) ||
!metaDataInfo.isPresent()) {
- return result;
+ if (metaDataInfo.isPresent() &&
MetricsUtil.isClassExisted(PROXY_CONTEXT_CLASS)) {
+ collectProxy(metaDataInfo.get());
+ result.add(metaDataInfo.get());
}
- collectProxy(metaDataInfo.get());
- result.add(metaDataInfo.get());
return result;
}
private void collectProxy(final GaugeMetricFamily metricFamily) {
MetaDataContexts metaDataContexts =
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
metricFamily.addMetric(Collections.singletonList(LOGIC_DB_COUNT),
metaDataContexts.getMetaDataMap().size());
- Set<String> databaseSet = new HashSet<>();
- metaDataContexts.getMetaDataMap().values().forEach(each ->
each.getResource().getDataSources().values()
- .forEach(dataSource ->
MetaDataInfoCollector.this.countDatabase(databaseSet, dataSource)));
- metricFamily.addMetric(Collections.singletonList(ACTUAL_DB_COUNT),
databaseSet.size());
+ metricFamily.addMetric(Collections.singletonList(ACTUAL_DB_COUNT),
getDatabaseNames(metaDataContexts).size());
+ }
+
+ private Collection<String> getDatabaseNames(final MetaDataContexts
metaDataContexts) {
+ Collection<String> result = new HashSet<>();
+ for (ShardingSphereMetaData each :
metaDataContexts.getMetaDataMap().values()) {
+ result.addAll(getDatabaseNames(each));
+ }
+ return result;
}
- private void countDatabase(final Set<String> databaseSet, final DataSource
dataSource) {
- if (dataSource instanceof HikariDataSource) {
- String jdbcUrl = ((HikariDataSource) dataSource).getJdbcUrl();
- try {
- URI uri = new URI(jdbcUrl.substring(5));
- if (null != uri.getPath()) {
- databaseSet.add(uri.getPath());
- }
- } catch (URISyntaxException | NullPointerException e) {
- log.info("Unsupported jdbc url by URI: {}", jdbcUrl);
+ private Collection<String> getDatabaseNames(final ShardingSphereMetaData
metaData) {
+ Collection<String> result = new HashSet<>();
+ for (DataSource each :
metaData.getResource().getDataSources().values()) {
+ getDatabaseName(each).ifPresent(result::add);
+ }
+ return result;
+ }
+
+ private Optional<String> getDatabaseName(final DataSource dataSource) {
+ Object jdbcUrl =
DataSourcePropertiesCreator.create(dataSource).getAllStandardProperties().get("url");
+ if (null == jdbcUrl) {
+ log.info("Can not get JDBC URL");
+ return Optional.empty();
+ }
+ try {
+ URI uri = new URI(jdbcUrl.toString().substring(5));
+ if (null != uri.getPath()) {
+ return Optional.of(uri.getPath());
}
+ } catch (final URISyntaxException | NullPointerException ignored) {
+ log.info("Unsupported JDBC URL by URI: {}", jdbcUrl);
}
+ return Optional.empty();
}
}
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/collector/ProxyInfoCollector.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
index 4181c57..f619a71 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
@@ -22,6 +22,7 @@ import io.prometheus.client.GaugeMetricFamily;
import org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
import org.apache.shardingsphere.agent.metrics.api.util.MetricsUtil;
import
org.apache.shardingsphere.agent.metrics.prometheus.wrapper.PrometheusWrapperFactory;
+import org.apache.shardingsphere.infra.state.StateContext;
import org.apache.shardingsphere.infra.state.StateType;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -32,13 +33,13 @@ import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
/**
- * Proxy info collector.
+ * Proxy information collector.
*/
public final class ProxyInfoCollector extends Collector {
private static final String PROXY_STATE = "state";
- private static final String PROXY_CLASS_STR =
"org.apache.shardingsphere.proxy.backend.context.ProxyContext";
+ private static final String PROXY_CLASS =
"org.apache.shardingsphere.proxy.backend.context.ProxyContext";
private static final PrometheusWrapperFactory FACTORY = new
PrometheusWrapperFactory();
@@ -51,16 +52,17 @@ public final class ProxyInfoCollector extends Collector {
@Override
public List<MetricFamilySamples> collect() {
- List<MetricFamilySamples> result = new LinkedList<>();
- if (MetricsUtil.classNotExist(PROXY_CLASS_STR)) {
- return result;
+ if (!MetricsUtil.isClassExisted(PROXY_CLASS)) {
+ return Collections.emptyList();
}
Optional<GaugeMetricFamily> proxyInfo =
FACTORY.createGaugeMetricFamily(MetricIds.PROXY_INFO);
- ProxyContext.getInstance().getStateContext().ifPresent(optional -> {
- proxyInfo.ifPresent(m ->
- m.addMetric(Collections.singletonList(PROXY_STATE),
PROXY_STATE_MAP.get(optional.getCurrentState())));
- proxyInfo.ifPresent(result::add);
- });
+ Optional<StateContext> stateContext =
ProxyContext.getInstance().getStateContext();
+ if (!proxyInfo.isPresent() || !stateContext.isPresent()) {
+ return Collections.emptyList();
+ }
+ List<MetricFamilySamples> result = new LinkedList<>();
+ proxyInfo.get().addMetric(Collections.singletonList(PROXY_STATE),
PROXY_STATE_MAP.get(stateContext.get().getCurrentState()));
+ result.add(proxyInfo.get());
return result;
}
}
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/definition/PrometheusPluginDefinitionService.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
index 75c8ace..651430d 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionService.java
@@ -19,7 +19,7 @@ package
org.apache.shardingsphere.agent.metrics.prometheus.definition;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.matcher.ElementMatchers;
-import org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint;
+import
org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint.Builder;
import org.apache.shardingsphere.agent.core.entity.Interceptor;
import org.apache.shardingsphere.agent.core.entity.Interceptors;
import org.apache.shardingsphere.agent.core.entity.TargetPoint;
@@ -36,14 +36,13 @@ public final class PrometheusPluginDefinitionService
extends AbstractPluginDefin
@Override
public void defineInterceptors() {
- Yaml yaml = new Yaml();
- InputStream in =
this.getClass().getResourceAsStream("/prometheus/interceptors.yaml");
- Interceptors interceptors = yaml.loadAs(in, Interceptors.class);
+ InputStream inputStream =
getClass().getResourceAsStream("/prometheus/interceptors.yaml");
+ Interceptors interceptors = new Yaml().loadAs(inputStream,
Interceptors.class);
for (Interceptor each : interceptors.getInterceptors()) {
if (null == each.getTarget()) {
continue;
}
- PluginInterceptorPoint.Builder builder =
defineInterceptor(each.getTarget());
+ Builder builder = defineInterceptor(each.getTarget());
if (null != each.getConstructAdvice() &&
!("".equals(each.getConstructAdvice()))) {
builder.onConstructor(ElementMatchers.isConstructor()).implement(each.getConstructAdvice()).build();
log.debug("Init construct: {}", each.getConstructAdvice());
@@ -51,28 +50,14 @@ public final class PrometheusPluginDefinitionService
extends AbstractPluginDefin
if (null == each.getPoints()) {
continue;
}
- String[] instancePoints = each
- .getPoints()
- .stream()
- .filter(i -> "instance".equals(i.getType()))
- .map(TargetPoint::getName)
- .toArray(String[]::new);
- String[] staticPoints = each
- .getPoints()
- .stream()
- .filter(i -> "static".equals(i.getType()))
- .map(TargetPoint::getName)
- .toArray(String[]::new);
+ String[] instancePoints = each.getPoints().stream().filter(i ->
"instance".equals(i.getType())).map(TargetPoint::getName).toArray(String[]::new);
+ String[] staticPoints = each.getPoints().stream().filter(i ->
"static".equals(i.getType())).map(TargetPoint::getName).toArray(String[]::new);
if (instancePoints.length > 0) {
-
builder.aroundInstanceMethod(ElementMatchers.namedOneOf(instancePoints))
- .implement(each.getInstanceAdvice())
- .build();
+
builder.aroundInstanceMethod(ElementMatchers.namedOneOf(instancePoints)).implement(each.getInstanceAdvice()).build();
log.debug("Init instance: {}", each.getInstanceAdvice());
}
if (staticPoints.length > 0) {
-
builder.aroundClassStaticMethod(ElementMatchers.namedOneOf(staticPoints))
- .implement(each.getStaticAdvice())
- .build();
+
builder.aroundClassStaticMethod(ElementMatchers.namedOneOf(staticPoints)).implement(each.getStaticAdvice()).build();
log.debug("Init static: {}", each.getStaticAdvice());
}
}
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/service/PrometheusPluginBootService.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
index b6d5e60..396324b 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
@@ -17,12 +17,12 @@
package org.apache.shardingsphere.agent.metrics.prometheus.service;
+import com.google.common.base.Preconditions;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.agent.config.PluginConfiguration;
-import org.apache.shardingsphere.agent.exception.PluginConfigurationException;
import org.apache.shardingsphere.agent.metrics.api.MetricsPool;
import
org.apache.shardingsphere.agent.metrics.prometheus.collector.BuildInfoCollector;
import
org.apache.shardingsphere.agent.metrics.prometheus.collector.MetaDataInfoCollector;
@@ -42,59 +42,45 @@ public final class PrometheusPluginBootService implements
PluginBootService {
private HTTPServer httpServer;
@Override
- public void start(final PluginConfiguration pluginConfiguration) {
- if (!checkConfig(pluginConfiguration)) {
- throw new PluginConfigurationException("prometheus config error,
host is null or port is %s", pluginConfiguration.getPort());
- }
- startServer(pluginConfiguration);
+ public void start(final PluginConfiguration pluginConfig) {
+ Preconditions.checkState(pluginConfig.getPort() > 0, "Prometheus
config error, host is null or port is `%s`", pluginConfig.getPort());
+ startServer(pluginConfig);
MetricsPool.setMetricsFactory(new PrometheusWrapperFactory());
}
- @Override
- public void close() {
- if (null != httpServer) {
- httpServer.stop();
- }
- }
-
- @Override
- public String getType() {
- return "Prometheus";
- }
-
- private boolean checkConfig(final PluginConfiguration pluginConfiguration)
{
- return pluginConfiguration.getPort() > 0;
- }
-
- private void startServer(final PluginConfiguration configuration) {
- boolean enabled =
Boolean.parseBoolean(configuration.getProps().getProperty("JVM_INFORMATION_COLLECTOR_ENABLED"));
- registerDefault();
- registerJvm(enabled);
- int port = configuration.getPort();
- String host = configuration.getHost();
- InetSocketAddress inetSocketAddress;
- if (null == host || "".equalsIgnoreCase(host)) {
- inetSocketAddress = new InetSocketAddress(port);
- } else {
- inetSocketAddress = new InetSocketAddress(host, port);
- }
+ private void startServer(final PluginConfiguration pluginConfig) {
+
registerCollector(Boolean.parseBoolean(pluginConfig.getProps().getProperty("JVM_INFORMATION_COLLECTOR_ENABLED")));
+ InetSocketAddress socketAddress =
getSocketAddress(pluginConfig.getHost(), pluginConfig.getPort());
try {
- httpServer = new HTTPServer(inetSocketAddress,
CollectorRegistry.defaultRegistry, true);
- log.info("Prometheus metrics HTTP server `{}:{}` start success",
inetSocketAddress.getHostString(), inetSocketAddress.getPort());
+ httpServer = new HTTPServer(socketAddress,
CollectorRegistry.defaultRegistry, true);
+ log.info("Prometheus metrics HTTP server `{}:{}` start success",
socketAddress.getHostString(), socketAddress.getPort());
} catch (final IOException ex) {
log.error("Prometheus metrics HTTP server start fail", ex);
}
}
- private void registerDefault() {
+ private void registerCollector(final boolean enabled) {
new ProxyInfoCollector().register();
new BuildInfoCollector().register();
new MetaDataInfoCollector().register();
- }
-
- private void registerJvm(final boolean enabled) {
if (enabled) {
DefaultExports.initialize();
}
}
+
+ private InetSocketAddress getSocketAddress(final String host, final int
port) {
+ return null == host || "".equals(host) ? new InetSocketAddress(port) :
new InetSocketAddress(host, port);
+ }
+
+ @Override
+ public void close() {
+ if (null != httpServer) {
+ httpServer.stop();
+ }
+ }
+
+ @Override
+ public String getType() {
+ return "Prometheus";
+ }
}
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/wrapper/PrometheusWrapperFactory.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
index efcb625..e43a7f0 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactory.java
@@ -47,17 +47,16 @@ public class PrometheusWrapperFactory implements
MetricsWrapperFactory {
@SuppressWarnings("unchecked")
private static void parseMetricsYaml() {
- Yaml yaml = new Yaml();
- InputStream in =
PrometheusWrapperFactory.class.getResourceAsStream("/prometheus/metrics.yaml");
- Map<String, List<Map<String, Object>>> metricsMap = yaml.loadAs(in,
LinkedHashMap.class);
+ InputStream inputStream =
PrometheusWrapperFactory.class.getResourceAsStream("/prometheus/metrics.yaml");
+ Map<String, List<Map<String, Object>>> metricsMap = new
Yaml().loadAs(inputStream, LinkedHashMap.class);
metrics = metricsMap.get("metrics");
}
/**
- * Create the metrics wrapper.
+ * Create metrics wrapper.
*
* @param id id
- * @return optional of metrics wrapper
+ * @return metrics wrapper
*/
@Override
public Optional<MetricsWrapper> create(final String id) {
@@ -65,45 +64,42 @@ public class PrometheusWrapperFactory implements
MetricsWrapperFactory {
}
/**
- * Create the gauge metric family.
+ * Create gauge metric family.
*
* @param id string
- * @return the optional of gauge metric family
+ * @return gauge metric family
*/
public Optional<GaugeMetricFamily> createGaugeMetricFamily(final String
id) {
- Optional<Map<String, Object>> metricMap = findById(id);
+ Optional<Map<String, Object>> metricMap = findMetric(id);
if (!metricMap.isPresent()) {
return Optional.empty();
}
Map<String, Object> metric = metricMap.get();
- if (null == getType(metric)) {
+ if (null == getMetricType(metric)) {
return Optional.empty();
}
- if ("GAUGEMETRICFAMILY".equalsIgnoreCase(getType(metric))) {
+ if ("GAUGEMETRICFAMILY".equalsIgnoreCase(getMetricType(metric))) {
return createGaugeMetricFamily(metric);
- } else {
- return Optional.empty();
}
+ return Optional.empty();
}
private Optional<GaugeMetricFamily> createGaugeMetricFamily(final
Map<String, Object> metric) {
- if (null != getLabels(metric)) {
- return Optional.of(new GaugeMetricFamily(getName(metric),
getHelp(metric), getLabels(metric)));
- } else {
- return Optional.of(new GaugeMetricFamily(getName(metric),
getHelp(metric), 1));
- }
+ return Optional.of(null == getMetricLabels(metric)
+ ? new GaugeMetricFamily(getMetricName(metric),
getMetricHelpMessage(metric), 1)
+ : new GaugeMetricFamily(getMetricName(metric),
getMetricHelpMessage(metric), getMetricLabels(metric)));
}
private Optional<MetricsWrapper> createById(final String id) {
- Optional<Map<String, Object>> metricMap = findById(id);
+ Optional<Map<String, Object>> metricMap = findMetric(id);
if (!metricMap.isPresent()) {
return Optional.empty();
}
Map<String, Object> metric = metricMap.get();
- if (null == getType(metric)) {
+ if (null == getMetricType(metric)) {
return Optional.empty();
}
- switch (getType(metric).toUpperCase()) {
+ switch (getMetricType(metric).toUpperCase()) {
case "COUNTER":
return createCounter(metric);
case "GAUGE":
@@ -117,145 +113,87 @@ public class PrometheusWrapperFactory implements
MetricsWrapperFactory {
}
}
+ private Optional<Map<String, Object>> findMetric(final String id) {
+ return metrics.stream().filter(optional ->
id.equals(getMetricId(optional))).findFirst();
+ }
+
private Optional<MetricsWrapper> createCounter(final Map<String, Object>
metric) {
- Counter.Builder builder = Counter.build()
- .name(getName(metric))
- .help(getHelp(metric));
- if (null != getLabels(metric)) {
- builder.labelNames(getLabels(metric).toArray(new String[0]));
+ Counter.Builder builder =
Counter.build().name(getMetricName(metric)).help(getMetricHelpMessage(metric));
+ if (null != getMetricLabels(metric)) {
+ builder.labelNames(getMetricLabels(metric).toArray(new String[0]));
}
- Counter counter = builder.register();
- CounterWrapper wrapper = new CounterWrapper(counter);
- return Optional.of(wrapper);
+ return Optional.of(new CounterWrapper(builder.register()));
}
private Optional<MetricsWrapper> createGauge(final Map<String, Object>
metric) {
- Gauge.Builder builder = Gauge.build()
- .name(getName(metric))
- .help(getHelp(metric));
- if (null != getLabels(metric)) {
- builder.labelNames(getLabels(metric).toArray(new String[0]));
+ Gauge.Builder builder =
Gauge.build().name(getMetricName(metric)).help(getMetricHelpMessage(metric));
+ if (null != getMetricLabels(metric)) {
+ builder.labelNames(getMetricLabels(metric).toArray(new String[0]));
}
- Gauge gauge = builder.register();
- GaugeWrapper wrapper = new GaugeWrapper(gauge);
- return Optional.of(wrapper);
+ return Optional.of(new GaugeWrapper(builder.register()));
}
private Optional<MetricsWrapper> createHistogram(final Map<String, Object>
metric) {
- Histogram.Builder builder = Histogram.build()
- .name(getName(metric))
- .help(getHelp(metric));
- if (null != getLabels(metric)) {
- builder.labelNames(getLabels(metric).toArray(new String[0]));
+ Histogram.Builder builder =
Histogram.build().name(getMetricName(metric)).help(getMetricHelpMessage(metric));
+ if (null != getMetricLabels(metric)) {
+ builder.labelNames(getMetricLabels(metric).toArray(new String[0]));
}
- if (null != getProps(metric)) {
- parserHistogramProps(builder, getProps(metric));
- }
- Histogram histogram = builder.register();
- HistogramWrapper wrapper = new HistogramWrapper(histogram);
- return Optional.of(wrapper);
- }
-
- private Optional<MetricsWrapper> createSummary(final Map<String, Object>
metric) {
- Summary.Builder builder = Summary.build()
- .name(getName(metric))
- .help(getHelp(metric));
- if (null != getLabels(metric)) {
- builder.labelNames(getLabels(metric).toArray(new String[0]));
+ if (null != getMetricProperties(metric)) {
+ parserHistogramProperties(builder, getMetricProperties(metric));
}
- Summary summary = builder.register();
- SummaryWrapper wrapper = new SummaryWrapper(summary);
- return Optional.of(wrapper);
+ return Optional.of(new HistogramWrapper(builder.register()));
}
@SuppressWarnings("unchecked")
- private void parserHistogramProps(final Histogram.Builder builder, final
Map<String, Object> props) {
+ private void parserHistogramProperties(final Histogram.Builder builder,
final Map<String, Object> props) {
if (null == props.get("buckets")) {
return;
}
- Map<String, Object> b = (Map<String, Object>) props.get("buckets");
- if ("exp".equals(b.get("type"))) {
- double start = null == b.get("start") ? 1 :
Double.parseDouble(b.get("start").toString());
- double factor = null == b.get("factor") ? 1 :
Double.parseDouble(b.get("factor").toString());
- int count = null == b.get("count") ? 1 : (int) b.get("count");
+ Map<String, Object> buckets = (Map<String, Object>)
props.get("buckets");
+ if ("exp".equals(buckets.get("type"))) {
+ double start = null == buckets.get("start") ? 1 :
Double.parseDouble(buckets.get("start").toString());
+ double factor = null == buckets.get("factor") ? 1 :
Double.parseDouble(buckets.get("factor").toString());
+ int count = null == buckets.get("count") ? 1 : (int)
buckets.get("count");
builder.exponentialBuckets(start, factor, count);
- } else if ("linear".equals(b.get("type"))) {
- double start = null == b.get("start") ? 1 :
Double.parseDouble(b.get("start").toString());
- double width = null == b.get("width") ? 1 :
Double.parseDouble(b.get("width").toString());
- int count = null == b.get("count") ? 1 : (int) b.get("count");
+ } else if ("linear".equals(buckets.get("type"))) {
+ double start = null == buckets.get("start") ? 1 :
Double.parseDouble(buckets.get("start").toString());
+ double width = null == buckets.get("width") ? 1 :
Double.parseDouble(buckets.get("width").toString());
+ int count = null == buckets.get("count") ? 1 : (int)
buckets.get("count");
builder.linearBuckets(start, width, count);
- }
+ }
}
- /**
- * Get metric represented as map.
- *
- * @param id metric id
- * @return the optional of metric map
- */
- protected Optional<Map<String, Object>> findById(final String id) {
- return metrics.stream().filter(m -> id.equals(getId(m))).findFirst();
+ private Optional<MetricsWrapper> createSummary(final Map<String, Object>
metric) {
+ Summary.Builder builder =
Summary.build().name(getMetricName(metric)).help(getMetricHelpMessage(metric));
+ if (null != getMetricLabels(metric)) {
+ builder.labelNames(getMetricLabels(metric).toArray(new String[0]));
+ }
+ return Optional.of(new SummaryWrapper(builder.register()));
}
- /**
- * Get metric id.
- *
- * @param metric metric Map
- * @return id of the metric
- */
- protected String getId(final Map<String, Object> metric) {
+ private String getMetricId(final Map<String, Object> metric) {
return (String) metric.get("id");
}
- /**
- * Get metric type.
- *
- * @param metric metric Map
- * @return type of the metric
- */
- protected String getType(final Map<String, Object> metric) {
+ private String getMetricType(final Map<String, Object> metric) {
return (String) metric.get("type");
}
- /**
- * Get metric name.
- *
- * @param metric metric Map
- * @return name of the metric
- */
- protected String getName(final Map<String, Object> metric) {
+ private String getMetricName(final Map<String, Object> metric) {
return (String) metric.get("name");
}
- /**
- * Get metric help message.
- *
- * @param metric metric Map
- * @return help message of the metric
- */
- protected String getHelp(final Map<String, Object> metric) {
+ private String getMetricHelpMessage(final Map<String, Object> metric) {
return (String) metric.get("help");
}
- /**
- * Get metric labels.
- *
- * @param metric metric Map
- * @return labels of the metric
- */
@SuppressWarnings("unchecked")
- protected List<String> getLabels(final Map<String, Object> metric) {
+ private List<String> getMetricLabels(final Map<String, Object> metric) {
return (List<String>) metric.get("labels");
}
- /**
- * Get metric properties.
- *
- * @param metric metric Map
- * @return properties of the metric
- */
@SuppressWarnings("unchecked")
- protected Map<String, Object> getProps(final Map<String, Object> metric) {
+ private Map<String, Object> getMetricProperties(final Map<String, Object>
metric) {
return (Map<String, Object>) metric.get("props");
}
}
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 1f12a1c..fcb5bda 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
@@ -17,19 +17,14 @@
package org.apache.shardingsphere.agent.metrics.prometheus.collector;
-import io.prometheus.client.Collector.MetricFamilySamples;
import org.junit.Test;
-import java.util.List;
-
import static org.junit.Assert.assertFalse;
public final class BuildInfoCollectorTest {
-
+
@Test
public void assertCollect() {
- BuildInfoCollector buildInfoCollector = new BuildInfoCollector();
- List<MetricFamilySamples> metricFamilySamples =
buildInfoCollector.collect();
- assertFalse(metricFamilySamples.isEmpty());
+ assertFalse(new BuildInfoCollector().collect().isEmpty());
}
}
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/MetaDataInfoCollectorTest.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/MetaDataInfoCollectorTest.java
index 905a14a..6184f76 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/MetaDataInfoCollectorTest.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/MetaDataInfoCollectorTest.java
@@ -17,19 +17,14 @@
package org.apache.shardingsphere.agent.metrics.prometheus.collector;
-import io.prometheus.client.Collector;
import org.junit.Test;
-import java.util.List;
-
import static org.junit.Assert.assertFalse;
public final class MetaDataInfoCollectorTest {
@Test
public void assertCollect() {
- MetaDataInfoCollector metaDataInfoCollector = new
MetaDataInfoCollector();
- List<Collector.MetricFamilySamples> metricFamilySamples =
metaDataInfoCollector.collect();
- assertFalse(metricFamilySamples.isEmpty());
+ assertFalse(new MetaDataInfoCollector().collect().isEmpty());
}
}
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/ProxyInfoCollectorTest.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/ProxyInfoCollectorTest.java
index 24c34bb..4d70753 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/ProxyInfoCollectorTest.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/ProxyInfoCollectorTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.agent.metrics.prometheus.collector;
-import io.prometheus.client.Collector;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
@@ -28,8 +27,6 @@ import
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.junit.Test;
-import java.util.List;
-
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
@@ -39,8 +36,6 @@ public final class ProxyInfoCollectorTest {
public void assertCollect() {
ProxyContext.getInstance().getContextManager().init(mock(MetaDataContexts.class),
mock(TransactionContexts.class),
new InstanceContext(new
ComputeNodeInstance(mock(InstanceDefinition.class)), new
MemoryWorkerIdGenerator(), new ModeConfiguration("Memory", null, false)));
- ProxyInfoCollector proxyInfoCollector = new ProxyInfoCollector();
- List<Collector.MetricFamilySamples> metricFamilySamples =
proxyInfoCollector.collect();
- assertFalse(metricFamilySamples.isEmpty());
+ assertFalse(new ProxyInfoCollector().collect().isEmpty());
}
}
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/definition/PrometheusPluginDefinitionServiceTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionServi
[...]
index 2ee7da3..a4642f4 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionServiceTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/definition/PrometheusPluginDefinitionServiceTest.java
@@ -17,26 +17,15 @@
package org.apache.shardingsphere.agent.metrics.prometheus.definition;
-import org.apache.shardingsphere.agent.api.point.PluginInterceptorPoint;
import org.junit.Test;
-import java.util.Collection;
-
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class PrometheusPluginDefinitionServiceTest {
- private final PrometheusPluginDefinitionService
metricsPluginDefinitionService = new PrometheusPluginDefinitionService();
-
@Test
public void assertDefine() {
- Collection<PluginInterceptorPoint> interceptorPointList =
metricsPluginDefinitionService.install();
- assertThat(interceptorPointList.size(), is(5));
- }
-
- @Test
- public void assertType() {
- assertThat(metricsPluginDefinitionService.getType(), is("Prometheus"));
+ assertThat(new PrometheusPluginDefinitionService().install().size(),
is(5));
}
}
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/service/PrometheusPluginBootServiceTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
index cbb4191..54b4341 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
@@ -17,8 +17,6 @@
package org.apache.shardingsphere.agent.metrics.prometheus.service;
-import io.prometheus.client.exporter.HTTPServer;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.agent.config.PluginConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
@@ -31,41 +29,29 @@ import
org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.junit.AfterClass;
import org.junit.Test;
-import java.lang.reflect.Field;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public final class PrometheusPluginBootServiceTest {
private static final PrometheusPluginBootService
PROMETHEUS_PLUGIN_BOOT_SERVICE = new PrometheusPluginBootService();
- @SneakyThrows
+ @AfterClass
+ public static void close() {
+ PROMETHEUS_PLUGIN_BOOT_SERVICE.close();
+ }
+
@Test
- public void assertStart() {
+ public void assertStart() throws IOException {
ProxyContext.getInstance().getContextManager().init(mock(MetaDataContexts.class),
mock(TransactionContexts.class),
new InstanceContext(new
ComputeNodeInstance(mock(InstanceDefinition.class)), new
MemoryWorkerIdGenerator(), new ModeConfiguration("Memory", null, false)));
Properties props = new Properties();
- props.setProperty("JVM_INFORMATION_COLLECTOR_ENABLED", "true");
- PluginConfiguration configuration = new
PluginConfiguration("localhost", 8090, "", props);
- PROMETHEUS_PLUGIN_BOOT_SERVICE.start(configuration);
- Field field =
PrometheusPluginBootService.class.getDeclaredField("httpServer");
- field.setAccessible(true);
- HTTPServer httpServer = (HTTPServer)
field.get(PROMETHEUS_PLUGIN_BOOT_SERVICE);
- assertNotNull(httpServer);
- assertThat(httpServer.getPort(), is(8090));
- }
-
- @Test
- public void assertType() {
- assertThat(PROMETHEUS_PLUGIN_BOOT_SERVICE.getType(), is("Prometheus"));
- }
-
- @AfterClass
- public static void close() {
- PROMETHEUS_PLUGIN_BOOT_SERVICE.close();
+ props.setProperty("JVM_INFORMATION_COLLECTOR_ENABLED",
Boolean.TRUE.toString());
+ PROMETHEUS_PLUGIN_BOOT_SERVICE.start(new
PluginConfiguration("localhost", 8090, "", props));
+ new Socket().connect(new InetSocketAddress("localhost", 8090));
}
}
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/wrapper/CounterWrapperTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/CounterWrapperTest.java
index 99bf840..deead2a 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/CounterWrapperTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/CounterWrapperTest.java
@@ -22,8 +22,10 @@ import
org.apache.shardingsphere.agent.metrics.prometheus.util.ReflectiveUtil;
import org.hamcrest.Matchers;
import org.junit.Test;
-public final class CounterWrapperTest {
+import static org.junit.Assert.assertThat;
+public final class CounterWrapperTest {
+
@Test
public void assertCreate() {
Counter counter = Counter.build().name("a").help("help").create();
@@ -31,6 +33,6 @@ public final class CounterWrapperTest {
counterWrapper.inc();
counterWrapper.inc(1);
counter = (Counter) ReflectiveUtil.getFieldValue(counterWrapper,
"counter");
- org.hamcrest.MatcherAssert.assertThat(counter.get(), Matchers.is(2.0));
+ assertThat(counter.get(), Matchers.is(2.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/wrapper/PrometheusWrapperFactoryTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactoryTest.java
index e8476d2..4dbab26 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactoryTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/PrometheusWrapperFactoryTest.java
@@ -17,27 +17,19 @@
package org.apache.shardingsphere.agent.metrics.prometheus.wrapper;
-import org.apache.shardingsphere.agent.metrics.api.MetricsWrapper;
import org.junit.Test;
-import java.util.Optional;
-
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
public final class PrometheusWrapperFactoryTest {
@Test
public void assertCreate() {
PrometheusWrapperFactory factory = new PrometheusWrapperFactory();
- Optional<MetricsWrapper> counterWrapper = factory.create("a");
- assertNotNull(counterWrapper.get());
- Optional<MetricsWrapper> gaugeWrapper = factory.create("b");
- assertNotNull(gaugeWrapper.get());
- Optional<MetricsWrapper> histogramWrapper1 = factory.create("c");
- assertNotNull(histogramWrapper1.get());
- Optional<MetricsWrapper> histogramWrapper2 = factory.create("d");
- assertNotNull(histogramWrapper2.get());
- Optional<MetricsWrapper> summaryWrapper = factory.create("e");
- assertNotNull(summaryWrapper.get());
+ assertTrue(factory.create("a").isPresent());
+ assertTrue(factory.create("b").isPresent());
+ assertTrue(factory.create("c").isPresent());
+ assertTrue(factory.create("d").isPresent());
+ assertTrue(factory.create("e").isPresent());
}
}
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/wrapper/SummaryWrapperTest.java
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/SummaryWrapperTest.java
index 297e3c8..ec5841a 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/SummaryWrapperTest.java
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/SummaryWrapperTest.java
@@ -28,10 +28,9 @@ public final class SummaryWrapperTest {
@Test
public void assertCreate() {
- Summary summary = Summary.build().name("a").help("help").create();
- SummaryWrapper summaryWrapper = new SummaryWrapper(summary);
+ SummaryWrapper summaryWrapper = new
SummaryWrapper(Summary.build().name("a").help("help").create());
summaryWrapper.observe(1);
- summary = (Summary) ReflectiveUtil.getFieldValue(summaryWrapper,
"summary");
+ Summary summary = (Summary)
ReflectiveUtil.getFieldValue(summaryWrapper, "summary");
assertThat(summary.collect().size(), is(1));
}
}
diff --git
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/resources/prometheus/metrics.yaml
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/resources/prometheus/metrics.yaml
index 78d535a..d514a07 100644
---
a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/resources/prometheus/metrics.yaml
+++
b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/resources/prometheus/metrics.yaml
@@ -16,7 +16,7 @@
#
metrics:
- - id: a
+ - id: a
type: COUNTER
name: proxy_request_total
help: the shardingsphere proxy request total
diff --git
a/shardingsphere-infra/shardingsphere-infra-datetime/shardingsphere-infra-datetime-type/shardingsphere-database-datetime/pom.xml
b/shardingsphere-infra/shardingsphere-infra-datetime/shardingsphere-infra-datetime-type/shardingsphere-database-datetime/pom.xml
index 58b2fe9..8aee66d 100644
---
a/shardingsphere-infra/shardingsphere-infra-datetime/shardingsphere-infra-datetime-type/shardingsphere-database-datetime/pom.xml
+++
b/shardingsphere-infra/shardingsphere-infra-datetime/shardingsphere-infra-datetime-type/shardingsphere-database-datetime/pom.xml
@@ -41,11 +41,6 @@
</dependency>
<dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
@@ -57,5 +52,10 @@
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
index 1e9f2b2..ab7e0a3 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
@@ -151,12 +151,6 @@
</dependency>
<dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- <scope>compile</scope>
- </dependency>
-
- <dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
@@ -172,5 +166,10 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
index 3f98e12..ac4701a 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
@@ -21,7 +21,6 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
-import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import
org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTemplate;
import
org.apache.shardingsphere.driver.jdbc.adapter.invocation.MethodInvocationRecorder;
@@ -110,7 +109,7 @@ public final class ConnectionManager implements
ExecutorJDBCConnectionManager, A
ShardingSphereUser user = users.iterator().next();
props.put("username", user.getGrantee().getUsername());
props.put("password", user.getPassword());
- return new DataSourceProperties(HikariDataSource.class.getName(),
props);
+ return new DataSourceProperties("com.zaxxer.hikari.HikariDataSource",
props);
}
private String createJdbcUrl(final InstanceId instanceId, final String
schema, final Map<String, Object> props) {
diff --git
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/pom.xml
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/pom.xml
index fab1bb0..a6e7dbb 100644
---
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/pom.xml
+++
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/pom.xml
@@ -46,10 +46,6 @@
</dependency>
<dependency>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- </dependency>
- <dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
@@ -73,5 +69,10 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ </dependency>
</dependencies>
</project>