You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/03/08 09:00:15 UTC
[shardingsphere] branch master updated: Add metrics for ShardingSphere-JDBC (#24492)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 7de1747c97d Add metrics for ShardingSphere-JDBC (#24492)
7de1747c97d is described below
commit 7de1747c97d1785562a39301a72171dd00da626b
Author: jiangML <10...@qq.com>
AuthorDate: Wed Mar 8 17:00:06 2023 +0800
Add metrics for ShardingSphere-JDBC (#24492)
* add metrics for jdbc
* optimize code
* optimize test
* add metrics for jdbc
* optimize code
* fix error
* optimize code
* optimize test
* optimize test
* fix checkstyle error
* fix error
---
.../advice/jdbc/AbstractExecuteCountAdvice.java | 44 +++++++++++
.../jdbc/AbstractExecuteErrorsCountAdvice.java | 44 +++++++++++
.../AbstractExecuteLatencyHistogramAdvice.java | 61 +++++++++++++++
.../jdbc/AbstractTransactionsCountAdvice.java | 44 +++++++++++
.../advice/jdbc/CommitTransactionsCountAdvice.java | 29 +++++++
.../jdbc/PreparedStatementExecuteCountAdvice.java | 29 +++++++
.../PreparedStatementExecuteErrorsCountAdvice.java | 29 +++++++
...aredStatementExecuteLatencyHistogramAdvice.java | 33 ++++++++
.../jdbc/RollbackTransactionsCountAdvice.java | 29 +++++++
.../advice/jdbc/StatementExecuteCountAdvice.java | 29 +++++++
.../jdbc/StatementExecuteErrorsCountAdvice.java | 29 +++++++
.../StatementExecuteLatencyHistogramAdvice.java | 33 ++++++++
.../metrics/core/config/MetricConfiguration.java | 4 +
.../impl/jdbc/JDBCMetaDataInfoExporter.java | 78 +++++++++++++++++++
.../core/exporter/impl/jdbc/JDBCStateExporter.java | 74 ++++++++++++++++++
.../jdbc/AbstractExecuteCountAdviceTest.java | 58 ++++++++++++++
.../jdbc/AbstractExecuteErrorsCountAdviceTest.java | 59 ++++++++++++++
.../AbstractExecuteLatencyHistogramAdviceTest.java | 64 +++++++++++++++
.../jdbc/AbstractTransactionsCountAdviceTest.java | 58 ++++++++++++++
.../impl/jdbc/JDBCMetaDataInfoExporterTest.java | 57 ++++++++++++++
.../exporter/impl/jdbc/JDBCStateExporterTest.java | 52 +++++++++++++
.../config/driver/foo-driver-fixture.yaml | 57 ++++++++++++++
.../PrometheusPluginLifecycleService.java | 17 +++-
.../META-INF/conf/prometheus-jdbc-advisors.yaml | 90 ++++++++++++++++++++++
24 files changed, 1099 insertions(+), 2 deletions(-)
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdvice.java
new file mode 100644
index 00000000000..be5f6adbbea
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdvice.java
@@ -0,0 +1,44 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.CounterMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+/**
+ * Abstract execute count advice for ShardingSphere-JDBC.
+ */
+public abstract class AbstractExecuteCountAdvice implements InstanceMethodAdvice {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_total", MetricCollectorType.COUNTER,
+ "Total number of statement execute", Collections.singletonList("statement_type"));
+
+ @Override
+ public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
+ MetricsCollectorRegistry.<CounterMetricsCollector>get(config, pluginType).inc(getStatementType());
+ }
+
+ protected abstract String getStatementType();
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdvice.java
new file mode 100644
index 00000000000..7cefa68af77
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdvice.java
@@ -0,0 +1,44 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.CounterMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+/**
+ * Abstract execute errors count advice for ShardingSphere-JDBC.
+ */
+public abstract class AbstractExecuteErrorsCountAdvice implements InstanceMethodAdvice {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_errors_total", MetricCollectorType.COUNTER,
+ "Total number of statement execute error", Collections.singletonList("statement_type"));
+
+ @Override
+ public void onThrowing(final TargetAdviceObject target, final Method method, final Object[] args, final Throwable throwable, final String pluginType) {
+ MetricsCollectorRegistry.<CounterMetricsCollector>get(config, pluginType).inc(getStatementType());
+ }
+
+ protected abstract String getStatementType();
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdvice.java
new file mode 100644
index 00000000000..5c5759e5512
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdvice.java
@@ -0,0 +1,61 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.HistogramMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Abstract execute latency histogram advice for ShardingSphere-JDBC.
+ */
+public abstract class AbstractExecuteLatencyHistogramAdvice implements InstanceMethodAdvice {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_latency_millis", MetricCollectorType.HISTOGRAM,
+ "Statement execute latency millis histogram", Collections.singletonMap("buckets", getBuckets()));
+
+ private static Map<String, Object> getBuckets() {
+ Map<String, Object> result = new HashMap<>(4, 1);
+ result.put("type", "exp");
+ result.put("start", 1);
+ result.put("factor", 2);
+ result.put("count", 13);
+ return result;
+ }
+
+ @Override
+ public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args, final String pluginType) {
+ getMethodTimeRecorder().record(method);
+ }
+
+ @Override
+ public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
+ MetricsCollectorRegistry.<HistogramMetricsCollector>get(config, pluginType).observe(getMethodTimeRecorder().getElapsedTimeAndClean(method));
+ }
+
+ protected abstract MethodTimeRecorder getMethodTimeRecorder();
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdvice.java
new file mode 100644
index 00000000000..55a7cf5c368
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdvice.java
@@ -0,0 +1,44 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.CounterMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+/**
+ * Abstract transactions count advice for ShardingSphere-JDBC.
+ */
+public abstract class AbstractTransactionsCountAdvice implements InstanceMethodAdvice {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_transactions_total", MetricCollectorType.COUNTER,
+ "Total transactions of ShardingSphere-JDBC", Collections.singletonList("type"));
+
+ @Override
+ public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
+ MetricsCollectorRegistry.<CounterMetricsCollector>get(config, pluginType).inc(getTransactionType());
+ }
+
+ protected abstract String getTransactionType();
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/CommitTransactionsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/CommitTransactionsCountAdvice.java
new file mode 100644
index 00000000000..2b05f64099d
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/CommitTransactionsCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Commit transactions count advice for ShardingSphere-JDBC.
+ */
+public final class CommitTransactionsCountAdvice extends AbstractTransactionsCountAdvice {
+
+ @Override
+ protected String getTransactionType() {
+ return "commit";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteCountAdvice.java
new file mode 100644
index 00000000000..cc806851e85
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Execute count advice for ShardingSpherePreparedStatement.
+ */
+public final class PreparedStatementExecuteCountAdvice extends AbstractExecuteCountAdvice {
+
+ @Override
+ protected String getStatementType() {
+ return "prepared_statement";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteErrorsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteErrorsCountAdvice.java
new file mode 100644
index 00000000000..613f4283134
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteErrorsCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Execute errors count advice for ShardingSpherePreparedStatement.
+ */
+public final class PreparedStatementExecuteErrorsCountAdvice extends AbstractExecuteErrorsCountAdvice {
+
+ @Override
+ protected String getStatementType() {
+ return "prepared_statement";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteLatencyHistogramAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteLatencyHistogramAdvice.java
new file mode 100644
index 00000000000..6f9dd78c432
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/PreparedStatementExecuteLatencyHistogramAdvice.java
@@ -0,0 +1,33 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
+
+/**
+ * Execute latency histogram advance for ShardingSpherePreparedStatement.
+ */
+public final class PreparedStatementExecuteLatencyHistogramAdvice extends AbstractExecuteLatencyHistogramAdvice {
+
+ private final MethodTimeRecorder methodTimeRecorder = new MethodTimeRecorder(PreparedStatementExecuteLatencyHistogramAdvice.class);
+
+ @Override
+ protected MethodTimeRecorder getMethodTimeRecorder() {
+ return methodTimeRecorder;
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/RollbackTransactionsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/RollbackTransactionsCountAdvice.java
new file mode 100644
index 00000000000..1f71c07e002
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/RollbackTransactionsCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Rollback transactions count advice for ShardingSphere-JDBC.
+ */
+public final class RollbackTransactionsCountAdvice extends AbstractTransactionsCountAdvice {
+
+ @Override
+ protected String getTransactionType() {
+ return "rollback";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteCountAdvice.java
new file mode 100644
index 00000000000..e2ecf069a0d
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Execute count advice for ShardingSphereStatement.
+ */
+public final class StatementExecuteCountAdvice extends AbstractExecuteCountAdvice {
+
+ @Override
+ protected String getStatementType() {
+ return "statement";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteErrorsCountAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteErrorsCountAdvice.java
new file mode 100644
index 00000000000..59e8185937c
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteErrorsCountAdvice.java
@@ -0,0 +1,29 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+/**
+ * Execute errors count advice for ShardingSphereStatement.
+ */
+public final class StatementExecuteErrorsCountAdvice extends AbstractExecuteErrorsCountAdvice {
+
+ @Override
+ protected String getStatementType() {
+ return "statement";
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteLatencyHistogramAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteLatencyHistogramAdvice.java
new file mode 100644
index 00000000000..9555b4a6c59
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/StatementExecuteLatencyHistogramAdvice.java
@@ -0,0 +1,33 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
+
+/**
+ * Execute latency histogram advance for ShardingSphereStatement.
+ */
+public final class StatementExecuteLatencyHistogramAdvice extends AbstractExecuteLatencyHistogramAdvice {
+
+ private final MethodTimeRecorder methodTimeRecorder = new MethodTimeRecorder(StatementExecuteLatencyHistogramAdvice.class);
+
+ @Override
+ protected MethodTimeRecorder getMethodTimeRecorder() {
+ return methodTimeRecorder;
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
index 81ddb3b8cd0..581cdd4eb4a 100644
--- a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/config/MetricConfiguration.java
@@ -48,4 +48,8 @@ public final class MetricConfiguration {
public MetricConfiguration(final String id, final MetricCollectorType type, final String help, final List<String> labels) {
this(id, type, help, labels, Collections.emptyMap());
}
+
+ public MetricConfiguration(final String id, final MetricCollectorType type, final String help, final Map<String, Object> props) {
+ this(id, type, help, Collections.emptyList(), props);
+ }
}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java
new file mode 100644
index 00000000000..1c66e0c9644
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java
@@ -0,0 +1,78 @@
+/*
+ * 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.plugin.metrics.core.exporter.impl.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.core.util.AgentReflectionUtil;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.MetricsExporter;
+import org.apache.shardingsphere.driver.ShardingSphereDriver;
+import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import javax.sql.DataSource;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * JDBC meta data information exporter.
+ */
+public final class JDBCMetaDataInfoExporter implements MetricsExporter {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_meta_data_info",
+ MetricCollectorType.GAUGE_METRIC_FAMILY, "Meta data information of ShardingSphere-JDBC",
+ Arrays.asList("database", "type"), Collections.emptyMap());
+
+ @Override
+ public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
+ Optional<ShardingSphereDriver> shardingSphereDriverOptional = getShardingSphereDriver();
+ if (!shardingSphereDriverOptional.isPresent()) {
+ return Optional.empty();
+ }
+ GaugeMetricFamilyMetricsCollector result = MetricsCollectorRegistry.get(config, pluginType);
+ result.cleanMetrics();
+ DriverDataSourceCache dataSourceCache = AgentReflectionUtil.getFieldValue(shardingSphereDriverOptional.get(), "dataSourceCache");
+ Map<String, DataSource> dataSourceMap = AgentReflectionUtil.getFieldValue(dataSourceCache, "dataSourceMap");
+ for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ ShardingSphereDataSource shardingSphereDataSource = (ShardingSphereDataSource) entry.getValue();
+ String databaseName = AgentReflectionUtil.getFieldValue(shardingSphereDataSource, "databaseName");
+ ContextManager contextManager = AgentReflectionUtil.getFieldValue(shardingSphereDataSource, "contextManager");
+ result.addMetric(Arrays.asList(databaseName, "storage_unit_count"), contextManager.getDataSourceMap(databaseName).size());
+ }
+ return Optional.of(result);
+ }
+
+ private Optional<ShardingSphereDriver> getShardingSphereDriver() {
+ Enumeration<Driver> driverEnumeration = DriverManager.getDrivers();
+ while (driverEnumeration.hasMoreElements()) {
+ Driver driver = driverEnumeration.nextElement();
+ if (driver instanceof ShardingSphereDriver) {
+ return Optional.of((ShardingSphereDriver) driver);
+ }
+ }
+ return Optional.empty();
+ }
+}
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporter.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporter.java
new file mode 100644
index 00000000000..072d8f41e3f
--- /dev/null
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporter.java
@@ -0,0 +1,74 @@
+/*
+ * 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.plugin.metrics.core.exporter.impl.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.core.util.AgentReflectionUtil;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.MetricsExporter;
+import org.apache.shardingsphere.driver.ShardingSphereDriver;
+import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import javax.sql.DataSource;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * JDBC state exporter.
+ */
+public final class JDBCStateExporter implements MetricsExporter {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_state", MetricCollectorType.GAUGE_METRIC_FAMILY, "State of ShardingSphere-JDBC. 0 is OK; 1 is CIRCUIT BREAK; 2 is LOCK");
+
+ @Override
+ public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
+ Optional<ShardingSphereDriver> shardingSphereDriverOptional = getShardingSphereDriver();
+ if (!shardingSphereDriverOptional.isPresent()) {
+ return Optional.empty();
+ }
+ GaugeMetricFamilyMetricsCollector result = MetricsCollectorRegistry.get(config, pluginType);
+ result.cleanMetrics();
+ DriverDataSourceCache dataSourceCache = AgentReflectionUtil.getFieldValue(shardingSphereDriverOptional.get(), "dataSourceCache");
+ Map<String, DataSource> dataSourceMap = AgentReflectionUtil.getFieldValue(dataSourceCache, "dataSourceMap");
+ for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ ShardingSphereDataSource shardingSphereDataSource = (ShardingSphereDataSource) entry.getValue();
+ ContextManager contextManager = AgentReflectionUtil.getFieldValue(shardingSphereDataSource, "contextManager");
+ result.addMetric(Collections.emptyList(), contextManager.getInstanceContext().getInstance().getState().getCurrentState().ordinal());
+ }
+ return Optional.of(result);
+ }
+
+ private Optional<ShardingSphereDriver> getShardingSphereDriver() {
+ Enumeration<Driver> driverEnumeration = DriverManager.getDrivers();
+ while (driverEnumeration.hasMoreElements()) {
+ Driver driver = driverEnumeration.nextElement();
+ if (driver instanceof ShardingSphereDriver) {
+ return Optional.of((ShardingSphereDriver) driver);
+ }
+ }
+ return Optional.empty();
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdviceTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdviceTest.java
new file mode 100644
index 00000000000..319117008de
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteCountAdviceTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.TargetAdviceObjectFixture;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class AbstractExecuteCountAdviceTest {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_total", MetricCollectorType.COUNTER,
+ "Total number of statement execute", Collections.singletonList("statement_type"));
+
+ @AfterEach
+ public void reset() {
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertWithStatement() {
+ StatementExecuteCountAdvice advice = new StatementExecuteCountAdvice();
+ advice.afterMethod(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, null, "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("statement=1"));
+ }
+
+ @Test
+ public void assertWithPreparedStatement() {
+ PreparedStatementExecuteCountAdvice advice = new PreparedStatementExecuteCountAdvice();
+ advice.afterMethod(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, null, "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("prepared_statement=1"));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdviceTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdviceTest.java
new file mode 100644
index 00000000000..76151f5510b
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteErrorsCountAdviceTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.TargetAdviceObjectFixture;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class AbstractExecuteErrorsCountAdviceTest {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_errors_total", MetricCollectorType.COUNTER,
+ "Total number of statement execute error", Collections.singletonList("statement_type"));
+
+ @AfterEach
+ public void reset() {
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertWithStatement() {
+ StatementExecuteErrorsCountAdvice advice = new StatementExecuteErrorsCountAdvice();
+ advice.onThrowing(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, mock(IOException.class), "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("statement=1"));
+ }
+
+ @Test
+ public void assertWithPreparedStatement() {
+ PreparedStatementExecuteErrorsCountAdvice advice = new PreparedStatementExecuteErrorsCountAdvice();
+ advice.onThrowing(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, mock(IOException.class), "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("prepared_statement=1"));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdviceTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdviceTest.java
new file mode 100644
index 00000000000..457cf054728
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractExecuteLatencyHistogramAdviceTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.TargetAdviceObjectFixture;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.mockito.Mockito.mock;
+
+public final class AbstractExecuteLatencyHistogramAdviceTest {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_statement_execute_latency_millis", MetricCollectorType.HISTOGRAM, null);
+
+ @AfterEach
+ public void reset() {
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertWithStatement() throws InterruptedException {
+ StatementExecuteLatencyHistogramAdvice advice = new StatementExecuteLatencyHistogramAdvice();
+ TargetAdviceObjectFixture targetObject = new TargetAdviceObjectFixture();
+ Method method = mock(Method.class);
+ advice.beforeMethod(targetObject, method, new Object[]{}, "FIXTURE");
+ Thread.sleep(200L);
+ advice.afterMethod(targetObject, method, new Object[]{}, null, "FIXTURE");
+ assertThat(Double.parseDouble(MetricsCollectorRegistry.get(config, "FIXTURE").toString()), greaterThanOrEqualTo(200d));
+ }
+
+ @Test
+ public void assertWithPreparedStatement() throws InterruptedException {
+ PreparedStatementExecuteLatencyHistogramAdvice advice = new PreparedStatementExecuteLatencyHistogramAdvice();
+ TargetAdviceObjectFixture targetObject = new TargetAdviceObjectFixture();
+ Method method = mock(Method.class);
+ advice.beforeMethod(targetObject, method, new Object[]{}, "FIXTURE");
+ Thread.sleep(200L);
+ advice.afterMethod(targetObject, method, new Object[]{}, null, "FIXTURE");
+ assertThat(Double.parseDouble(MetricsCollectorRegistry.get(config, "FIXTURE").toString()), greaterThanOrEqualTo(200d));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdviceTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdviceTest.java
new file mode 100644
index 00000000000..bbdaf7d036e
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/jdbc/AbstractTransactionsCountAdviceTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.plugin.metrics.core.advice.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.TargetAdviceObjectFixture;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class AbstractTransactionsCountAdviceTest {
+
+ private final MetricConfiguration config = new MetricConfiguration("jdbc_transactions_total", MetricCollectorType.COUNTER,
+ "Total transactions of ShardingSphere-JDBC", Collections.singletonList("type"));
+
+ @AfterEach
+ public void reset() {
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertWithCommit() {
+ CommitTransactionsCountAdvice advice = new CommitTransactionsCountAdvice();
+ advice.afterMethod(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, null, "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("commit=1"));
+ }
+
+ @Test
+ public void assertWithRollback() {
+ RollbackTransactionsCountAdvice advice = new RollbackTransactionsCountAdvice();
+ advice.afterMethod(new TargetAdviceObjectFixture(), mock(Method.class), new Object[]{}, null, "FIXTURE");
+ assertThat(MetricsCollectorRegistry.get(config, "FIXTURE").toString(), is("rollback=1"));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporterTest.java
new file mode 100644
index 00000000000..e7e0687f121
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporterTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.plugin.metrics.core.exporter.impl.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.apache.shardingsphere.driver.ShardingSphereDriver;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public final class JDBCMetaDataInfoExporterTest {
+
+ @AfterEach
+ public void reset() throws SQLException {
+ MetricConfiguration config = new MetricConfiguration("jdbc_meta_data_info",
+ MetricCollectorType.GAUGE_METRIC_FAMILY, "Meta data information of ShardingSphere-JDBC",
+ Arrays.asList("database", "type"), Collections.emptyMap());
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertExport() throws SQLException {
+ DriverManager.registerDriver(new ShardingSphereDriver());
+ DriverManager.getConnection("jdbc:shardingsphere:classpath:config/driver/foo-driver-fixture.yaml");
+ Optional<GaugeMetricFamilyMetricsCollector> collector = new JDBCMetaDataInfoExporter().export("FIXTURE");
+ assertTrue(collector.isPresent());
+ assertThat(collector.get().toString(), is("foo_driver_fixture_db=2, storage_unit_count=2"));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporterTest.java
new file mode 100644
index 00000000000..7219cc63b25
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCStateExporterTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.plugin.metrics.core.exporter.impl.jdbc;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.MetricsCollectorRegistry;
+import org.apache.shardingsphere.agent.plugin.metrics.core.collector.type.GaugeMetricFamilyMetricsCollector;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricCollectorType;
+import org.apache.shardingsphere.agent.plugin.metrics.core.config.MetricConfiguration;
+import org.apache.shardingsphere.agent.plugin.metrics.core.fixture.collector.MetricsCollectorFixture;
+import org.apache.shardingsphere.driver.ShardingSphereDriver;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public final class JDBCStateExporterTest {
+
+ @AfterEach
+ public void reset() {
+ MetricConfiguration config = new MetricConfiguration("jdbc_state", MetricCollectorType.GAUGE_METRIC_FAMILY, "State of ShardingSphere-JDBC. 0 is OK; 1 is CIRCUIT BREAK; 2 is LOCK");
+ ((MetricsCollectorFixture) MetricsCollectorRegistry.get(config, "FIXTURE")).reset();
+ }
+
+ @Test
+ public void assertExport() throws SQLException {
+ DriverManager.registerDriver(new ShardingSphereDriver());
+ Optional<GaugeMetricFamilyMetricsCollector> collector = new JDBCStateExporter().export("FIXTURE");
+ assertTrue(collector.isPresent());
+ assertThat(collector.get().toString(), is("0"));
+ }
+}
diff --git a/agent/plugins/metrics/core/src/test/resources/config/driver/foo-driver-fixture.yaml b/agent/plugins/metrics/core/src/test/resources/config/driver/foo-driver-fixture.yaml
new file mode 100644
index 00000000000..1942b9272c0
--- /dev/null
+++ b/agent/plugins/metrics/core/src/test/resources/config/driver/foo-driver-fixture.yaml
@@ -0,0 +1,57 @@
+#
+# 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.
+#
+
+databaseName: foo_driver_fixture_db
+
+dataSources:
+ ds_0:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: org.h2.Driver
+ jdbcUrl: jdbc:h2:mem:foo_ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ username: sa
+ password:
+ ds_1:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: org.h2.Driver
+ jdbcUrl: jdbc:h2:mem:foo_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ username: sa
+ password:
+
+rules:
+ - !SHARDING
+ autoTables:
+ t_order:
+ actualDataSources: ds_0,ds_1
+ shardingStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: auto-mod
+ keyGenerateStrategy:
+ column: user_id
+ keyGeneratorName: snowflake
+ shardingAlgorithms:
+ auto-mod:
+ type: HASH_MOD
+ props:
+ sharding-count: 2
+
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+
+props:
+ sql-show: true
diff --git a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java
index 21a90111e74..017b6129f50 100644
--- a/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java
+++ b/agent/plugins/metrics/type/prometheus/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/prometheus/PrometheusPluginLifecycleService.java
@@ -25,6 +25,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.agent.api.PluginConfiguration;
import org.apache.shardingsphere.agent.plugin.core.config.validator.PluginConfigurationValidator;
import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl.BuildInfoExporter;
+import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl.jdbc.JDBCMetaDataInfoExporter;
+import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl.jdbc.JDBCStateExporter;
import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl.proxy.ProxyMetaDataInfoExporter;
import org.apache.shardingsphere.agent.plugin.metrics.core.exporter.impl.proxy.ProxyStateExporter;
import org.apache.shardingsphere.agent.plugin.metrics.prometheus.exoprter.PrometheusMetricsExporter;
@@ -63,14 +65,25 @@ public final class PrometheusPluginLifecycleService implements PluginLifecycleSe
private void registerCollector(final boolean isCollectJVMInformation, final boolean isEnhancedForProxy) {
new PrometheusMetricsExporter(new BuildInfoExporter()).register();
if (isEnhancedForProxy) {
- new PrometheusMetricsExporter(new ProxyStateExporter()).register();
- new PrometheusMetricsExporter(new ProxyMetaDataInfoExporter()).register();
+ registerCollectorForProxy();
+ } else {
+ registerCollectorForJDBC();
}
if (isCollectJVMInformation) {
DefaultExports.initialize();
}
}
+ private void registerCollectorForProxy() {
+ new PrometheusMetricsExporter(new ProxyStateExporter()).register();
+ new PrometheusMetricsExporter(new ProxyMetaDataInfoExporter()).register();
+ }
+
+ private void registerCollectorForJDBC() {
+ new PrometheusMetricsExporter(new JDBCStateExporter()).register();
+ new PrometheusMetricsExporter(new JDBCMetaDataInfoExporter()).register();
+ }
+
private InetSocketAddress getSocketAddress(final PluginConfiguration pluginConfig) {
return Strings.isNullOrEmpty(pluginConfig.getHost()) ? new InetSocketAddress(pluginConfig.getPort()) : new InetSocketAddress(pluginConfig.getHost(), pluginConfig.getPort());
}
diff --git a/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-jdbc-advisors.yaml b/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-jdbc-advisors.yaml
index b1312a0905c..2b66546db58 100644
--- a/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-jdbc-advisors.yaml
+++ b/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-jdbc-advisors.yaml
@@ -14,3 +14,93 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+
+advisors:
+ - target: org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.SQLParseCountAdvice
+ pointcuts:
+ - name: parse
+ type: method
+ - target: org.apache.shardingsphere.infra.route.engine.SQLRouteEngine
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.SQLRouteCountAdvice
+ pointcuts:
+ - name: route
+ type: method
+ - target: org.apache.shardingsphere.infra.route.engine.SQLRouteEngine
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.RouteResultCountAdvice
+ pointcuts:
+ - name: route
+ type: method
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.StatementExecuteCountAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ params:
+ - index: 0
+ type: java.lang.String
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.StatementExecuteErrorsCountAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ params:
+ - index: 0
+ type: java.lang.String
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.StatementExecuteLatencyHistogramAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ params:
+ - index: 0
+ type: java.lang.String
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.PreparedStatementExecuteCountAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.PreparedStatementExecuteErrorsCountAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ - target: org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.PreparedStatementExecuteLatencyHistogramAdvice
+ pointcuts:
+ - name: executeQuery
+ type: method
+ - name: execute
+ type: method
+ - name: executeUpdate
+ type: method
+ - target: org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.CommitTransactionsCountAdvice
+ pointcuts:
+ - name: commit
+ type: method
+ - target: org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection
+ advice: org.apache.shardingsphere.agent.plugin.metrics.core.advice.jdbc.RollbackTransactionsCountAdvice
+ pointcuts:
+ - name: rollback
+ type: method