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 dc74f131a7 Refactor GaugeMetricSample (#11692)
dc74f131a7 is described below
commit dc74f131a73eb2607d3d473496decac36acadb1a
Author: Mengyang Tang <[email protected]>
AuthorDate: Wed Mar 1 09:23:39 2023 +0800
Refactor GaugeMetricSample (#11692)
---
.../metrics/model/sample/GaugeMetricSample.java | 40 +++++++++------
.../collector/AggregateMetricsCollector.java | 10 ++--
.../metrics/collector/DefaultMetricsCollector.java | 8 ++-
.../collector/sample/MethodMetricsSampler.java | 26 +++++++---
.../sample/SimpleMetricsCountSampler.java | 2 +-
.../collector/sample/ThreadPoolMetricsSampler.java | 25 +++++-----
.../metrics/report/AbstractMetricsReporter.java | 32 ++++++------
.../metrics/service/DefaultMetricsService.java | 6 ++-
.../collector/AggregateMetricsCollectorTest.java | 28 +++++------
.../dubbo/metrics/filter/MetricsFilterTest.java | 10 ++--
.../collector/DefaultMetricsCollectorTest.java | 32 ++++++------
.../model/sample/GaugeMetricSampleTest.java | 18 ++++---
.../dubbo/metrics/sampler/CountSamplerTest.java | 57 ++++++++++------------
.../collector/stat/MetadataStatComposite.java | 7 ++-
.../collector/MetadataMetricsCollectorTest.java | 15 +++---
.../collector/stat/RegistryStatComposite.java | 10 ++--
.../collector/RegistryMetricsCollectorTest.java | 7 ++-
.../collector/RegistryMetricsSampleTest.java | 6 +--
.../model/sample/GaugeMetricSampleTest.java | 16 +++---
19 files changed, 195 insertions(+), 160 deletions(-)
diff --git
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
index 9277ab1d1e..63d4c5c70d 100644
---
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
+++
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
@@ -21,35 +21,45 @@ import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.MetricsKey;
import java.util.Map;
-import java.util.function.Supplier;
+import java.util.Objects;
+import java.util.function.ToDoubleFunction;
/**
* GaugeMetricSample.
*/
-public class GaugeMetricSample extends MetricSample {
+public class GaugeMetricSample<T> extends MetricSample {
- private Supplier<Number> supplier;
+ private final T value;
- public GaugeMetricSample(MetricsKey metricsKey, Map<String, String> tags,
MetricsCategory category, Supplier<Number> supplier) {
- super(metricsKey.getName(), metricsKey.getDescription(), tags,
Type.GAUGE, category);
- this.supplier = supplier;
+ private final ToDoubleFunction<T> apply;
+
+ public GaugeMetricSample(MetricsKey metricsKey, Map<String, String> tags,
MetricsCategory category, T value, ToDoubleFunction<T> apply) {
+ this(metricsKey.getName(), metricsKey.getDescription(), tags,
category, null, value, apply);
}
- public GaugeMetricSample(String name, String description, Map<String,
String> tags, MetricsCategory category, Supplier<Number> supplier) {
- super(name, description, tags, Type.GAUGE, category);
- this.supplier = supplier;
+ public GaugeMetricSample(String name, String description, Map<String,
String> tags, MetricsCategory category, T value, ToDoubleFunction<T> apply) {
+ this(name, description, tags, category, null, value, apply);
}
- public GaugeMetricSample(String name, String description, Map<String,
String> tags, MetricsCategory category, String baseUnit, Supplier<Number>
supplier) {
+ public GaugeMetricSample(String name, String description, Map<String,
String> tags, MetricsCategory category, String baseUnit, T value,
ToDoubleFunction<T> apply) {
super(name, description, tags, Type.GAUGE, category, baseUnit);
- this.supplier = supplier;
+ this.value = Objects.requireNonNull(value, "The GaugeMetricSample
value cannot be null");
+ this.apply = Objects.requireNonNull(apply, "The GaugeMetricSample
apply cannot be null");
+ }
+
+ public T getValue() {
+ return this.value;
+ }
+
+ public ToDoubleFunction<T> getApply() {
+ return this.apply;
}
- public Supplier<Number> getSupplier() {
- return supplier;
+ public long applyAsLong() {
+ return (long) getApply().applyAsDouble(getValue());
}
- public void setSupplier(Supplier<Number> supplier) {
- this.supplier = supplier;
+ public double applyAsDouble() {
+ return getApply().applyAsDouble(getValue());
}
}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
index 1651c87dc3..c5e0191d7b 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
@@ -32,11 +32,13 @@ import org.apache.dubbo.metrics.model.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
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.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+
import static org.apache.dubbo.metrics.model.MetricsCategory.QPS;
import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
@@ -134,18 +136,18 @@ public class AggregateMetricsCollector implements
MetricsCollector, MetricsListe
private void collectMethod(List<MetricSample> list, MetricsEvent.Type
eventType, MetricsKey metricsKey) {
ConcurrentHashMap<MethodMetric, TimeWindowCounter> windowCounter =
methodTypeCounter.get(eventType);
if (windowCounter != null) {
- windowCounter.forEach((k, v) -> list.add(new
GaugeMetricSample(metricsKey.formatName(k.getSide()), k.getTags(), REQUESTS,
v::get)));
+ windowCounter.forEach((k, v) -> list.add(new
GaugeMetricSample<>(metricsKey.formatName(k.getSide()), k.getTags(), REQUESTS,
v, TimeWindowCounter::get)));
}
}
private void collectQPS(List<MetricSample> list) {
- qps.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.METRIC_QPS.formatName(k.getSide()), k.getTags(),
QPS, () -> v.get() / v.bucketLivedSeconds())));
+ qps.forEach((k, v) -> list.add(new
GaugeMetricSample<>(MetricsKey.METRIC_QPS.formatName(k.getSide()), k.getTags(),
QPS, v, value -> value.get() / value.bucketLivedSeconds())));
}
private void collectRT(List<MetricSample> list) {
rt.forEach((k, v) -> {
- list.add(new
GaugeMetricSample(MetricsKey.METRIC_RT_P99.formatName(k.getSide()),
k.getTags(), RT, () -> v.quantile(0.99)));
- list.add(new
GaugeMetricSample(MetricsKey.METRIC_RT_P95.formatName(k.getSide()),
k.getTags(), RT, () -> v.quantile(0.95)));
+ list.add(new
GaugeMetricSample<>(MetricsKey.METRIC_RT_P99.formatName(k.getSide()),
k.getTags(), RT, v, value -> value.quantile(0.99)));
+ list.add(new
GaugeMetricSample<>(MetricsKey.METRIC_RT_P95.formatName(k.getSide()),
k.getTags(), RT, v, value -> value.quantile(0.95)));
});
}
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 87bd5ea53e..ae3d378c88 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
@@ -29,8 +29,11 @@ import org.apache.dubbo.metrics.model.ApplicationMetric;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
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;
@@ -111,8 +114,9 @@ public class DefaultMetricsCollector implements
MetricsCollector {
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
GaugeMetricSample(APPLICATION_METRIC_INFO, k.getTags(),
- APPLICATION, v::get))));
+ .ifPresent(map -> map.forEach((k, v) ->
+ samples.add(new
GaugeMetricSample<>(APPLICATION_METRIC_INFO, k.getTags(), APPLICATION, v,
AtomicLong::get)))
+ );
return samples;
}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
index 3144a3c241..186a691f90 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
@@ -28,9 +28,11 @@ import org.apache.dubbo.metrics.model.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.Invocation;
+
import java.util.ArrayList;
import java.util.List;
-import java.util.function.Supplier;
+import java.util.function.ToDoubleFunction;
+
import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
@@ -62,7 +64,7 @@ public class MethodMetricsSampler extends
SimpleMetricsCountSampler<Invocation,
collect(metricSamples);
metricSamples.addAll(
- this.collectRT((key, metric, count) -> getGaugeMetricSample(key,
metric, RT, () -> count)));
+ this.collectRT((key, metric, count) -> getGaugeMetricSample(key,
metric, RT, count, Number::longValue)));
return metricSamples;
}
@@ -78,19 +80,27 @@ public class MethodMetricsSampler extends
SimpleMetricsCountSampler<Invocation,
count(list, MetricsEvent.Type.TOTAL_FAILED,
MetricsKey.METRIC_REQUESTS_TOTAL_FAILED);
count(list, MetricsEvent.Type.NETWORK_EXCEPTION,
MetricsKey.METRIC_REQUESTS_NETWORK_FAILED);
count(list, MetricsEvent.Type.SERVICE_UNAVAILABLE,
MetricsKey.METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED);
-
count(list,MetricsEvent.Type.CODEC_EXCEPTION,MetricsKey.METRIC_REQUESTS_CODEC_FAILED);
+ count(list, MetricsEvent.Type.CODEC_EXCEPTION,
MetricsKey.METRIC_REQUESTS_CODEC_FAILED);
}
- private GaugeMetricSample getGaugeMetricSample(MetricsKey metricsKey,
MethodMetric methodMetric,
- MetricsCategory
metricsCategory, Supplier<Number> get) {
- return new
GaugeMetricSample(metricsKey.getNameByType(methodMetric.getSide()),
metricsKey.getDescription(),
- methodMetric.getTags(), metricsCategory, get);
+ private GaugeMetricSample<Number> getGaugeMetricSample(MetricsKey
metricsKey,
+ MethodMetric
methodMetric,
+ MetricsCategory
metricsCategory,
+ Number value,
+
ToDoubleFunction<Number> apply) {
+ return new GaugeMetricSample<>(
+ metricsKey.getNameByType(methodMetric.getSide()),
+ metricsKey.getDescription(),
+ methodMetric.getTags(),
+ metricsCategory,
+ value,
+ apply);
}
private <T extends Metric> void count(List<MetricSample> list,
MetricsEvent.Type eventType, MetricsKey metricsKey) {
getCount(eventType).filter(e -> !e.isEmpty())
.ifPresent(map -> map.forEach((k, v) ->
- list.add(getGaugeMetricSample(metricsKey, k, REQUESTS,
v::get))));
+ list.add(getGaugeMetricSample(metricsKey, k, REQUESTS, v,
value -> v.get()))));
}
}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
index 5e1ddded2a..7580cee6c3 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
@@ -44,7 +44,7 @@ public abstract class SimpleMetricsCountSampler<S, K, M
extends Metric>
implements MetricsCountSampler<S, K, M> {
private final ConcurrentMap<M, AtomicLong> EMPTY_COUNT = new
ConcurrentHashMap<>();
- private Map<K, ConcurrentMap<M, AtomicLong>> metricCounter = new
ConcurrentHashMap<>();
+ private final Map<K, ConcurrentMap<M, AtomicLong>> metricCounter = new
ConcurrentHashMap<>();
// lastRT, totalRT, rtCount, avgRT share a container, can utilize the
system cache line
private final ConcurrentMap<M, AtomicLongArray> rtSample = new
ConcurrentHashMap<>();
private final ConcurrentMap<M, LongAccumulator> minRT = new
ConcurrentHashMap<>();
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java
index 6198f76261..7495b5a884 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java
@@ -15,6 +15,7 @@
* limitations under the License.
*/
package org.apache.dubbo.metrics.collector.sample;
+
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
@@ -23,6 +24,7 @@ import org.apache.dubbo.metrics.model.MetricsKey;
import org.apache.dubbo.metrics.model.ThreadPoolMetric;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -31,6 +33,7 @@ import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
+
import static
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION;
import static org.apache.dubbo.metrics.model.MetricsCategory.THREAD_POOL;
@@ -38,9 +41,9 @@ public class ThreadPoolMetricsSampler implements
MetricsSampler {
private final ErrorTypeAwareLogger logger =
LoggerFactory.getErrorTypeAwareLogger(ThreadPoolMetricsSampler.class);
- private DefaultMetricsCollector collector;
+ private final DefaultMetricsCollector collector;
private FrameworkExecutorRepository frameworkExecutorRepository;
- private Map<String, ThreadPoolExecutor> sampleThreadPoolExecutor = new
ConcurrentHashMap<>();
+ private final Map<String, ThreadPoolExecutor> sampleThreadPoolExecutor =
new ConcurrentHashMap<>();
public ThreadPoolMetricsSampler(DefaultMetricsCollector collector) {
this.collector = collector;
@@ -57,23 +60,23 @@ public class ThreadPoolMetricsSampler implements
MetricsSampler {
public List<MetricSample> sample() {
List<MetricSample> metricSamples = new ArrayList<>();
- sampleThreadPoolExecutor.forEach((name, executor)->{
- metricSamples.addAll(createMetricsSample(name,executor));
+ sampleThreadPoolExecutor.forEach((name, executor) -> {
+ metricSamples.addAll(createMetricsSample(name, executor));
});
return metricSamples;
}
- private List<MetricSample> createMetricsSample(String
name,ThreadPoolExecutor executor) {
+ private List<MetricSample> createMetricsSample(String name,
ThreadPoolExecutor executor) {
List<MetricSample> list = new ArrayList<>();
ThreadPoolMetric poolMetrics = new
ThreadPoolMetric(collector.getApplicationName(), name, executor);
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_CORE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getCorePoolSize));
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_LARGEST_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getLargestPoolSize));
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_MAX_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getMaximumPoolSize));
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_ACTIVE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getActiveCount));
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_THREAD_COUNT,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getPoolSize));
- list.add(new GaugeMetricSample(MetricsKey.THREAD_POOL_QUEUE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics::getQueueSize));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_CORE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getCorePoolSize));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_LARGEST_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getLargestPoolSize));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_MAX_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getMaximumPoolSize));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_ACTIVE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getActiveCount));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_THREAD_COUNT,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getPoolSize));
+ list.add(new GaugeMetricSample<>(MetricsKey.THREAD_POOL_QUEUE_SIZE,
poolMetrics.getTags(), THREAD_POOL, poolMetrics,
ThreadPoolMetric::getQueueSize));
return list;
}
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 dccc46e2cc..e88bd0cdc4 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,17 +17,6 @@
package org.apache.dubbo.metrics.report;
-import io.micrometer.core.instrument.Gauge;
-import io.micrometer.core.instrument.MeterRegistry;
-import io.micrometer.core.instrument.Tag;
-import io.micrometer.core.instrument.Tags;
-import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
-import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
-import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
-import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
-import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
-import io.micrometer.core.instrument.binder.system.UptimeMetrics;
-import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.constants.MetricsConstants;
@@ -41,6 +30,18 @@ import
org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
+import io.micrometer.core.instrument.Gauge;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.Tag;
+import io.micrometer.core.instrument.Tags;
+import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
+import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
+import io.micrometer.core.instrument.binder.system.UptimeMetrics;
+import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -62,6 +63,7 @@ public abstract class AbstractMetricsReporter implements
MetricsReporter {
private final AtomicBoolean initialized = new AtomicBoolean(false);
protected final URL url;
+ @SuppressWarnings("rawtypes")
protected final List<MetricsCollector> collectors = new ArrayList<>();
public static final CompositeMeterRegistry compositeRegistry = new
CompositeMeterRegistry();
@@ -120,6 +122,7 @@ public abstract class AbstractMetricsReporter implements
MetricsReporter {
}
}
+ @SuppressWarnings("rawtypes")
private void initCollectors() {
ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
beanFactory.getOrRegisterBean(AggregateMetricsCollector.class);
@@ -130,11 +133,10 @@ public abstract class AbstractMetricsReporter implements
MetricsReporter {
private void scheduleMetricsCollectorSyncJob() {
NamedThreadFactory threadFactory = new
NamedThreadFactory("metrics-collector-sync-job", true);
collectorSyncJobExecutor = Executors.newScheduledThreadPool(1,
threadFactory);
- collectorSyncJobExecutor.scheduleWithFixedDelay(() -> {
- refreshData();
- }, DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD,
TimeUnit.SECONDS);
+ collectorSyncJobExecutor.scheduleWithFixedDelay(this::refreshData,
DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD, TimeUnit.SECONDS);
}
+ @SuppressWarnings({"unchecked", "rawtypes"})
public void refreshData() {
collectors.forEach(collector -> {
List<MetricSample> samples = collector.collect();
@@ -152,7 +154,7 @@ public abstract class AbstractMetricsReporter implements
MetricsReporter {
tags.add(Tag.of(k, v));
});
- Gauge.builder(gaugeSample.getName(),
gaugeSample.getSupplier())
+ Gauge.builder(gaugeSample.getName(),
gaugeSample.getValue(), gaugeSample.getApply())
.description(gaugeSample.getDescription()).tags(tags).register(compositeRegistry);
break;
case COUNTER:
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/service/DefaultMetricsService.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/service/DefaultMetricsService.java
index 3aedcfc5f3..eeb07827ca 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/service/DefaultMetricsService.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/service/DefaultMetricsService.java
@@ -33,6 +33,7 @@ import java.util.Map;
*/
public class DefaultMetricsService implements MetricsService {
+ @SuppressWarnings("rawtypes")
protected final List<MetricsCollector> collectors = new ArrayList<>();
public DefaultMetricsService(ApplicationModel applicationModel) {
@@ -52,7 +53,7 @@ public class DefaultMetricsService implements MetricsService {
@Override
public Map<MetricsCategory, List<MetricsEntity>>
getMetricsByCategories(String serviceUniqueName, String methodName, Class<?>[]
parameterTypes, List<MetricsCategory> categories) {
Map<MetricsCategory, List<MetricsEntity>> result = new HashMap<>();
- for (MetricsCollector collector : collectors) {
+ for (MetricsCollector<?> collector : collectors) {
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
if (categories.contains(sample.getCategory())) {
@@ -65,6 +66,7 @@ public class DefaultMetricsService implements MetricsService {
return result;
}
+ @SuppressWarnings({"unchecked", "rawtypes"})
private MetricsEntity sampleToEntity(MetricSample sample) {
MetricsEntity entity = new MetricsEntity();
@@ -74,7 +76,7 @@ public class DefaultMetricsService implements MetricsService {
switch (sample.getType()) {
case GAUGE:
GaugeMetricSample gaugeSample = (GaugeMetricSample) sample;
- entity.setValue(gaugeSample.getSupplier().get());
+
entity.setValue(gaugeSample.getApply().applyAsDouble(gaugeSample.getValue()));
break;
case COUNTER:
case LONG_TASK_TIMER:
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
index bba5f0a1ee..c4232f0039 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
@@ -31,6 +31,7 @@ import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
+
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -105,13 +106,13 @@ class AggregateMetricsCollectorTest {
defaultCollector.setApplicationName(applicationName);
MethodMetricsSampler methodMetricsCountSampler =
defaultCollector.getMethodSampler();
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.TOTAL);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.SUCCEED);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.UNKNOWN_FAILED);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.BUSINESS_FAILED);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.NETWORK_EXCEPTION);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.SERVICE_UNAVAILABLE);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.CODEC_EXCEPTION);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.TOTAL);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.SUCCEED);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.UNKNOWN_FAILED);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.BUSINESS_FAILED);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.NETWORK_EXCEPTION);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.SERVICE_UNAVAILABLE);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.CODEC_EXCEPTION);
List<MetricSample> samples = collector.collect();
@@ -125,10 +126,9 @@ class AggregateMetricsCollectorTest {
}
samples = collector.collect();
- Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_AGG.getNameByType(side)),
1L);
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED_AGG.getNameByType(side)),
1L);
@@ -162,10 +162,8 @@ class AggregateMetricsCollectorTest {
Assertions.assertEquals(tags.get(TAG_VERSION_KEY), version);
}
- Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P99.getNameByType(side)));
Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P95.getNameByType(side)));
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java
index 11029fef55..41b92f1bd9 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java
@@ -187,10 +187,10 @@ class MetricsFilterTest {
Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_TOTAL_FAILED.getNameByType(side)));
MetricSample timeoutSample =
metricsMap.get(MetricsKey.METRIC_REQUESTS_TIMEOUT.getNameByType(side));
- Assertions.assertSame(((GaugeMetricSample)
timeoutSample).getSupplier().get().longValue(), count);
+ Assertions.assertSame(((GaugeMetricSample)
timeoutSample).applyAsLong(), count);
GaugeMetricSample failedSample = (GaugeMetricSample)
metricsMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_FAILED.getNameByType(side));
- Assertions.assertSame(failedSample.getSupplier().get().longValue(),
count);
+ Assertions.assertSame(failedSample.applyAsLong(), count);
}
@Test
@@ -215,7 +215,7 @@ class MetricsFilterTest {
MetricSample sample =
metricsMap.get(MetricsKey.METRIC_REQUESTS_LIMIT.getNameByType(side));
- Assertions.assertSame(((GaugeMetricSample)
sample).getSupplier().get().longValue(), count);
+ Assertions.assertSame(((GaugeMetricSample) sample).applyAsLong(),
count);
}
@Test
@@ -297,7 +297,7 @@ class MetricsFilterTest {
MetricSample sample = metricsMap.get(metricsKey.getName());
- Assertions.assertSame(((GaugeMetricSample)
sample).getSupplier().get().longValue(), count);
+ Assertions.assertSame(((GaugeMetricSample) sample).applyAsLong(),
count);
teardown();
}
@@ -322,7 +322,7 @@ class MetricsFilterTest {
MetricSample sample = metricsMap.get(metricsKey.getName());
- Assertions.assertSame(((GaugeMetricSample)
sample).getSupplier().get().longValue(), count);
+ Assertions.assertSame(((GaugeMetricSample) sample).applyAsLong(),
count);
Assertions.assertTrue(metricsMap.containsKey(metricsKey.getName()));
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java
index 1dad4e792d..7a1d238b3f 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java
@@ -34,6 +34,7 @@ import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
+
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -41,7 +42,6 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
-import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
@@ -83,7 +83,7 @@ class DefaultMetricsCollectorTest {
side = CommonConstants.CONSUMER;
invocation.setInvoker(new TestMetricsInvoker(side));
-
RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side="+side));
+
RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side="
+ side));
}
@@ -93,6 +93,7 @@ class DefaultMetricsCollectorTest {
}
@Test
+ @SuppressWarnings("rawtypes")
void testRequestsMetrics() {
DefaultMetricsCollector collector = new DefaultMetricsCollector();
collector.setCollectEnabled(true);
@@ -100,31 +101,28 @@ class DefaultMetricsCollectorTest {
MethodMetricsSampler methodMetricsCountSampler =
collector.getMethodSampler();
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.TOTAL);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.PROCESSING);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.SUCCEED);
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.UNKNOWN_FAILED);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.TOTAL);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.PROCESSING);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.SUCCEED);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.UNKNOWN_FAILED);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
Assertions.assertTrue(sample instanceof GaugeMetricSample);
GaugeMetricSample gaugeSample = (GaugeMetricSample) sample;
Map<String, String> tags = gaugeSample.getTags();
- Supplier<Number> supplier = gaugeSample.getSupplier();
Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY),
interfaceName);
Assertions.assertEquals(tags.get(TAG_METHOD_KEY), methodName);
Assertions.assertEquals(tags.get(TAG_GROUP_KEY), group);
Assertions.assertEquals(tags.get(TAG_VERSION_KEY), version);
- Assertions.assertEquals(supplier.get().longValue(), 1);
+ Assertions.assertEquals(gaugeSample.applyAsLong(), 1);
}
- methodMetricsCountSampler.dec(invocation,MetricsEvent.Type.PROCESSING);
+ methodMetricsCountSampler.dec(invocation,
MetricsEvent.Type.PROCESSING);
samples = collector.collect();
- Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+
+ Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_PROCESSING.getNameByType(side)),
0L);
}
@@ -151,10 +149,8 @@ class DefaultMetricsCollectorTest {
Assertions.assertEquals(tags.get(TAG_VERSION_KEY), version);
}
- Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_LAST.getNameByType(side)),
0L);
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_MIN.getNameByType(side)),
0L);
@@ -173,7 +169,7 @@ class DefaultMetricsCollectorTest {
collector.addListener(mockListener);
collector.setApplicationName(applicationModel.getApplicationName());
-
methodMetricsCountSampler.incOnEvent(invocation,MetricsEvent.Type.TOTAL);
+ methodMetricsCountSampler.incOnEvent(invocation,
MetricsEvent.Type.TOTAL);
Assertions.assertNotNull(mockListener.getCurEvent());
Assertions.assertTrue(mockListener.getCurEvent() instanceof
RequestEvent);
Assertions.assertEquals(((RequestEvent)
mockListener.getCurEvent()).getType(), MetricsEvent.Type.TOTAL);
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/sample/GaugeMetricSampleTest.java
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/sample/GaugeMetricSampleTest.java
index 42aa87e8a1..d43e969370 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/sample/GaugeMetricSampleTest.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/sample/GaugeMetricSampleTest.java
@@ -20,13 +20,15 @@ package org.apache.dubbo.metrics.metrics.model.sample;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
+
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
-import java.util.function.Supplier;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.ToDoubleFunction;
class GaugeMetricSampleTest {
@@ -35,7 +37,8 @@ class GaugeMetricSampleTest {
private static Map<String, String> tags;
private static MetricsCategory category;
private static String baseUnit;
- private static Supplier<Number> supplier;
+ private static AtomicLong value;
+ private static ToDoubleFunction<AtomicLong> apply;
@BeforeAll
public static void setup() {
@@ -44,20 +47,21 @@ class GaugeMetricSampleTest {
tags = new HashMap<>();
category = MetricsCategory.REQUESTS;
baseUnit = "byte";
- supplier = () -> 1;
+ value = new AtomicLong(1);
+ apply = AtomicLong::longValue;
}
@Test
void test() {
- GaugeMetricSample sample = new GaugeMetricSample(name, description,
tags, category, baseUnit, supplier);
+ GaugeMetricSample<?> sample = new GaugeMetricSample<>(name,
description, tags, category, baseUnit, value, apply);
Assertions.assertEquals(sample.getName(), name);
Assertions.assertEquals(sample.getDescription(), description);
Assertions.assertEquals(sample.getTags(), tags);
Assertions.assertEquals(sample.getType(), MetricSample.Type.GAUGE);
Assertions.assertEquals(sample.getCategory(), category);
Assertions.assertEquals(sample.getBaseUnit(), baseUnit);
- Assertions.assertEquals(sample.getSupplier().get(), 1);
- sample.setSupplier(() -> 2);
- Assertions.assertEquals(sample.getSupplier().get(), 2);
+ Assertions.assertEquals(1, sample.applyAsLong());
+ value.set(2);
+ Assertions.assertEquals(2, sample.applyAsLong());
}
}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/CountSamplerTest.java
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/CountSamplerTest.java
index 98cab8f0d6..b1d701c903 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/CountSamplerTest.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/CountSamplerTest.java
@@ -25,6 +25,7 @@ import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
+
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
@@ -53,83 +54,78 @@ public class CountSamplerTest {
String applicationName = "test";
sampler.addRT(applicationName, RTType.METHOD_REQUEST, 2L);
+ @SuppressWarnings("rawtypes")
Map<String, GaugeMetricSample> collect =
getCollect(RTType.METHOD_REQUEST);
Assertions.assertNotNull(collect);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_LAST.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_LAST.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_LAST.getName()).applyAsLong() == 2);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MIN.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MIN.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_MIN.getName()).applyAsLong() == 2);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MAX.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MAX.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_MAX.getName()).applyAsLong() == 2);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_AVG.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_AVG.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_AVG.getName()).applyAsLong() == 2);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_SUM.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_SUM.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_SUM.getName()).applyAsLong() == 2);
sampler.addRT(applicationName, RTType.METHOD_REQUEST, 1L);
collect = getCollect(RTType.METHOD_REQUEST);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_LAST.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_LAST.getName()).getSupplier().get().longValue() == 1);
+ MetricsKey.METRIC_RT_LAST.getName()).applyAsLong() == 1);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MIN.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MIN.getName()).getSupplier().get().longValue() == 1);
+ MetricsKey.METRIC_RT_MIN.getName()).applyAsLong() == 1);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MAX.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MAX.getName()).getSupplier().get().longValue() == 2);
+ MetricsKey.METRIC_RT_MAX.getName()).applyAsLong() == 2);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_AVG.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_AVG.getName()).getSupplier().get().longValue() == 1);
+ MetricsKey.METRIC_RT_AVG.getName()).applyAsLong() == 1);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_SUM.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_SUM.getName()).getSupplier().get().longValue() == 3);
+ MetricsKey.METRIC_RT_SUM.getName()).applyAsLong() == 3);
sampler.addRT(applicationName, RTType.APPLICATION, 4L);
collect = getCollect(RTType.APPLICATION);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_LAST.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_LAST.getName()).getSupplier().get().longValue() == 4);
+ MetricsKey.METRIC_RT_LAST.getName()).applyAsLong() == 4);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MIN.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MIN.getName()).getSupplier().get().longValue() == 4);
+ MetricsKey.METRIC_RT_MIN.getName()).applyAsLong() == 4);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_MAX.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_MAX.getName()).getSupplier().get().longValue() == 4);
+ MetricsKey.METRIC_RT_MAX.getName()).applyAsLong() == 4);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_AVG.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_AVG.getName()).getSupplier().get().longValue() == 4);
+ MetricsKey.METRIC_RT_AVG.getName()).applyAsLong() == 4);
Assertions.assertTrue(
null != collect.get(MetricsKey.METRIC_RT_SUM.getName()) &&
collect.get(
-
MetricsKey.METRIC_RT_SUM.getName()).getSupplier().get().longValue() == 4);
+ MetricsKey.METRIC_RT_SUM.getName()).applyAsLong() == 4);
}
@NotNull
+ @SuppressWarnings("rawtypes")
private Map<String, GaugeMetricSample> getCollect(RTType rtType) {
- List<GaugeMetricSample> metricSamples = sampler.collectRT((key,
metric, count) -> new GaugeMetricSample(key.formatName("consumer"),
metric.getTags(), RT, () -> count),rtType);
+ List<GaugeMetricSample> metricSamples = sampler.collectRT((key,
metric, count) ->
+ new GaugeMetricSample<>(key.formatName("consumer"),
metric.getTags(), RT, count, __ -> count), rtType);
- Map<String, GaugeMetricSample> collect = metricSamples.stream()
+ return metricSamples.stream()
.collect(Collectors.toMap(MetricSample::getName, v -> v));
- return collect;
- }
-
- private GaugeMetricSample getGaugeMetricSample(MetricsKey metricsKey,
MethodMetric methodMetric,
- MetricsCategory
metricsCategory, Supplier<Number> get) {
- return new
GaugeMetricSample(metricsKey.getNameByType(methodMetric.getSide()),
metricsKey.getDescription(),
- methodMetric.getTags(), metricsCategory, get);
}
- public class RequestMetricsCountSampler
- extends SimpleMetricsCountSampler<String, RTType,
RequestMethodMetrics> {
+ public class RequestMetricsCountSampler extends
SimpleMetricsCountSampler<String, RTType, RequestMethodMetrics> {
@Override
public List<MetricSample> sample() {
@@ -156,21 +152,22 @@ public class CountSamplerTest {
}
}
- static enum RTType{
+ static enum RTType {
METHOD_REQUEST,
APPLICATION
}
static class RequestMethodMetrics implements Metric {
- private String applicationName;
+ private final String applicationName;
public RequestMethodMetrics(String applicationName) {
- this.applicationName=applicationName;
+ this.applicationName = applicationName;
}
+
@Override
public Map<String, String> getTags() {
- Map<String,String> tags = new HashMap<>();
+ Map<String, String> tags = new HashMap<>();
tags.put("serviceName", "test");
tags.put("version", "1.0.0");
tags.put("uptime", "20220202");
diff --git
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
index c017565158..2cb8b99298 100644
---
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
+++
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
@@ -94,6 +94,7 @@ public class MetadataStatComposite implements MetricsExport {
}
@Override
+ @SuppressWarnings("rawtypes")
public List<GaugeMetricSample> exportNumMetrics() {
List<GaugeMetricSample> list = new ArrayList<>();
for (MetadataEvent.Type type : numStats.keySet()) {
@@ -106,19 +107,21 @@ public class MetadataStatComposite implements
MetricsExport {
}
@Override
+ @SuppressWarnings("rawtypes")
public List<GaugeMetricSample> exportRtMetrics() {
List<GaugeMetricSample> list = new ArrayList<>();
for (LongContainer<? extends Number> rtContainer : rtStats) {
MetricsKeyWrapper metricsKeyWrapper =
rtContainer.getMetricsKeyWrapper();
for (Map.Entry<String, ? extends Number> entry :
rtContainer.entrySet()) {
- list.add(new GaugeMetricSample(metricsKeyWrapper.targetKey(),
metricsKeyWrapper.targetDesc(),
ApplicationMetric.getTagsByName(entry.getKey()), MetricsCategory.RT, () ->
rtContainer.getValueSupplier().apply(entry.getKey())));
+ list.add(new
GaugeMetricSample<>(metricsKeyWrapper.targetKey(),
metricsKeyWrapper.targetDesc(),
ApplicationMetric.getTagsByName(entry.getKey()), MetricsCategory.RT, entry,
value -> rtContainer.getValueSupplier().apply(value.getKey())));
}
}
return list;
}
+ @SuppressWarnings("rawtypes")
public GaugeMetricSample convertToSample(String applicationName,
MetadataEvent.Type type, MetricsCategory category, AtomicLong targetNumber) {
- return new GaugeMetricSample(type.getMetricsKey(),
ApplicationMetric.getTagsByName(applicationName), category, targetNumber::get);
+ return new GaugeMetricSample<>(type.getMetricsKey(),
ApplicationMetric.getTagsByName(applicationName), category, targetNumber,
AtomicLong::get);
}
diff --git
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java
index bf4cfcbdb6..1212c0f55b 100644
---
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java
+++
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java
@@ -28,6 +28,7 @@ import
org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
+
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -100,10 +101,9 @@ class MetadataMetricsCollectorTest {
Map<String, String> tags = sample.getTags();
Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME),
applicationModel.getApplicationName());
}
- Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_PUSH, MetricsKey.METRIC_RT_LAST).targetKey()),
lastTimePair.calc());
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_PUSH, MetricsKey.METRIC_RT_MIN).targetKey()),
Math.min(c1, c2));
@@ -150,10 +150,9 @@ class MetadataMetricsCollectorTest {
Map<String, String> tags = sample.getTags();
Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME),
applicationModel.getApplicationName());
}
- Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_SUBSCRIBE, MetricsKey.METRIC_RT_LAST).targetKey()),
lastTimePair.calc());
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_SUBSCRIBE, MetricsKey.METRIC_RT_MIN).targetKey()),
Math.min(c1, c2));
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java
index 16ad947a92..2e41f9274f 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java
@@ -111,6 +111,7 @@ public class RegistryStatComposite implements MetricsExport
{
}
@Override
+ @SuppressWarnings({"rawtypes"})
public List<GaugeMetricSample> exportNumMetrics() {
List<GaugeMetricSample> list = new ArrayList<>();
for (RegistryEvent.Type type : numStats.keySet()) {
@@ -123,30 +124,33 @@ public class RegistryStatComposite implements
MetricsExport {
}
@Override
+ @SuppressWarnings({"rawtypes"})
public List<GaugeMetricSample> exportRtMetrics() {
List<GaugeMetricSample> list = new ArrayList<>();
for (LongContainer<? extends Number> rtContainer : rtStats) {
MetricsKeyWrapper metricsKeyWrapper =
rtContainer.getMetricsKeyWrapper();
for (Map.Entry<String, ? extends Number> entry :
rtContainer.entrySet()) {
- list.add(new GaugeMetricSample(metricsKeyWrapper.targetKey(),
metricsKeyWrapper.targetDesc(),
ApplicationMetric.getTagsByName(entry.getKey()), MetricsCategory.RT, () ->
rtContainer.getValueSupplier().apply(entry.getKey())));
+ list.add(new
GaugeMetricSample<>(metricsKeyWrapper.targetKey(),
metricsKeyWrapper.targetDesc(),
ApplicationMetric.getTagsByName(entry.getKey()), MetricsCategory.RT, entry,
value -> rtContainer.getValueSupplier().apply(value.getKey())));
}
}
return list;
}
+ @SuppressWarnings({"rawtypes"})
public List<GaugeMetricSample> exportSkMetrics() {
List<GaugeMetricSample> list = new ArrayList<>();
for (RegistryEvent.Type type : skStats.keySet()) {
Map<ServiceKeyMetric, AtomicLong> stringAtomicLongMap =
skStats.get(type);
for (ServiceKeyMetric serviceKeyMetric :
stringAtomicLongMap.keySet()) {
- list.add(new GaugeMetricSample(type.getMetricsKey(),
serviceKeyMetric.getTags(), MetricsCategory.REGISTRY,
stringAtomicLongMap.get(serviceKeyMetric)::get));
+ list.add(new GaugeMetricSample<>(type.getMetricsKey(),
serviceKeyMetric.getTags(), MetricsCategory.REGISTRY, stringAtomicLongMap,
value -> value.get(serviceKeyMetric).get()));
}
}
return list;
}
+ @SuppressWarnings({"rawtypes"})
public GaugeMetricSample convertToSample(String applicationName,
RegistryEvent.Type type, MetricsCategory category, AtomicLong targetNumber) {
- return new GaugeMetricSample(type.getMetricsKey(),
ApplicationMetric.getTagsByName(applicationName), category, targetNumber::get);
+ return new GaugeMetricSample<>(type.getMetricsKey(),
ApplicationMetric.getTagsByName(applicationName), category, targetNumber,
AtomicLong::get);
}
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java
index 2789758812..b5a0b1c19d 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java
@@ -98,10 +98,9 @@ class RegistryMetricsCollectorTest {
Map<String, String> tags = sample.getTags();
Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME),
applicationModel.getApplicationName());
}
- Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
metricSamples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_REGISTER, MetricsKey.METRIC_RT_LAST).targetKey()),
lastTimePair.calc());
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(OP_TYPE_REGISTER, MetricsKey.METRIC_RT_MIN).targetKey()),
Math.min(c1, c2));
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java
index 87c89ec4f2..759b4dae31 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java
@@ -74,10 +74,8 @@ class RegistryMetricsSampleTest {
Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME),
applicationName);
}
- Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> {
- Number number = ((GaugeMetricSample) k).getSupplier().get();
- return number.longValue();
- }));
+ @SuppressWarnings("rawtypes")
+ Map<String, Long> sampleMap =
samples.stream().collect(Collectors.toMap(MetricSample::getName, k ->
((GaugeMetricSample) k).applyAsLong()));
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(RegistryStatComposite.OP_TYPE_REGISTER,
MetricsKey.METRIC_RT_LAST).targetKey()), 0L);
Assertions.assertEquals(sampleMap.get(new
MetricsKeyWrapper(RegistryStatComposite.OP_TYPE_REGISTER,
MetricsKey.METRIC_RT_MIN).targetKey()), 0L);
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java
index bfa6d2e176..4931e24c13 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java
@@ -26,7 +26,9 @@ import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
+import java.util.function.ToDoubleFunction;
class GaugeMetricSampleTest {
@@ -35,7 +37,8 @@ class GaugeMetricSampleTest {
private static Map<String, String> tags;
private static MetricsCategory category;
private static String baseUnit;
- private static Supplier<Number> supplier;
+ private static AtomicLong value;
+ private static ToDoubleFunction<AtomicLong> apply;
@BeforeAll
public static void setup() {
@@ -44,20 +47,21 @@ class GaugeMetricSampleTest {
tags = new HashMap<>();
category = MetricsCategory.REQUESTS;
baseUnit = "byte";
- supplier = () -> 1;
+ value = new AtomicLong(1);
+ apply = AtomicLong::longValue;
}
@Test
void test() {
- GaugeMetricSample sample = new GaugeMetricSample(name, description,
tags, category, baseUnit, supplier);
+ GaugeMetricSample<?> sample = new GaugeMetricSample<>(name,
description, tags, category, baseUnit, value, apply);
Assertions.assertEquals(sample.getName(), name);
Assertions.assertEquals(sample.getDescription(), description);
Assertions.assertEquals(sample.getTags(), tags);
Assertions.assertEquals(sample.getType(), MetricSample.Type.GAUGE);
Assertions.assertEquals(sample.getCategory(), category);
Assertions.assertEquals(sample.getBaseUnit(), baseUnit);
- Assertions.assertEquals(sample.getSupplier().get(), 1);
- sample.setSupplier(() -> 2);
- Assertions.assertEquals(sample.getSupplier().get(), 2);
+ Assertions.assertEquals(1, sample.applyAsLong());
+ value.set(2);
+ Assertions.assertEquals(2, sample.applyAsLong());
}
}