You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "AlbumenJ (via GitHub)" <gi...@apache.org> on 2023/03/17 05:44:40 UTC

[GitHub] [dubbo] AlbumenJ commented on a diff in pull request #11806: add nop for prometheus metrics

AlbumenJ commented on code in PR #11806:
URL: https://github.com/apache/dubbo/pull/11806#discussion_r1139781385


##########
dubbo-metrics/dubbo-metrics-prometheus/src/main/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsReporterFactory.java:
##########
@@ -18,21 +18,48 @@
 package org.apache.dubbo.metrics.prometheus;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.metrics.report.MetricsReporter;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.metrics.report.AbstractMetricsReporterFactory;
+import org.apache.dubbo.metrics.report.MetricsReporter;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
+
 /**
  * MetricsReporterFactory to create PrometheusMetricsReporter.
  */
 public class PrometheusMetricsReporterFactory extends AbstractMetricsReporterFactory {
 
+    private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(PrometheusMetricsReporterFactory.class);
+
     public PrometheusMetricsReporterFactory(ApplicationModel applicationModel) {
         super(applicationModel);
     }
 
     @Override
     public MetricsReporter createMetricsReporter(URL url) {
-        return new PrometheusMetricsReporter(url, getApplicationModel());
+        try {
+            return new PrometheusMetricsReporter(url, getApplicationModel());
+        } catch (NoClassDefFoundError ncde) {
+            String msg = ncde.getMessage();
+            if (messageContainsPrometheusMetricsReporter(msg)) {
+                logger.error(INTERNAL_ERROR, "", "", "Failed to load class \"org.apache.dubbo.metrics.prometheus.PrometheusMetricsReporter\".", ncde);
+                logger.error(INTERNAL_ERROR, "", "", "Defaulting to no-operation (NOP) metricsReporter implementation", ncde);
+                logger.error(INTERNAL_ERROR, "", "", "Introduce the micrometer-core package to use the ability of metrics", ncde);
+                return new NopPrometheusMetricsReporter();
+            } else {
+                logger.error(INTERNAL_ERROR, "", "", "Failed to instantiate PrometheusMetricsReporter", ncde);
+                throw ncde;
+            }
+        }
+    }
+
+    private static boolean messageContainsPrometheusMetricsReporter(String msg) {

Review Comment:
   ```suggestion
       private static boolean dependenciesNotFound(String msg) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org