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/03/30 02:17:27 UTC

[dubbo] branch 3.2 updated: :bento: try change application metrics to counter (#11956)

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 547678e684 :bento: try change application metrics to counter (#11956)
547678e684 is described below

commit 547678e6848db8117d804d82e4e341eb13ed9cbe
Author: songxiaosheng <so...@elastic.link>
AuthorDate: Thu Mar 30 10:16:51 2023 +0800

    :bento: try change application metrics to counter (#11956)
    
    * :bento: try change application metrics to counter
    
    * :bento: try change application metrics to counter
    
    * 🍱 try change application metrics to counter
---
 .../metrics/model/sample/CounterMetricSample.java  | 42 ++++++++++++++++++++++
 .../metrics/collector/DefaultMetricsCollector.java |  9 ++---
 .../metrics/report/AbstractMetricsReporter.java    | 28 ++++++++++-----
 3 files changed, 67 insertions(+), 12 deletions(-)

diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java
new file mode 100644
index 0000000000..46ce0d50f9
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java
@@ -0,0 +1,42 @@
+/*
+ * 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.metrics.model.sample;
+
+import org.apache.dubbo.metrics.model.MetricsCategory;
+
+import java.util.Map;
+
+public class CounterMetricSample<T extends Number>  extends MetricSample {
+
+    private final T value;
+
+    public CounterMetricSample(String name, String description, Map<String, String> tags,
+                               MetricsCategory category, T value ) {
+        super(name, description, tags, Type.COUNTER, category);
+        this.value = value;
+    }
+
+    public CounterMetricSample(String name, String description, Map<String, String> tags,   MetricsCategory category,
+                               String baseUnit, T value) {
+        super(name, description, tags, Type.COUNTER, category, baseUnit);
+        this.value = value;
+    }
+
+    public T getValue() {
+        return value;
+    }
+}
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 9123f3936d..baedb35db2 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
@@ -25,12 +25,11 @@ import org.apache.dubbo.metrics.event.MetricsEvent;
 import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
 import org.apache.dubbo.metrics.listener.MetricsListener;
 import org.apache.dubbo.metrics.model.ApplicationMetric;
-import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
 import org.apache.dubbo.metrics.model.sample.MetricSample;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
 import static org.apache.dubbo.metrics.model.MetricsCategory.APPLICATION;
 import static org.apache.dubbo.metrics.model.MetricsKey.APPLICATION_METRIC_INFO;
 
@@ -105,7 +104,7 @@ public class DefaultMetricsCollector implements MetricsCollector {
         applicationSampler.inc(applicationName, MetricsEvent.Type.APPLICATION_INFO);
     }
 
-    public void registryDefaultSample(){
+    public void registryDefaultSample() {
         this.threadPoolSampler.registryDefaultSampleThreadPoolExecutor();
     }
 
@@ -129,7 +128,9 @@ public class DefaultMetricsCollector implements MetricsCollector {
             List<MetricSample> samples = new ArrayList<>();
             this.getCount(MetricsEvent.Type.APPLICATION_INFO).filter(e -> !e.isEmpty())
                 .ifPresent(map -> map.forEach((k, v) ->
-                    samples.add(new GaugeMetricSample<>(APPLICATION_METRIC_INFO, k.getTags(), APPLICATION, v, AtomicLong::get)))
+                    samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
+                        APPLICATION_METRIC_INFO.getDescription(),
+                        k.getTags(), APPLICATION, v)))
                 );
             return samples;
         }
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java
index 78215624e9..5f93e8cda2 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsReporter.java
@@ -17,6 +17,7 @@
 
 package org.apache.dubbo.metrics.report;
 
+import io.micrometer.core.instrument.FunctionCounter;
 import io.micrometer.core.instrument.binder.MeterBinder;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
@@ -29,6 +30,7 @@ import org.apache.dubbo.common.utils.NamedThreadFactory;
 import org.apache.dubbo.metrics.collector.AggregateMetricsCollector;
 import org.apache.dubbo.metrics.collector.MetricsCollector;
 import org.apache.dubbo.metrics.collector.HistogramMetricsCollector;
+import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
 import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
 import org.apache.dubbo.metrics.model.sample.MetricSample;
 import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -157,19 +159,17 @@ public abstract class AbstractMetricsReporter implements MetricsReporter {
                     switch (sample.getType()) {
                         case GAUGE:
                             GaugeMetricSample gaugeSample = (GaugeMetricSample) sample;
-                            List<Tag> tags = new ArrayList<>();
-                            gaugeSample.getTags().forEach((k, v) -> {
-                                if (v == null) {
-                                    v = "";
-                                }
-
-                                tags.add(Tag.of(k, v));
-                            });
+                            List<Tag> tags = getTags(gaugeSample);
 
                             Gauge.builder(gaugeSample.getName(), gaugeSample.getValue(), gaugeSample.getApply())
                                 .description(gaugeSample.getDescription()).tags(tags).register(compositeRegistry);
                             break;
                         case COUNTER:
+                            CounterMetricSample counterMetricSample = (CounterMetricSample) sample;
+                            FunctionCounter.builder(counterMetricSample.getName(),  counterMetricSample.getValue(),
+                                    Number::doubleValue).description(counterMetricSample.getDescription())
+                                .tags(getTags(counterMetricSample))
+                                .register(compositeRegistry);
                         case TIMER:
                         case LONG_TASK_TIMER:
                         case DISTRIBUTION_SUMMARY:
@@ -185,6 +185,18 @@ public abstract class AbstractMetricsReporter implements MetricsReporter {
         });
     }
 
+    private static List<Tag> getTags(MetricSample gaugeSample) {
+        List<Tag> tags = new ArrayList<>();
+        gaugeSample.getTags().forEach((k, v) -> {
+            if (v == null) {
+                v = "";
+            }
+
+            tags.add(Tag.of(k, v));
+        });
+        return tags;
+    }
+
     private void registerDubboShutdownHook() {
         applicationModel.getBeanFactory().getBean(ShutdownHookCallbacks.class).addCallback(this::destroy);
     }