You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/05/19 07:30:25 UTC

[dubbo] branch 3.2 updated: :bug: fix prometheus init failed when start (#12349)

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

albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new c22db81a8b :bug: fix prometheus init failed when start (#12349)
c22db81a8b is described below

commit c22db81a8bd26fb1a1cd0e41b04cb0bbe421cec5
Author: songxiaosheng <so...@elastic.link>
AuthorDate: Fri May 19 15:30:18 2023 +0800

    :bug: fix prometheus init failed when start (#12349)
    
    * :bug: fix prometheus init failed when start
    
    * :bug: fix prometheus init failed when start
    
    * :bug: fix prometheus init failed when start
---
 .../config/deploy/DefaultApplicationDeployer.java  | 25 ++++++++--------
 .../deploy/DefaultApplicationDeployerTest.java     | 33 ++++++++++++++++++++++
 .../metrics/collector/DefaultMetricsCollector.java | 14 ++++-----
 3 files changed, 54 insertions(+), 18 deletions(-)

diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
index ef6306eac9..0e8fc3dd86 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
@@ -38,6 +38,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.threadpool.manager.ExecutorRepository;
 import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
 import org.apache.dubbo.common.utils.ArrayUtils;
+import org.apache.dubbo.common.utils.ClassUtils;
 import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.config.ApplicationConfig;
@@ -373,19 +374,9 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
         Optional<MetricsConfig> configOptional = configManager.getMetrics();
 
         // TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use.
-        boolean importMetricsPrometheus;  // Use package references instead of config checks
-        try {
-            Class.forName("io.micrometer.prometheus.PrometheusConfig");
-            importMetricsPrometheus = true;
-        } catch (ClassNotFoundException e) {
-            importMetricsPrometheus = false;
-        }
-
-        if (!importMetricsPrometheus) {
-            //use old metrics
+        if (!isSupportPrometheus()) {
             return;
         }
-
         MetricsConfig metricsConfig = configOptional.orElse(new MetricsConfig(applicationModel));
         if (StringUtils.isBlank(metricsConfig.getProtocol())) {
             metricsConfig.setProtocol(PROTOCOL_PROMETHEUS);
@@ -399,6 +390,18 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
         applicationModel.getBeanFactory().registerBean(metricsReporter);
     }
 
+    public static boolean isSupportPrometheus() {
+        return isClassPresent("io.micrometer.prometheus.PrometheusConfig")
+            && isClassPresent("io.prometheus.client.exporter.BasicAuthHttpConnectionFactory")
+            && isClassPresent("io.prometheus.client.exporter.HttpConnectionFactory")
+            && isClassPresent("io.prometheus.client.exporter.PushGateway");
+    }
+
+
+    private static boolean isClassPresent(String className) {
+        return ClassUtils.isPresent(className, DefaultApplicationDeployer.class.getClassLoader());
+    }
+
 
     private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) {
         return isUsedRegistryAsCenter(registryConfig, registryConfig::getUseAsConfigCenter, "config",
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployerTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployerTest.java
new file mode 100644
index 0000000000..fab3cf577b
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployerTest.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.dubbo.config.deploy;
+
+import org.apache.dubbo.common.utils.Assert;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DefaultApplicationDeployerTest {
+
+    @Test
+    void isSupportPrometheus() {
+        boolean supportPrometheus = new DefaultApplicationDeployer(ApplicationModel.defaultModel()).isSupportPrometheus();
+        Assert.assertTrue(supportPrometheus,"DefaultApplicationDeployer.isSupportPrometheus() should return true");
+    }
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
index af3f7bcfe6..c4c3ef9ec0 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
@@ -71,7 +71,7 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
             protected void init(RtStatComposite rtStatComposite) {
                 super.init(rtStatComposite);
                 rtStatComposite.init(MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD),
-                        MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD));
+                    MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD));
             }
         });
         super.setEventMulticaster(new DefaultSubDispatcher(this));
@@ -146,17 +146,17 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
         public List<MetricSample> sample() {
             List<MetricSample> samples = new ArrayList<>();
             this.getCount(MetricsEvent.Type.APPLICATION_INFO).filter(e -> !e.isEmpty())
-                    .ifPresent(map -> map.forEach((k, v) ->
-                            samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
-                                    APPLICATION_METRIC_INFO.getDescription(),
-                                    k.getTags(), APPLICATION, v)))
-                    );
+                .ifPresent(map -> map.forEach((k, v) ->
+                    samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
+                        APPLICATION_METRIC_INFO.getDescription(),
+                        k.getTags(), APPLICATION, v)))
+                );
             return samples;
         }
 
         @Override
         protected void countConfigure(
-                MetricsCountSampleConfigurer<String, MetricsEvent.Type, ApplicationMetric> sampleConfigure) {
+            MetricsCountSampleConfigurer<String, MetricsEvent.Type, ApplicationMetric> sampleConfigure) {
             sampleConfigure.configureMetrics(configure -> new ApplicationMetric(applicationModel));
         }
     };