Refactor GaugeMetricSample (#11692)
This commit is contained in:
parent
ed9e26f017
commit
dc74f131a7
|
|
@ -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 Supplier<Number> getSupplier() {
|
||||
return supplier;
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setSupplier(Supplier<Number> supplier) {
|
||||
this.supplier = supplier;
|
||||
public ToDoubleFunction<T> getApply() {
|
||||
return this.apply;
|
||||
}
|
||||
|
||||
public long applyAsLong() {
|
||||
return (long) getApply().applyAsDouble(getValue());
|
||||
}
|
||||
|
||||
public double applyAsDouble() {
|
||||
return getApply().applyAsDouble(getValue());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()))));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<>();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)));
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue