This is an automated email from the ASF dual-hosted git repository.

haiboduan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new aa61bc8  [type: bug] fix metrics build info bug (#2994)
aa61bc8 is described below

commit aa61bc89ce82f5d3ba4d8a509506ddf0be9f7432
Author: xiaoyu <[email protected]>
AuthorDate: Mon Mar 7 12:22:30 2022 +0800

    [type: bug] fix metrics build info bug (#2994)
---
 .../boot/PrometheusPluginBootService.java          | 13 +++--
 .../prometheus/collector/BuildInfoCollector.java   | 55 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git 
a/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/boot/PrometheusPluginBootService.java
 
b/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/boot/PrometheusPluginBootService.java
index a5b7fc4..7dc327b 100644
--- 
a/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/boot/PrometheusPluginBootService.java
+++ 
b/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/boot/PrometheusPluginBootService.java
@@ -20,11 +20,11 @@ package 
org.apache.shenyu.agent.plugin.metrics.prometheus.boot;
 import io.prometheus.client.CollectorRegistry;
 import io.prometheus.client.exporter.HTTPServer;
 import io.prometheus.client.hotspot.DefaultExports;
-import io.prometheus.jmx.BuildInfoCollector;
 import io.prometheus.jmx.JmxCollector;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.agent.api.config.AgentPluginConfig;
 import org.apache.shenyu.agent.api.spi.AgentPluginBootService;
+import 
org.apache.shenyu.agent.plugin.metrics.prometheus.collector.BuildInfoCollector;
 import org.apache.shenyu.spi.Join;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -76,13 +76,16 @@ public final class PrometheusPluginBootService implements 
AgentPluginBootService
             return;
         }
         new BuildInfoCollector().register();
-        boolean enabled = 
Boolean.parseBoolean(props.getProperty("jvm_enabled"));
-        if (enabled) {
-            DefaultExports.initialize();
+        String jvmEnabled = String.valueOf(props.getProperty("jvm_enabled"));
+        if (StringUtils.isNotEmpty(jvmEnabled)) {
+            boolean enabled = Boolean.parseBoolean(jvmEnabled);
+            if (enabled) {
+                DefaultExports.initialize();
+            }
         }
         try {
             String jmxConfig = String.valueOf(props.get("jmx_config"));
-            if (StringUtils.isNotEmpty(jmxConfig)) {
+            if (!"null".equals(jmxConfig) && 
StringUtils.isNotEmpty(jmxConfig)) {
                 new JmxCollector(jmxConfig).register();
             }
         } catch (MalformedObjectNameException e) {
diff --git 
a/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/collector/BuildInfoCollector.java
 
b/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/collector/BuildInfoCollector.java
new file mode 100644
index 0000000..a8db6e7
--- /dev/null
+++ 
b/shenyu-agent/shenyu-agent-plugins/shenyu-agent-plugin-metrics/shenyu-agent-plugin-metrics-prometheus/src/main/java/org/apache/shenyu/agent/plugin/metrics/prometheus/collector/BuildInfoCollector.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.shenyu.agent.plugin.metrics.prometheus.collector;
+
+import io.prometheus.client.Collector;
+import io.prometheus.client.GaugeMetricFamily;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Build info collector.
+ */
+public final class BuildInfoCollector extends Collector {
+    
+    private static final Logger LOG = 
LoggerFactory.getLogger(BuildInfoCollector.class);
+    
+    private static final String CLASS_NAME = 
"org.apache.shenyu.web.handler.ShenyuWebHandler";
+    
+    @Override
+    public List<MetricFamilySamples> collect() {
+        List<String> labels = new ArrayList<>();
+        labels.add("version");
+        labels.add("name");
+        GaugeMetricFamily gaugeMetricFamily = new 
GaugeMetricFamily("build_info", "build information", labels);
+        try {
+            Package proxyPkg = Class.forName(CLASS_NAME).getPackage();
+            final String proxyVersion = proxyPkg.getImplementationVersion();
+            final String proxyName = proxyPkg.getImplementationTitle();
+            gaugeMetricFamily.addMetric(Arrays.asList(null != proxyVersion ? 
proxyVersion : "unknown", null != proxyName ? proxyName : "unknown"), 1L);
+        } catch (ClassNotFoundException ex) {
+            LOG.error("No proxy class find :{}", CLASS_NAME);
+        }
+        return Collections.singletonList(gaugeMetricFamily);
+    }
+}

Reply via email to