You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/12/07 04:55:39 UTC
[shardingsphere] branch master updated: Add agent metrics impl
(#8510)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 398ac7c Add agent metrics impl (#8510)
398ac7c is described below
commit 398ac7c6e82520f7f94564939722fec6e62ea9e3
Author: xiaoyu <54...@qq.com>
AuthorDate: Mon Dec 7 12:55:14 2020 +0800
Add agent metrics impl (#8510)
* Add agent metrics impl
---
.../core/plugin/advice/MethodAroundAdvice.java | 9 ++-
.../metrics/api/reporter/MetricsReporter.java | 69 ++++++++++++++++-
.../shardingsphere-agent-metrics-bootstrap/pom.xml | 9 ++-
.../metrics/bootstrap/ChannelHandlerAdvice.java | 55 +++++++++++++
.../bootstrap/CommandExecutorTaskAdvice.java | 52 +++++++++++++
.../metrics/bootstrap/DataNodeRouterAdvice.java | 90 ++++++++++++++++++++++
...Definition.java => ElapsedTimeThreadLocal.java} | 48 ++++++++----
...uginDefinition.java => MethodNameConstant.java} | 28 +++----
.../agent/metrics/bootstrap/TransactionAdvice.java | 50 ++++++++++++
.../definition/MetricsPluginDefinition.java | 69 +++++++++++++++++
10 files changed, 442 insertions(+), 37 deletions(-)
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundAdvice.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundAdvice.java
index 5af9c3a..ba26060 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundAdvice.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/advice/MethodAroundAdvice.java
@@ -33,7 +33,8 @@ public interface MethodAroundAdvice {
* @param args all method arguments
* @param result wrapped class of result to detect whether or not to execute the origin method
*/
- void beforeMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result);
+ default void beforeMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result) {
+ }
/**
* Intercept the target method and weave the method after origin method. It will invoke after the origin calling
@@ -43,7 +44,8 @@ public interface MethodAroundAdvice {
* @param args all method arguments
* @param result wrapped class of result to detect whether or not to execute the origin method.
*/
- void afterMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result);
+ default void afterMethod(TargetObject target, Method method, Object[] args, MethodInvocationResult result) {
+ }
/**
* Weaving the method after origin method throwing.
@@ -53,5 +55,6 @@ public interface MethodAroundAdvice {
* @param args all method arguments
* @param throwable exception from target method
*/
- void onThrowing(TargetObject target, Method method, Object[] args, Throwable throwable);
+ default void onThrowing(TargetObject target, Method method, Object[] args, Throwable throwable) {
+ }
}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
index fe432de..f417b06 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/reporter/MetricsReporter.java
@@ -65,6 +65,16 @@ public final class MetricsReporter {
}
/**
+ * Register counter.
+ *
+ * @param name name
+ * @param document document for counter
+ */
+ public static void registerCounter(final String name, final String document) {
+ METRICS_REGISTER.registerCounter(name, null, document);
+ }
+
+ /**
* Register gauge.
*
* @param name name
@@ -76,7 +86,17 @@ public final class MetricsReporter {
}
/**
- * Register histogram.
+ * Register gauge.
+ *
+ * @param name name
+ * @param document document for gauge
+ */
+ public static void registerGauge(final String name, final String document) {
+ METRICS_REGISTER.registerGauge(name, null, document);
+ }
+
+ /**
+ * Register histogram by label names.
*
* @param name name
* @param labelNames label names
@@ -87,6 +107,16 @@ public final class MetricsReporter {
}
/**
+ * Register histogram.
+ *
+ * @param name name
+ * @param document document for histogram
+ */
+ public static void registerHistogram(final String name, final String document) {
+ METRICS_REGISTER.registerHistogram(name, null, document);
+ }
+
+ /**
* Counter increment.
*
* @param name name
@@ -97,6 +127,15 @@ public final class MetricsReporter {
}
/**
+ * Counter increment.
+ *
+ * @param name name
+ */
+ public static void counterIncrement(final String name) {
+ counterIncrement(name, null);
+ }
+
+ /**
* Counter increment by count.
*
* @param name name
@@ -118,6 +157,15 @@ public final class MetricsReporter {
}
/**
+ * Gauge increment.
+ *
+ * @param name name
+ */
+ public static void gaugeIncrement(final String name) {
+ gaugeIncrement(name, null);
+ }
+
+ /**
* Gauge decrement.
*
* @param name name
@@ -128,6 +176,15 @@ public final class MetricsReporter {
}
/**
+ * Gauge decrement.
+ *
+ * @param name name
+ */
+ public static void gaugeDecrement(final String name) {
+ gaugeDecrement(name, null);
+ }
+
+ /**
* Record time by duration.
*
* @param name name
@@ -138,6 +195,16 @@ public final class MetricsReporter {
METRICS_REGISTER.recordTime(name, labelValues, duration);
}
+ /**
+ * Record time by duration.
+ *
+ * @param name name
+ * @param duration duration
+ */
+ public static void recordTime(final String name, final long duration) {
+ recordTime(name, null, duration);
+ }
+
private static String[] getLabelNames(final List<String> labels) {
return labels.toArray(new String[0]);
}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/pom.xml b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/pom.xml
index 0c79c79..1d38a39 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/pom.xml
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/pom.xml
@@ -29,7 +29,8 @@
<artifactId>shardingsphere-agent-metrics-bootstrap</artifactId>
<properties>
- <entrypoint.class>org.apache.shardingsphere.agent.metrics.bootstrap.MetricsPluginDefinition</entrypoint.class>
+ <entrypoint.class>org.apache.shardingsphere.agent.metrics.bootstrap.definition.MetricsPluginDefinition</entrypoint.class>
+ <shardingsphere.version>5.0.0-RC1-SNAPSHOT</shardingsphere.version>
</properties>
<dependencies>
@@ -38,6 +39,12 @@
<artifactId>shardingsphere-agent-metrics-api</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-infra-route</artifactId>
+ <version>${shardingsphere.version}</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ChannelHandlerAdvice.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ChannelHandlerAdvice.java
new file mode 100644
index 0000000..7345add
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ChannelHandlerAdvice.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.shardingsphere.agent.metrics.bootstrap;
+
+import java.lang.reflect.Method;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundAdvice;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
+
+/**
+ * Channel handler advice.
+ */
+public final class ChannelHandlerAdvice implements MethodAroundAdvice {
+
+ private static final String REQUEST_TOTAL = "proxy_request_total";
+
+ private static final String COLLECTION_TOTAL = "proxy_connection_total";
+
+ static {
+ MetricsReporter.registerCounter(REQUEST_TOTAL, "the shardingsphere proxy request total");
+ MetricsReporter.registerGauge(COLLECTION_TOTAL, "the shardingsphere proxy connection total");
+ }
+
+ @Override
+ public void beforeMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ collectMetrics(method.getName());
+ }
+
+ private void collectMetrics(final String methodName) {
+ if (MethodNameConstant.CHANNEL_READ.equals(methodName)) {
+ MetricsReporter.counterIncrement(REQUEST_TOTAL);
+ } else if (MethodNameConstant.CHANNEL_ACTIVE.equals(methodName)) {
+ MetricsReporter.gaugeIncrement(COLLECTION_TOTAL);
+ } else if (MethodNameConstant.CHANNEL_INACTIVE.equals(methodName)) {
+ MetricsReporter.gaugeDecrement(COLLECTION_TOTAL);
+ }
+ }
+}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/CommandExecutorTaskAdvice.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/CommandExecutorTaskAdvice.java
new file mode 100644
index 0000000..b665260
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/CommandExecutorTaskAdvice.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.metrics.bootstrap;
+
+import java.lang.reflect.Method;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundAdvice;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
+
+/**
+ * Command executor task advice.
+ */
+public final class CommandExecutorTaskAdvice implements MethodAroundAdvice {
+
+ private static final String METRICS_NAME = " proxy_execute_latency_millis";
+
+ static {
+ MetricsReporter.registerHistogram(METRICS_NAME, "the shardingsphere proxy executor latency millis");
+ }
+
+ @Override
+ public void beforeMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ ElapsedTimeThreadLocal.INSTANCE.set(System.currentTimeMillis());
+ }
+
+ @Override
+ public void afterMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ try {
+ long elapsedTime = System.currentTimeMillis() - ElapsedTimeThreadLocal.INSTANCE.get();
+ MetricsReporter.recordTime(METRICS_NAME, elapsedTime);
+ } finally {
+ ElapsedTimeThreadLocal.INSTANCE.remove();
+ }
+ }
+}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/DataNodeRouterAdvice.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/DataNodeRouterAdvice.java
new file mode 100644
index 0000000..3454f9b
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/DataNodeRouterAdvice.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.shardingsphere.agent.metrics.bootstrap;
+
+import java.lang.reflect.Method;
+import java.util.Collection;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundAdvice;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
+
+/**
+ * Data node router advice.
+ */
+public final class DataNodeRouterAdvice implements MethodAroundAdvice {
+
+ private static final String SELECT = "sql_select_total";
+
+ private static final String UPDATE = "sql_update_total";
+
+ private static final String DELETE = "sql_delete_total";
+
+ private static final String INSERT = "sql_insert_total";
+
+ private static final String ROUTE_DATASOURCE = "route_datasource";
+
+ private static final String ROUTE_TABLE = "route_table";
+
+ static {
+ MetricsReporter.registerCounter(SELECT, "the shardingsphere proxy executor select sql total");
+ MetricsReporter.registerCounter(UPDATE, "the shardingsphere proxy executor update sql total");
+ MetricsReporter.registerCounter(DELETE, "the shardingsphere proxy executor delete sql total");
+ MetricsReporter.registerCounter(INSERT, "the shardingsphere proxy executor insert sql total");
+ MetricsReporter.registerCounter(ROUTE_DATASOURCE, new String[] {"name"}, "the shardingsphere proxy route datasource");
+ MetricsReporter.registerCounter(ROUTE_TABLE, new String[] {"name"}, "the shardingsphere proxy route table");
+ }
+
+ @Override
+ public void beforeMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ SQLStatement sqlStatement = (SQLStatement) args[0];
+ if (sqlStatement instanceof InsertStatement) {
+ MetricsReporter.counterIncrement(SELECT);
+ } else if (sqlStatement instanceof DeleteStatement) {
+ MetricsReporter.counterIncrement(DELETE);
+ } else if (sqlStatement instanceof UpdateStatement) {
+ MetricsReporter.counterIncrement(UPDATE);
+ } else if (sqlStatement instanceof SelectStatement) {
+ MetricsReporter.counterIncrement(INSERT);
+ }
+ }
+
+ @Override
+ public void afterMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ RouteContext routeContext = (RouteContext) result.getResult();
+ if (null != routeContext) {
+ Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
+ for (RouteUnit each : routeUnits) {
+ RouteMapper dataSourceMapper = each.getDataSourceMapper();
+ MetricsReporter.counterIncrement(ROUTE_DATASOURCE, new String[] {dataSourceMapper.getActualName()});
+ for (RouteMapper table : each.getTableMappers()) {
+ MetricsReporter.counterIncrement(ROUTE_TABLE, new String[] {table.getActualName()});
+ }
+ }
+ }
+ }
+}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ElapsedTimeThreadLocal.java
similarity index 59%
copy from shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
copy to shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ElapsedTimeThreadLocal.java
index 8de1da6..bb973ad 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/ElapsedTimeThreadLocal.java
@@ -13,28 +13,44 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
*/
package org.apache.shardingsphere.agent.metrics.bootstrap;
-import net.bytebuddy.matcher.ElementMatchers;
-import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;
-
/**
- * Metrics plugin definition.
+ * The enum Elapsed time thread local.
*/
-public class MetricsPluginDefinition extends PluginDefinition {
-
- public MetricsPluginDefinition() {
- super("sample-metrics");
+public enum ElapsedTimeThreadLocal {
+
+ /**
+ * Instance elapsed time thread local.
+ */
+ INSTANCE;
+
+ private static final ThreadLocal<Long> CURRENT_LOCAL = new ThreadLocal<>();
+
+ /**
+ * Set.
+ *
+ * @param time the time
+ */
+ public void set(final long time) {
+ CURRENT_LOCAL.set(time);
}
-
- @Override
- protected void define() {
- intercept("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask")
- .aroundInstanceMethod(ElementMatchers.named("run"))
- .implement("org.apache.shardingsphere.agent.plugin.trace.SampleAdvice")
- .build();
+
+ /**
+ * Get long.
+ *
+ * @return the long
+ */
+ public Long get() {
+ return CURRENT_LOCAL.get();
+ }
+
+ /**
+ * Remove.
+ */
+ public void remove() {
+ CURRENT_LOCAL.remove();
}
}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MethodNameConstant.java
similarity index 59%
rename from shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
rename to shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MethodNameConstant.java
index 8de1da6..23491e3 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MetricsPluginDefinition.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/MethodNameConstant.java
@@ -18,23 +18,19 @@
package org.apache.shardingsphere.agent.metrics.bootstrap;
-import net.bytebuddy.matcher.ElementMatchers;
-import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;
-
/**
- * Metrics plugin definition.
+ * Method name constant.
*/
-public class MetricsPluginDefinition extends PluginDefinition {
-
- public MetricsPluginDefinition() {
- super("sample-metrics");
- }
+public final class MethodNameConstant {
- @Override
- protected void define() {
- intercept("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask")
- .aroundInstanceMethod(ElementMatchers.named("run"))
- .implement("org.apache.shardingsphere.agent.plugin.trace.SampleAdvice")
- .build();
- }
+ public static final String CHANNEL_ACTIVE = "channelActive";
+
+ public static final String CHANNEL_READ = "channelRead";
+
+ public static final String CHANNEL_INACTIVE = "channelInactive";
+
+ public static final String COMMIT = "commit";
+
+ public static final String ROLL_BACK = "rollback";
+
}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/TransactionAdvice.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/TransactionAdvice.java
new file mode 100644
index 0000000..9ea14e6
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/TransactionAdvice.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.shardingsphere.agent.metrics.bootstrap;
+
+import java.lang.reflect.Method;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundAdvice;
+import org.apache.shardingsphere.agent.core.plugin.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject;
+import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
+
+/**
+ * Transaction advice.
+ */
+public final class TransactionAdvice implements MethodAroundAdvice {
+
+ private static final String COMMIT = "proxy_transaction_commit";
+
+ private static final String ROLLBACK = "proxy_transaction_rollback";
+
+ static {
+ MetricsReporter.registerCounter(COMMIT, "the shardingsphere proxy transaction commit count total");
+ MetricsReporter.registerCounter(ROLLBACK, "the shardingsphere proxy transaction rollback count total");
+ }
+
+ @Override
+ public void beforeMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
+ String methodName = method.getName();
+ if (MethodNameConstant.COMMIT.equals(methodName)) {
+ MetricsReporter.counterIncrement(COMMIT);
+ } else if (MethodNameConstant.ROLL_BACK.equals(methodName)) {
+ MetricsReporter.counterIncrement(ROLLBACK);
+ }
+ }
+}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/definition/MetricsPluginDefinition.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/definition/MetricsPluginDefinition.java
new file mode 100644
index 0000000..0d2ab43
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-bootstrap/src/main/java/org/apache/shardingsphere/agent/metrics/bootstrap/definition/MetricsPluginDefinition.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.bootstrap.definition;
+
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;
+import org.apache.shardingsphere.agent.metrics.bootstrap.MethodNameConstant;
+
+/**
+ * Metrics plugin definition.
+ */
+public final class MetricsPluginDefinition extends PluginDefinition {
+
+ private static final String COMMAND_EXECUTOR_TASK_ENHANCE_CLASS = "org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask";
+
+ private static final String COMMAND_EXECUTOR_TASK_ADVICE_CLASS = "org.apache.shardingsphere.agent.metrics.bootstrap.CommandExecutorTaskAdvice";
+
+ private static final String CHANNEL_HANDLER_ENHANCE_CLASS = "org.apache.shardingsphere.proxy.frontend.netty.FrontendChannelInboundHandler";
+
+ private static final String CHANNEL_HANDLER_ADVICE_CLASS = "org.apache.shardingsphere.agent.metrics.bootstrap.ChannelHandlerAdvice";
+
+ private static final String DATA_NODE_ROUTER_ENHANCE_CLASS = "org.apache.shardingsphere.infra.route.DataNodeRouter";
+
+ private static final String DATA_NODE_ROUTER_ADVICE_CLASS = "org.apache.shardingsphere.agent.metrics.bootstrap.DataNodeRouterAdvice";
+
+ private static final String TRANSACTION_ENHANCE_CLASS = "org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.BackendTransactionManager";
+
+ private static final String TRANSACTION_ADVICE_CLASS = "org.apache.shardingsphere.agent.metrics.bootstrap.TransactionAdvice";
+
+ public MetricsPluginDefinition() {
+ super("Metrics");
+ }
+
+ @Override
+ protected void define() {
+ intercept(COMMAND_EXECUTOR_TASK_ENHANCE_CLASS)
+ .aroundInstanceMethod(ElementMatchers.named("run"))
+ .implement(COMMAND_EXECUTOR_TASK_ADVICE_CLASS)
+ .build();
+ intercept(CHANNEL_HANDLER_ENHANCE_CLASS)
+ .aroundInstanceMethod(ElementMatchers.named(MethodNameConstant.CHANNEL_ACTIVE).or(ElementMatchers.named(MethodNameConstant.CHANNEL_INACTIVE))
+ .or(ElementMatchers.named(MethodNameConstant.CHANNEL_READ)))
+ .implement(CHANNEL_HANDLER_ADVICE_CLASS)
+ .build();
+ intercept(DATA_NODE_ROUTER_ENHANCE_CLASS)
+ .aroundInstanceMethod(ElementMatchers.named("route"))
+ .implement(DATA_NODE_ROUTER_ADVICE_CLASS)
+ .build();
+ intercept(TRANSACTION_ENHANCE_CLASS)
+ .aroundInstanceMethod(ElementMatchers.named(MethodNameConstant.COMMIT).or(ElementMatchers.named(MethodNameConstant.ROLL_BACK)))
+ .implement(TRANSACTION_ADVICE_CLASS)
+ .build();
+ }
+}