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 2021/07/29 02:03:19 UTC

[shardingsphere] branch master updated: Add proxy state and uptime metrics (#11555)

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 50d166c  Add proxy state and uptime metrics (#11555)
50d166c is described below

commit 50d166c9e49b17104c6f633c9a98ab9b7c69a908
Author: Dachuan J <46...@users.noreply.github.com>
AuthorDate: Thu Jul 29 10:02:41 2021 +0800

    Add proxy state and uptime metrics (#11555)
---
 .../metrics/api/constant/MethodNameConstant.java   |  4 --
 .../pom.xml                                        | 12 ++++
 .../prometheus/collector/BuildInfoCollector.java   | 19 ++++--
 .../prometheus/collector/ProxyInfoCollector.java   | 68 ++++++++++++++++++++++
 .../service/PrometheusPluginBootService.java       |  8 ++-
 .../proxy/frontend/ShardingSphereProxy.java        | 16 ++++-
 6 files changed, 117 insertions(+), 10 deletions(-)

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/constant/MethodNameConstant.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/constant/MethodNameConstant.java
index d359697..24662b2 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/constant/MethodNameConstant.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-api/src/main/java/org/apache/shardingsphere/agent/metrics/api/constant/MethodNameConstant.java
@@ -30,8 +30,6 @@ public final class MethodNameConstant {
     
     public static final String COMMAND_EXECUTOR_EXCEPTION = "processException";
     
-    public static final String SQL_ROUTER = "route";
-    
     public static final String CHANNEL_ACTIVE = "channelActive";
     
     public static final String CHANNEL_READ = "channelRead";
@@ -41,6 +39,4 @@ public final class MethodNameConstant {
     public static final String COMMIT = "commit";
     
     public static final String ROLL_BACK = "rollback";
-
-    public static final String DECORATE = "decorate";
 }
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
index af9ce2f..4f51edc 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/pom.xml
@@ -59,6 +59,18 @@
             <artifactId>HikariCP</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-proxy-backend</artifactId>
+            <version>${project.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-proxy-frontend-core</artifactId>
+            <version>${project.version}</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
     
     <build>
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
index ada3f8c..d69c6cf 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/BuildInfoCollector.java
@@ -19,26 +19,37 @@ package org.apache.shardingsphere.agent.metrics.prometheus.collector;
 
 import io.prometheus.client.Collector;
 import io.prometheus.client.GaugeMetricFamily;
-import java.util.ArrayList;
+import lombok.extern.slf4j.Slf4j;
+
 import java.util.Arrays;
+import java.util.LinkedList;
 import java.util.List;
 
 /**
  * Build info collector.
  */
+@Slf4j
 public final class BuildInfoCollector extends Collector {
     
     @Override
     public List<MetricFamilySamples> collect() {
-        List<MetricFamilySamples> result = new ArrayList<>(1);
+        List<MetricFamilySamples> result = new LinkedList<>();
         GaugeMetricFamily artifactInfo = new GaugeMetricFamily(
-                "jmx_exporter_build_info",
-                "A metric with a constant '1' value labeled with the version of the JMX exporter.",
+                "build_info",
+                "A metric with a constant '1' value labeled with the version of shardingsphere.",
                 Arrays.asList("version", "name"));
         Package pkg = getClass().getPackage();
         String version = pkg.getImplementationVersion();
         String name = pkg.getImplementationTitle();
         artifactInfo.addMetric(Arrays.asList(null != version ? version : "unknown", null != name ? name : "unknown"), 1L);
+        try {
+            pkg = Class.forName("org.apache.shardingsphere.proxy.Bootstrap").getPackage();
+            version = pkg.getImplementationVersion();
+            name = pkg.getImplementationTitle();
+            artifactInfo.addMetric(Arrays.asList(null != version ? version : "unknown", null != name ? name : "unknown"), 1L);
+        } catch (ClassNotFoundException ex) {
+            log.warn("No proxy class find");
+        }
         result.add(artifactInfo);
         return result;
     }
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
new file mode 100644
index 0000000..6cf096b
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/collector/ProxyInfoCollector.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.metrics.prometheus.collector;
+
+import io.prometheus.client.Collector;
+import io.prometheus.client.GaugeMetricFamily;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.frontend.ShardingSphereProxy;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Proxy info collector.
+ */
+public final class ProxyInfoCollector extends Collector {
+    
+    private static final String PROXY_STATE = "state";
+    
+    private static final String PROXY_UP_TIME = "uptime";
+    
+    @Override
+    public List<Collector.MetricFamilySamples> collect() {
+        List<MetricFamilySamples> result = new LinkedList<>();
+        if (!checkProxy()) {
+            return result;
+        }
+        GaugeMetricFamily proxyInfo = new GaugeMetricFamily(
+                "proxy_info",
+                "Proxy information.",
+                Arrays.asList("name"));
+        proxyInfo.addMetric(Arrays.asList(PROXY_STATE), ProxyContext.getInstance().getStateContext().getCurrentState().ordinal());
+        Date now = new Date();
+        if (0 >= ShardingSphereProxy.getStartTime()) {
+            proxyInfo.addMetric(Arrays.asList(PROXY_UP_TIME), 0);
+        } else {
+            proxyInfo.addMetric(Arrays.asList(PROXY_UP_TIME), now.getTime() - ShardingSphereProxy.getStartTime());
+        }
+        result.add(proxyInfo);
+        return result;
+    }
+    
+    private boolean checkProxy() {
+        try {
+            Class.forName("org.apache.shardingsphere.proxy.frontend.ShardingSphereProxy");
+        } catch (ClassNotFoundException ex) {
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
index cda28b8..ab8ab3d 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/main/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootService.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.agent.config.PluginConfiguration;
 import org.apache.shardingsphere.agent.exception.PluginConfigurationException;
 import org.apache.shardingsphere.agent.metrics.api.reporter.MetricsReporter;
 import org.apache.shardingsphere.agent.metrics.prometheus.collector.BuildInfoCollector;
+import org.apache.shardingsphere.agent.metrics.prometheus.collector.ProxyInfoCollector;
 import org.apache.shardingsphere.agent.metrics.prometheus.register.PrometheusMetricsRegister;
 import org.apache.shardingsphere.agent.spi.boot.PluginBootService;
 
@@ -66,6 +67,7 @@ public final class PrometheusPluginBootService implements PluginBootService {
     
     private void startServer(final PluginConfiguration configuration) {
         boolean enabled = Boolean.parseBoolean(configuration.getProps().getProperty("JVM_INFORMATION_COLLECTOR_ENABLED"));
+        registerDefault();
         registerJvm(enabled);
         int port = configuration.getPort();
         String host = configuration.getHost();
@@ -83,9 +85,13 @@ public final class PrometheusPluginBootService implements PluginBootService {
         }
     }
     
+    private void registerDefault() {
+        new ProxyInfoCollector().register();
+        new BuildInfoCollector().register();
+    }
+    
     private void registerJvm(final boolean enabled) {
         if (enabled) {
-            new BuildInfoCollector().register();
             DefaultExports.initialize();
         }
     }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
index cbdcf03..35907f1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
@@ -36,12 +36,16 @@ import org.apache.shardingsphere.proxy.backend.context.BackendExecutorContext;
 import org.apache.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer;
 import org.apache.shardingsphere.proxy.frontend.protocol.FrontDatabaseProtocolTypeFactory;
 
+import java.util.Date;
+
 /**
  * ShardingSphere-Proxy.
  */
 @Slf4j
 public final class ShardingSphereProxy {
     
+    private static long startTime;
+    
     private EventLoopGroup bossGroup;
     
     private EventLoopGroup workerGroup;
@@ -58,7 +62,8 @@ public final class ShardingSphereProxy {
             ServerBootstrap bootstrap = new ServerBootstrap();
             initServerBootstrap(bootstrap);
             ChannelFuture future = bootstrap.bind(port).sync();
-            log.info("ShardingSphere-Proxy start success.");
+            startTime = new Date().getTime();
+            log.info("ShardingSphere-Proxy start success");
             future.channel().closeFuture().sync();
         } finally {
             workerGroup.shutdownGracefully();
@@ -83,4 +88,13 @@ public final class ShardingSphereProxy {
                 .handler(new LoggingHandler(LogLevel.INFO))
                 .childHandler(new ServerHandlerInitializer(FrontDatabaseProtocolTypeFactory.getDatabaseType()));
     }
+    
+    /**
+     * Get proxy start time.
+     *
+     * @return proxy start time
+     */
+    public static Long getStartTime() {
+        return startTime;
+    }
 }