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 bef06c8313 Metrics Count Optimization (#11529)
bef06c8313 is described below
commit bef06c831352dd0af346857568d99e1f1337fccd
Author: jojocodeX <[email protected]>
AuthorDate: Tue Feb 14 07:33:58 2023 +0800
Metrics Count Optimization (#11529)
---
.../config/deploy/DefaultApplicationDeployer.java | 4 +-
.../apache/dubbo/metrics/event/MetricsEvent.java | 1 +
.../dubbo/metrics/model/ThreadPoolMetric.java | 3 +-
.../collector/AggregateMetricsCollector.java | 10 +-
.../metrics/collector/DefaultMetricsCollector.java | 215 ++++++---------------
.../collector/sample/MethodMetricsSampler.java | 102 ++++++++++
.../sample/MetricsCountSampleConfigurer.java | 85 ++++++++
.../collector/sample/MetricsCountSampler.java | 53 +++++
.../metrics/collector/sample/MetricsSampler.java | 27 +++
.../sample/SimpleMetricsCountSampler.java | 184 ++++++++++++++++++
.../collector/sample/ThreadPoolMetricsSampler.java | 83 ++++++++
.../collector/stat/DefaultMetricsStatHandler.java | 91 ---------
.../collector/stat/MetricsStatComposite.java | 131 -------------
...Executor.java => MethodMetricsInterceptor.java} | 49 +++--
.../apache/dubbo/metrics/filter/MetricsFilter.java | 16 +-
.../collector/AggregateMetricsCollectorTest.java | 23 ++-
.../collector/DefaultMetricsCollectorTest.java | 31 +--
17 files changed, 672 insertions(+), 436 deletions(-)
diff --git
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
index 21613c019e..bee56527c2 100644
---
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
+++
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
@@ -17,7 +17,6 @@
package org.apache.dubbo.config.deploy;
import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.config.ReferenceCache;
@@ -365,8 +364,7 @@ public class DefaultApplicationDeployer extends
AbstractDeployer<ApplicationMode
// TODO compatible with old usage of metrics, remove protocol check
after new metrics is ready for use.
if (metricsConfig != null &&
PROTOCOL_PROMETHEUS.equals(metricsConfig.getProtocol())) {
collector.setCollectEnabled(true);
-
collector.addApplicationInfo(applicationModel.getApplicationName(),
Version.getVersion());
- collector.addThreadPool(applicationModel.getFrameworkModel(),
applicationModel.getApplicationName());
+ collector.collectApplication(applicationModel);
String protocol = metricsConfig.getProtocol();
if (StringUtils.isNotEmpty(protocol)) {
MetricsReporterFactory metricsReporterFactory =
getExtensionLoader(MetricsReporterFactory.class).getAdaptiveExtension();
diff --git
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
index dee72086cb..3b21f6cc7f 100644
---
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
+++
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
@@ -55,5 +55,6 @@ public abstract class MetricsEvent {
UNKNOWN_FAILED,
TOTAL_FAILED,
APPLICATION_INFO
+
}
}
diff --git
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ThreadPoolMetric.java
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ThreadPoolMetric.java
index 162925e9fa..002653359b 100644
---
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ThreadPoolMetric.java
+++
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ThreadPoolMetric.java
@@ -33,7 +33,7 @@ import static
org.apache.dubbo.common.constants.MetricsConstants.TAG_THREAD_NAME
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName;
-public class ThreadPoolMetric {
+public class ThreadPoolMetric implements Metric{
private String applicationName;
@@ -91,7 +91,6 @@ public class ThreadPoolMetric {
tags.put(TAG_PID, ConfigUtils.getPid()+"");
tags.put(TAG_HOSTNAME, getLocalHostName());
tags.put(TAG_APPLICATION_NAME, applicationName);
-
tags.put(TAG_THREAD_NAME, threadPoolName);
return tags;
}
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 c44c9b0b57..963d3ddca4 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
@@ -17,10 +17,6 @@
package org.apache.dubbo.metrics.collector;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.context.ConfigManager;
@@ -37,6 +33,11 @@ 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.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;
@@ -158,7 +159,6 @@ public class AggregateMetricsCollector implements
MetricsCollector, MetricsListe
timeoutRequests.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT_AGG, k.getTags(),
REQUESTS, v::get)));
limitRequests.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT_AGG, k.getTags(),
REQUESTS, v::get)));
totalFailedRequests.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED_AGG,
k.getTags(), REQUESTS, v::get)));
-
}
private void collectQPS(List<MetricSample> list) {
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 ae7b2ebecd..40c58e86b7 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
@@ -17,37 +17,26 @@
package org.apache.dubbo.metrics.collector;
-import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
-import org.apache.dubbo.metrics.collector.stat.MetricsStatComposite;
-import org.apache.dubbo.metrics.collector.stat.MetricsStatHandler;
-import org.apache.dubbo.metrics.event.EmptyEvent;
+import org.apache.dubbo.common.Version;
+import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
+import org.apache.dubbo.metrics.collector.sample.MetricsCountSampleConfigurer;
+import org.apache.dubbo.metrics.collector.sample.MetricsSampler;
+import org.apache.dubbo.metrics.collector.sample.SimpleMetricsCountSampler;
+import org.apache.dubbo.metrics.collector.sample.ThreadPoolMetricsSampler;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
import org.apache.dubbo.metrics.listener.MetricsListener;
-import org.apache.dubbo.metrics.model.MetricsKey;
-import org.apache.dubbo.metrics.model.ThreadPoolMetric;
+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.FrameworkModel;
-import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.List;
-import java.util.HashSet;
-
-import java.util.Optional;
-import java.util.Set;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.Function;
-import static org.apache.dubbo.metrics.model.MetricsCategory.THREAD_POOL;
import static org.apache.dubbo.metrics.model.MetricsCategory.APPLICATION;
-import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
-import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
-
+import static
org.apache.dubbo.metrics.model.MetricsKey.APPLICATION_METRIC_INFO;
/**
* Default implementation of {@link MetricsCollector}
@@ -55,177 +44,83 @@ import static
org.apache.dubbo.metrics.model.MetricsCategory.RT;
public class DefaultMetricsCollector implements MetricsCollector {
private AtomicBoolean collectEnabled = new AtomicBoolean(false);
- private final Set<ThreadPoolMetric> threadPoolMetricSet = new
HashSet<ThreadPoolMetric>();
- private final MetricsStatComposite stats;
private final SimpleMetricsEventMulticaster eventMulticaster;
+ private MethodMetricsSampler methodSampler = new
MethodMetricsSampler(this);
+ private ThreadPoolMetricsSampler threadPoolSampler = new
ThreadPoolMetricsSampler(this);
+ private String applicationName;
+ private ApplicationModel applicationModel;
+ private List<MetricsSampler> samplers = new ArrayList<>();
public DefaultMetricsCollector() {
- this.stats = new MetricsStatComposite( this);
this.eventMulticaster = SimpleMetricsEventMulticaster.getInstance();
+ samplers.add(methodSampler);
+ samplers.add(applicationSampler);
+ samplers.add(threadPoolSampler);
}
- public void setCollectEnabled(Boolean collectEnabled) {
- this.collectEnabled.compareAndSet(isCollectEnabled(), collectEnabled);
- }
-
- public Boolean isCollectEnabled() {
- return collectEnabled.get();
- }
-
- public void increaseTotalRequests(String applicationName, Invocation
invocation) {
- increaseAndPublishEvent(applicationName, MetricsEvent.Type.TOTAL,
invocation);
- }
-
- public void increaseSucceedRequests(String applicationName, Invocation
invocation) {
- increaseAndPublishEvent(applicationName, MetricsEvent.Type.SUCCEED,
invocation);
- }
-
- public void increaseUnknownFailedRequests(String applicationName,
Invocation invocation) {
- increaseAndPublishEvent(applicationName,
MetricsEvent.Type.UNKNOWN_FAILED, invocation);
+ public void setApplicationName(String applicationName) {
+ this.applicationName = applicationName;
}
- public void businessFailedRequests(String applicationName, Invocation
invocation) {
- increaseAndPublishEvent(applicationName,
MetricsEvent.Type.BUSINESS_FAILED, invocation);
+ public String getApplicationName() {
+ return this.applicationName;
}
- public void timeoutRequests(String applicationName, Invocation invocation)
{
-
increaseAndPublishEvent(applicationName,MetricsEvent.Type.REQUEST_TIMEOUT,
invocation);
+ public ApplicationModel getApplicationModel(){
+ return this.applicationModel;
}
- public void limitRequests(String applicationName, Invocation invocation) {
-
increaseAndPublishEvent(applicationName,MetricsEvent.Type.REQUEST_LIMIT,
invocation);
+ public SimpleMetricsEventMulticaster getEventMulticaster(){
+ return this.eventMulticaster;
}
- public void increaseProcessingRequests(String applicationName, Invocation
invocation) {
- increaseAndPublishEvent(applicationName,MetricsEvent.Type.PROCESSING,
invocation);
- }
-
- public void decreaseProcessingRequests(String applicationName, Invocation
invocation) {
- decreaseAndPublishEvent(applicationName,MetricsEvent.Type.PROCESSING,
invocation);
- }
-
- public void totalFailedRequests(String applicationName, Invocation
invocation) {
-
increaseAndPublishEvent(applicationName,MetricsEvent.Type.TOTAL_FAILED,
invocation);
- }
-
- private void increaseAndPublishEvent(String applicationName,
MetricsEvent.Type total, Invocation invocation) {
- this.eventMulticaster.publishEvent(doExecute(total, statHandler ->
statHandler.increase(applicationName,invocation)));
+ public void setCollectEnabled(Boolean collectEnabled) {
+ this.collectEnabled.compareAndSet(isCollectEnabled(), collectEnabled);
}
- private void decreaseAndPublishEvent(String applicationName,
MetricsEvent.Type type, Invocation invocation) {
- this.eventMulticaster.publishEvent(doExecute(type, statHandler ->
statHandler.decrease(applicationName,invocation)));
+ public Boolean isCollectEnabled() {
+ return collectEnabled.get();
}
- public void addRT(String applicationName,Invocation invocation, Long
responseTime) {
-
this.eventMulticaster.publishEvent(stats.addRtAndRetrieveEvent(applicationName,invocation,
responseTime));
- }
- public void addApplicationInfo(String applicationName, String version) {
- doExecute(MetricsEvent.Type.APPLICATION_INFO, statHandler ->
statHandler.addApplication(applicationName,version));
+ public MethodMetricsSampler getMethodSampler(){
+ return this.methodSampler;
}
- public void addThreadPool(FrameworkModel frameworkModel, String
applicationName) {
- FrameworkExecutorRepository frameworkExecutorRepository =
-
frameworkModel.getBeanFactory().getBean(FrameworkExecutorRepository.class);
- addThreadPoolExecutor(applicationName, "SharedExecutor",
frameworkExecutorRepository.getSharedExecutor());
- addThreadPoolExecutor(applicationName, "MappingRefreshingExecutor",
frameworkExecutorRepository.getMappingRefreshingExecutor());
- addThreadPoolExecutor(applicationName, "PoolRouterExecutor",
frameworkExecutorRepository.getPoolRouterExecutor());
- }
-
- private void addThreadPoolExecutor(String applicationName, String
threadPoolName, ExecutorService executorService) {
- Optional<ExecutorService> executorOptional =
Optional.ofNullable(executorService);
- if (executorOptional.isPresent() && executorOptional.get() instanceof
ThreadPoolExecutor ) {
- threadPoolMetricSet.add(new ThreadPoolMetric(applicationName,
threadPoolName,
- (ThreadPoolExecutor) executorOptional.get()));
- }
+ public void collectApplication(ApplicationModel applicationModel) {
+ this.setApplicationName(applicationModel.getApplicationName());
+ this.applicationModel = applicationModel;
+
applicationSampler.inc(applicationName,MetricsEvent.Type.APPLICATION_INFO);
}
@Override
public List<MetricSample> collect() {
List<MetricSample> list = new ArrayList<>();
- collectApplication(list);
- collectRequests(list);
- collectRT(list);
- collectThreadPool(list);
+ for (MetricsSampler sampler : samplers) {
+ List<MetricSample> sample = sampler.sample();
+ list.addAll(sample);
+ }
return list;
}
- private void collectThreadPool(List<MetricSample> list) {
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_CORE_SIZE, e.getTags(), THREAD_POOL,
e::getCorePoolSize)));
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_LARGEST_SIZE, e.getTags(),
THREAD_POOL, e::getLargestPoolSize)));
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_MAX_SIZE, e.getTags(), THREAD_POOL,
e::getMaximumPoolSize)));
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_ACTIVE_SIZE, e.getTags(), THREAD_POOL,
e::getActiveCount)));
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_THREAD_COUNT, e.getTags(),
THREAD_POOL, e::getPoolSize)));
- threadPoolMetricSet.forEach(e -> list.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_QUEUE_SIZE, e.getTags(), THREAD_POOL,
e::getQueueSize)));
- }
-
- private void collectApplication(List<MetricSample> list) {
- doCollect(MetricsEvent.Type.APPLICATION_INFO,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.APPLICATION_METRIC_INFO, k.getTags(),
- APPLICATION, v::get))));
- }
-
- private void collectRequests(List<MetricSample> list) {
- doCollect(MetricsEvent.Type.TOTAL, MetricsStatHandler::get).filter(e
-> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS, k.getTags(), REQUESTS,
v::get))));
-
- doCollect(MetricsEvent.Type.SUCCEED, MetricsStatHandler::get).filter(e
-> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED, k.getTags(),
REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.UNKNOWN_FAILED,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED, k.getTags(),
REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.PROCESSING,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING, k.getTags(),
REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.BUSINESS_FAILED,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED,
k.getTags(), REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.REQUEST_TIMEOUT,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT, k.getTags(),
REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.REQUEST_LIMIT,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT, k.getTags(),
REQUESTS, v::get))));
-
- doCollect(MetricsEvent.Type.TOTAL_FAILED,
MetricsStatHandler::get).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED,
k.getTags(), REQUESTS, v::get))));
-
- }
-
- private void collectRT(List<MetricSample> list) {
- this.stats.getLastRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_LAST, k.getTags(), RT,
v::get)));
- this.stats.getMinRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MIN, k.getTags(), RT, v::get)));
- this.stats.getMaxRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MAX, k.getTags(), RT, v::get)));
-
- this.stats.getTotalRT().forEach((k, v) -> {
- list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_SUM,
k.getTags(), RT, v::get));
-
- AtomicLong avg = this.stats.getAvgRT().get(k);
- AtomicLong count = this.stats.getRtCount().get(k);
- avg.set(v.get() / count.get());
- list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_AVG,
k.getTags(), RT, avg::get));
- });
+ public void addListener(MetricsListener listener) {
+ this.eventMulticaster.addListener(listener);
}
- private <
- T> Optional<T> doCollect(MetricsEvent.Type metricsEventType,
Function<MetricsStatHandler, T> statExecutor) {
- if (isCollectEnabled()) {
- MetricsStatHandler handler = stats.getHandler(metricsEventType);
- T result = statExecutor.apply(handler);
- return Optional.ofNullable(result);
+ public SimpleMetricsCountSampler<String,MetricsEvent.Type,
ApplicationMetric> applicationSampler = new
SimpleMetricsCountSampler<String,MetricsEvent.Type,ApplicationMetric>(){
+ @Override
+ 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))));
+ return samples;
}
- return Optional.empty();
- }
- private MetricsEvent doExecute(MetricsEvent.Type metricsEventType,
- Function<MetricsStatHandler, MetricsEvent>
statExecutor) {
- if (isCollectEnabled()) {
- MetricsStatHandler handler = stats.getHandler(metricsEventType);
- return statExecutor.apply(handler);
+ @Override
+ protected void countConfigure(
+ MetricsCountSampleConfigurer<String, MetricsEvent.Type,
ApplicationMetric> sampleConfigure) {
+ sampleConfigure.configureMetrics(configure -> new
ApplicationMetric(sampleConfigure.getSource(),
+ Version.getVersion()));
}
- return EmptyEvent.instance();
- }
-
- public void addListener(MetricsListener listener) {
- this.eventMulticaster.addListener(listener);
- }
+ };
}
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
new file mode 100644
index 0000000000..b915e80792
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.RTEvent;
+import org.apache.dubbo.metrics.event.RequestEvent;
+import org.apache.dubbo.metrics.model.MethodMetric;
+import org.apache.dubbo.metrics.model.Metric;
+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.concurrent.atomic.AtomicLong;
+
+import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
+import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
+
+public class MethodMetricsSampler extends
SimpleMetricsCountSampler<Invocation, MetricsEvent.Type, MethodMetric>{
+
+ private DefaultMetricsCollector collector;
+
+ public MethodMetricsSampler(DefaultMetricsCollector collector) {
+ this.collector = collector;
+ }
+
+ @Override
+ protected void countConfigure(
+ MetricsCountSampleConfigurer<Invocation, MetricsEvent.Type,
MethodMetric> sampleConfigure) {
+ sampleConfigure.configureMetrics(configure -> new
MethodMetric(collector.getApplicationName(), configure.getSource()));
+ sampleConfigure.configureEventHandler(configure ->
collector.getEventMulticaster().publishEvent(new
RequestEvent(configure.getMetric(), configure.getMetricName())));
+ }
+
+ @Override
+ public void rtConfigure(
+ MetricsCountSampleConfigurer<Invocation, MetricsEvent.Type,
MethodMetric> sampleConfigure) {
+ sampleConfigure.configureMetrics(configure -> new
MethodMetric(collector.getApplicationName(), configure.getSource()));
+ sampleConfigure.configureEventHandler(configure ->
collector.getEventMulticaster().publishEvent(new RTEvent(configure.getMetric(),
configure.getRt())));
+ }
+
+ @Override
+ public List<MetricSample> sample() {
+ List<MetricSample> metricSamples = new ArrayList<>();
+
+ collect(metricSamples);
+
+ collectRT(metricSamples);
+
+ return metricSamples;
+ }
+
+ private void collect(List<MetricSample> list) {
+ count(list, MetricsEvent.Type.TOTAL,
MetricsKey.PROVIDER_METRIC_REQUESTS);
+ count(list, MetricsEvent.Type.SUCCEED,
MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED);
+ count(list, MetricsEvent.Type.UNKNOWN_FAILED,
MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED);
+ count(list, MetricsEvent.Type.PROCESSING,
MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING);
+ count(list, MetricsEvent.Type.BUSINESS_FAILED,
MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED);
+ count(list, MetricsEvent.Type.REQUEST_TIMEOUT,
MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT);
+ count(list, MetricsEvent.Type.REQUEST_LIMIT,
MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT);
+ count(list, MetricsEvent.Type.TOTAL_FAILED,
MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED);
+ }
+
+ private void collectRT(List<MetricSample> list) {
+ this.getLastRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_LAST, k.getTags(), RT,
v::get)));
+ this.getMinRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MIN, k.getTags(), RT, v::get)));
+ this.getMaxRT().forEach((k, v) -> list.add(new
GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MAX, k.getTags(), RT, v::get)));
+
+ this.getTotalRT().forEach((k, v) -> {
+ list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_SUM,
k.getTags(), RT, v::get));
+
+ AtomicLong avg = this.getAvgRT().get(k);
+ AtomicLong count = this.getRtCount().get(k);
+ avg.set(v.get() / count.get());
+ list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_AVG,
k.getTags(), RT, avg::get));
+ });
+ }
+
+ 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(new
GaugeMetricSample(metricsKey, k.getTags(),
+ REQUESTS, v::get))));
+ }
+}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
new file mode 100644
index 0000000000..15da69cd56
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.metrics.model.Metric;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+public class MetricsCountSampleConfigurer<S,K,M extends Metric> {
+
+ public S source;
+
+ public K metricName;
+
+ public M metric;
+
+ public Long rt;
+
+ public Consumer<MetricsCountSampleConfigurer<S,K,M>> fireEventHandler =
new Consumer<MetricsCountSampleConfigurer<S, K,M>>() {
+ @Override
+ public void accept(MetricsCountSampleConfigurer<S, K,M>
skMetricsCountSampleConfigure) {
+
+ }
+ };
+
+ public void setSource(S source){
+ this.source = source;
+ }
+
+ public MetricsCountSampleConfigurer<S,K,M> setMetricsName(K metricName){
+ this.metricName = metricName;
+ return this;
+ }
+
+ public MetricsCountSampleConfigurer<S,K,M>
configureMetrics(Function<MetricsCountSampleConfigurer<S,K,M>,M> builder){
+ this.metric = builder.apply(this);
+ return this;
+ }
+
+ public MetricsCountSampleConfigurer<S,K,M> configureEventHandler(
+ Consumer<MetricsCountSampleConfigurer<S,K,M>> fireEventHandler){
+ this.fireEventHandler = fireEventHandler;
+ return this;
+ }
+
+ public S getSource() {
+ return source;
+ }
+
+ public K getMetricName() {
+ return metricName;
+ }
+
+ public M getMetric() {
+ return metric;
+ }
+
+ public void setRt(Long rt) {
+ this.rt = rt;
+ }
+
+ public Long getRt(){
+ return this.rt;
+ }
+
+ public Consumer<MetricsCountSampleConfigurer<S, K,M>>
getFireEventHandler() {
+ return fireEventHandler;
+ }
+}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
new file mode 100644
index 0000000000..1d16155c10
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.metrics.model.Metric;
+
+import java.util.Optional;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.LongAccumulator;
+
+public interface MetricsCountSampler<S, K,M extends Metric> extends
MetricsSampler {
+
+ void inc(S source, K metricName);
+
+ void dec(S source, K metricName);
+
+ void incOnEvent(S source, K metricName);
+
+ void decOnEvent(S source, K metricName);
+
+ void addRT(S source, Long rt);
+
+ Optional<ConcurrentMap<M, AtomicLong>> getCount(K metricName);
+
+ ConcurrentMap<M, AtomicLong> getLastRT();
+
+ ConcurrentMap<M, LongAccumulator> getMinRT();
+
+ ConcurrentMap<M, LongAccumulator> getMaxRT();
+
+ ConcurrentMap<M, AtomicLong> getAvgRT();
+
+ ConcurrentMap<M, AtomicLong> getTotalRT();
+
+ ConcurrentMap<M, AtomicLong> getRtCount();
+
+}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsSampler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsSampler.java
new file mode 100644
index 0000000000..9d59726288
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsSampler.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+
+import java.util.List;
+
+public interface MetricsSampler {
+
+ List<MetricSample> sample();
+}
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
new file mode 100644
index 0000000000..1160e5ca76
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.common.utils.Assert;
+import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
+import org.apache.dubbo.metrics.model.Metric;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.LongAccumulator;
+import java.util.function.Function;
+
+/**
+ * @param <S> request source
+ * @param <K> metricsName
+ * @param <M> metric
+ */
+
+public abstract class SimpleMetricsCountSampler<S, K, M extends Metric>
+ implements MetricsCountSampler<S, K, M> {
+
+ private final ConcurrentMap<M, AtomicLong> lastRT = new
ConcurrentHashMap<>();
+ private final ConcurrentMap<M, LongAccumulator> minRT = new
ConcurrentHashMap<>();
+ private final ConcurrentMap<M, LongAccumulator> maxRT = new
ConcurrentHashMap<>();
+ private final ConcurrentMap<M, AtomicLong> avgRT = new
ConcurrentHashMap<>();
+ private final ConcurrentMap<M, AtomicLong> totalRT = new
ConcurrentHashMap<>();
+ private final ConcurrentMap<M, AtomicLong> rtCount = new
ConcurrentHashMap<>();
+
+ private Map<K, ConcurrentMap<M, AtomicLong>> metricCounter = new
ConcurrentHashMap<>();
+
+ @Override
+ public void inc(S source, K metricName) {
+ doExecute(source,metricName,counter->{
+ counter.incrementAndGet();
+ return false;
+ });
+ }
+
+ @Override
+ public void dec(S source, K metricName) {
+ doExecute(source,metricName,counter->{
+ counter.decrementAndGet();
+ return false;
+ });
+ }
+
+ @Override
+ public void incOnEvent(S source, K metricName) {
+ doExecute(source,metricName,counter->{
+ counter.incrementAndGet();
+ return true;
+ });
+ }
+
+ @Override
+ public void decOnEvent(S source, K metricName) {
+ doExecute(source,metricName,counter->{
+ counter.decrementAndGet();
+ return true;
+ });
+ }
+
+ @Override
+ public void addRT(S source, Long rt) {
+ MetricsCountSampleConfigurer<S,K,M> sampleConfigure = new
MetricsCountSampleConfigurer<>();
+ sampleConfigure.setSource(source);
+
+ this.rtConfigure(sampleConfigure);
+
+ M metric = sampleConfigure.getMetric();
+
+ AtomicLong last = ConcurrentHashMapUtils.computeIfAbsent(lastRT,
metric, k -> new AtomicLong());
+ last.set(rt);
+
+ LongAccumulator min = ConcurrentHashMapUtils.computeIfAbsent(minRT,
metric, k -> new LongAccumulator(Long::min, Long.MAX_VALUE));
+ min.accumulate(rt);
+
+ LongAccumulator max = ConcurrentHashMapUtils.computeIfAbsent(maxRT,
metric, k -> new LongAccumulator(Long::max, Long.MIN_VALUE));
+ max.accumulate(rt);
+
+ AtomicLong total = ConcurrentHashMapUtils.computeIfAbsent(totalRT,
metric, k -> new AtomicLong());
+ total.addAndGet(rt);
+
+ AtomicLong count = ConcurrentHashMapUtils.computeIfAbsent(rtCount,
metric, k -> new AtomicLong());
+ count.incrementAndGet();
+
+ ConcurrentHashMapUtils.computeIfAbsent(avgRT, metric, key -> new
AtomicLong());
+
+ sampleConfigure.setRt(rt);
+
+ sampleConfigure.getFireEventHandler().accept(sampleConfigure);
+
+ }
+
+ @Override
+ public Optional<ConcurrentMap<M, AtomicLong>> getCount(K metricName) {
+ return Optional.ofNullable(metricCounter.get(metricName) == null ?
+ new ConcurrentHashMap<>() :
+ metricCounter.get(metricName));
+ }
+
+ @Override
+ public ConcurrentMap<M, AtomicLong> getLastRT() {
+ return this.lastRT;
+ }
+
+ @Override
+ public ConcurrentMap<M, LongAccumulator> getMinRT() {
+ return this.minRT;
+ }
+
+ @Override
+ public ConcurrentMap<M, LongAccumulator> getMaxRT() {
+ return this.maxRT;
+ }
+
+ @Override
+ public ConcurrentMap<M, AtomicLong> getAvgRT() {
+ return this.avgRT;
+ }
+
+ @Override
+ public ConcurrentMap<M, AtomicLong> getTotalRT() {
+ return this.totalRT;
+ }
+
+ @Override
+ public ConcurrentMap<M, AtomicLong> getRtCount() {
+ return this.rtCount;
+ }
+
+ protected void rtConfigure(MetricsCountSampleConfigurer<S,K,M> configure) {
+
+ }
+
+ protected abstract void countConfigure(MetricsCountSampleConfigurer<S,K,M>
sampleConfigure);
+
+ private void doExecute(S source,K metricsName,Function<AtomicLong,Boolean>
counter){
+ MetricsCountSampleConfigurer<S,K,M> sampleConfigure = new
MetricsCountSampleConfigurer<>();
+ sampleConfigure.setSource(source);
+ sampleConfigure.setMetricsName(metricsName);
+
+ this.countConfigure(sampleConfigure);
+
+ Map<M, AtomicLong> metricAtomic = metricCounter.get(metricsName);
+
+ if (metricAtomic == null) {
+ metricAtomic = metricCounter.computeIfAbsent(metricsName, k -> new
ConcurrentHashMap<>());
+ }
+
+ Assert.notNull(sampleConfigure.getMetric(), "metrics is null");
+
+ AtomicLong atomicCounter =
metricAtomic.get(sampleConfigure.getMetric());
+
+ if (atomicCounter == null) {
+ atomicCounter =
metricAtomic.computeIfAbsent(sampleConfigure.getMetric(), k -> new
AtomicLong());
+ }
+ Boolean isEvent = counter.apply(atomicCounter);
+
+ if (isEvent) {
+ sampleConfigure.getFireEventHandler().accept(sampleConfigure);
+ }
+ }
+
+}
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
new file mode 100644
index 0000000000..67cb73d3fe
--- /dev/null
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSampler.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.metrics.collector.sample;
+
+import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
+import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+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.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import static org.apache.dubbo.metrics.model.MetricsCategory.THREAD_POOL;
+
+public class ThreadPoolMetricsSampler implements MetricsSampler {
+
+ private DefaultMetricsCollector collector;
+ private FrameworkExecutorRepository frameworkExecutorRepository;
+ private Set<ThreadPoolMetric> threadPoolMetricSet = new HashSet<>();
+
+ public ThreadPoolMetricsSampler(DefaultMetricsCollector collector) {
+ this.collector = collector;
+ }
+
+ @Override
+ public List<MetricSample> sample() {
+ collect();
+ List<MetricSample> metricSamples = new ArrayList<>();
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_CORE_SIZE, e.getTags(), THREAD_POOL,
e::getCorePoolSize)));
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_LARGEST_SIZE, e.getTags(),
THREAD_POOL, e::getLargestPoolSize)));
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_MAX_SIZE, e.getTags(), THREAD_POOL,
e::getMaximumPoolSize)));
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_ACTIVE_SIZE, e.getTags(), THREAD_POOL,
e::getActiveCount)));
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_THREAD_COUNT, e.getTags(),
THREAD_POOL, e::getPoolSize)));
+ threadPoolMetricSet.forEach(e -> metricSamples.add(new
GaugeMetricSample(MetricsKey.THREAD_POOL_QUEUE_SIZE, e.getTags(), THREAD_POOL,
e::getQueueSize)));
+
+ return metricSamples;
+ }
+
+ private void collect() {
+ try{
+ if (this.frameworkExecutorRepository == null) {
+ this.frameworkExecutorRepository =
collector.getApplicationModel().getFrameworkModel().getBeanFactory().getBean(FrameworkExecutorRepository.class);
+ }
+ }catch(Exception ex){}
+
+ if (frameworkExecutorRepository != null) {
+ addThread("SharedExecutor",
frameworkExecutorRepository.getSharedExecutor());
+ addThread("MappingRefreshingExecutor",
frameworkExecutorRepository.getMappingRefreshingExecutor());
+ addThread("PoolRouterExecutor",
frameworkExecutorRepository.getPoolRouterExecutor());
+ }
+ }
+
+ private void addThread(String threadPoolName, ExecutorService
executorService) {
+ Optional<ExecutorService> executorOptional =
Optional.ofNullable(executorService);
+ if (executorOptional.isPresent() && executorOptional.get() instanceof
ThreadPoolExecutor) {
+ threadPoolMetricSet.add(
+ new ThreadPoolMetric(collector.getApplicationName(),
threadPoolName,
+ (ThreadPoolExecutor) executorOptional.get()));
+ }
+
+ }
+}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java
deleted file mode 100644
index 077fd41f64..0000000000
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/DefaultMetricsStatHandler.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.dubbo.metrics.collector.stat;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.BiConsumer;
-
-import org.apache.dubbo.metrics.event.EmptyEvent;
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.model.ApplicationMetric;
-import org.apache.dubbo.metrics.model.MethodMetric;
-import org.apache.dubbo.rpc.Invocation;
-
-
-public class DefaultMetricsStatHandler implements MetricsStatHandler {
-
- private final Map<MethodMetric, AtomicLong> counts = new
ConcurrentHashMap<>();
-
- private final Map<ApplicationMetric, AtomicLong> applicationMetrics = new
ConcurrentHashMap<>();
-
- public DefaultMetricsStatHandler() {
- }
-
- @Override
- public MetricsEvent increase(String applicationName, Invocation
invocation) {
- return this.doIncrExecute(applicationName, invocation);
- }
-
- public MetricsEvent decrease(String applicationName, Invocation
invocation) {
- return this.doDecrExecute(applicationName,invocation);
- }
- @Override
- public MetricsEvent addApplication(String applicationName, String version)
{
- ApplicationMetric applicationMetric = new
ApplicationMetric(applicationName, version);
- AtomicLong count =
applicationMetrics.computeIfAbsent(applicationMetric, k -> new AtomicLong(0L));
- count.incrementAndGet();
- return EmptyEvent.instance();
- }
- protected MetricsEvent doExecute(String applicationName, Invocation
invocation, BiConsumer<MethodMetric, Map<MethodMetric, AtomicLong>> execute) {
- MethodMetric metric = new MethodMetric(applicationName, invocation);
- execute.accept(metric, counts);
-
- return this.retrieveMetricsEvent(metric);
- }
-
- protected MetricsEvent doIncrExecute(String applicationName, Invocation
invocation) {
- return this.doExecute(applicationName, invocation, (metric, counts) ->
{
- AtomicLong count = counts.computeIfAbsent(metric, k -> new
AtomicLong(0L));
- count.incrementAndGet();
- });
- }
-
-
- protected MetricsEvent doDecrExecute(String applicationName, Invocation
invocation) {
- return this.doExecute(applicationName, invocation, (metric, counts) ->
{
- AtomicLong count = counts.computeIfAbsent(metric, k -> new
AtomicLong(0L));
- count.decrementAndGet();
- });
- }
-
- @Override
- public Map<MethodMetric, AtomicLong> get() {
- return counts;
- }
-
- public MetricsEvent retrieveMetricsEvent(MethodMetric metric) {
- return EmptyEvent.instance();
- }
-
- public Map<ApplicationMetric, AtomicLong> getApplicationMetric() {
- return applicationMetrics;
- }
-
-
-}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java
deleted file mode 100644
index f1f4157af9..0000000000
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatComposite.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.dubbo.metrics.collector.stat;
-
-import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
-import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
-import org.apache.dubbo.metrics.event.EmptyEvent;
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.RTEvent;
-import org.apache.dubbo.metrics.event.RequestEvent;
-import org.apache.dubbo.metrics.model.MethodMetric;
-import org.apache.dubbo.rpc.Invocation;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.LongAccumulator;
-
-public class MetricsStatComposite {
-
- public Map<MetricsEvent.Type, MetricsStatHandler> stats = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, AtomicLong> lastRT = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, LongAccumulator> minRT = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, LongAccumulator> maxRT = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, AtomicLong> avgRT = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, AtomicLong> totalRT = new
ConcurrentHashMap<>();
- private final ConcurrentMap<MethodMetric, AtomicLong> rtCount = new
ConcurrentHashMap<>();
- private DefaultMetricsCollector collector;
-
- public MetricsStatComposite(DefaultMetricsCollector collector) {
- this.collector = collector;
- this.init();
- }
-
- public MetricsStatHandler getHandler(MetricsEvent.Type statType) {
- return stats.get(statType);
- }
-
- public Map<MethodMetric, AtomicLong> getLastRT() {
- return this.lastRT;
- }
-
- public Map<MethodMetric, LongAccumulator> getMinRT() {
- return this.minRT;
- }
-
- public Map<MethodMetric, LongAccumulator> getMaxRT() {
- return this.maxRT;
- }
-
- public Map<MethodMetric, AtomicLong> getAvgRT() {
- return this.avgRT;
- }
-
- public Map<MethodMetric, AtomicLong> getTotalRT() {
- return this.totalRT;
- }
-
- public Map<MethodMetric, AtomicLong> getRtCount() {
- return this.rtCount;
- }
-
- public MetricsEvent addRtAndRetrieveEvent(String applicationName,
Invocation invocation, Long responseTime) {
- if (!collector.isCollectEnabled()) {
- return EmptyEvent.instance();
- }
- MethodMetric metric = new MethodMetric(applicationName, invocation);
-
- AtomicLong last = ConcurrentHashMapUtils.computeIfAbsent(lastRT,
metric, k -> new AtomicLong());
- last.set(responseTime);
-
- LongAccumulator min = ConcurrentHashMapUtils.computeIfAbsent(minRT,
metric, k -> new LongAccumulator(Long::min, Long.MAX_VALUE));
- min.accumulate(responseTime);
-
- LongAccumulator max = ConcurrentHashMapUtils.computeIfAbsent(maxRT,
metric, k -> new LongAccumulator(Long::max, Long.MIN_VALUE));
- max.accumulate(responseTime);
-
- AtomicLong total = ConcurrentHashMapUtils.computeIfAbsent(totalRT,
metric, k -> new AtomicLong());
- total.addAndGet(responseTime);
-
- AtomicLong count = ConcurrentHashMapUtils.computeIfAbsent(rtCount,
metric, k -> new AtomicLong());
- count.incrementAndGet();
-
- ConcurrentHashMapUtils.computeIfAbsent(avgRT, metric, k -> new
AtomicLong());
-
- return new RTEvent(metric, responseTime);
- }
- public void addApplicationInfo(String applicationName, String version) {
- MetricsStatHandler metricsStatHandler =
stats.get(MetricsEvent.Type.APPLICATION_INFO);
- metricsStatHandler.addApplication(applicationName, version);
- }
-
- private void init() {
- stats.put(MetricsEvent.Type.TOTAL,
buildMetricsStatHandler(MetricsEvent.Type.TOTAL));
- stats.put(MetricsEvent.Type.SUCCEED,
buildMetricsStatHandler(MetricsEvent.Type.SUCCEED));
- stats.put(MetricsEvent.Type.UNKNOWN_FAILED,
buildMetricsStatHandler(MetricsEvent.Type.UNKNOWN_FAILED));
- stats.put(MetricsEvent.Type.BUSINESS_FAILED,
buildMetricsStatHandler(MetricsEvent.Type.BUSINESS_FAILED));
- stats.put(MetricsEvent.Type.PROCESSING, new
DefaultMetricsStatHandler());
- stats.put(MetricsEvent.Type.REQUEST_LIMIT,
buildMetricsStatHandler(MetricsEvent.Type.REQUEST_LIMIT));
- stats.put(MetricsEvent.Type.REQUEST_TIMEOUT,
buildMetricsStatHandler(MetricsEvent.Type.REQUEST_TIMEOUT));
- stats.put(MetricsEvent.Type.TOTAL_FAILED,
buildMetricsStatHandler(MetricsEvent.Type.TOTAL_FAILED));
- stats.put(MetricsEvent.Type.APPLICATION_INFO,
buildMetricsStatHandler(MetricsEvent.Type.APPLICATION_INFO));
- }
-
- private DefaultMetricsStatHandler
buildMetricsStatHandler(MetricsEvent.Type type) {
- return new DefaultMetricsStatHandler() {
- @Override
- public MetricsEvent retrieveMetricsEvent(MethodMetric metric) {
- return new RequestEvent(metric, type);
- }
- };
- }
-
-
-}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsCollectExecutor.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
similarity index 51%
rename from
dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsCollectExecutor.java
rename to
dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
index 4e378b49e9..d327dcb789 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsCollectExecutor.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
@@ -17,7 +17,8 @@
package org.apache.dubbo.metrics.filter;
-import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
+import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
@@ -26,62 +27,68 @@ import java.util.function.Supplier;
import static
org.apache.dubbo.common.constants.MetricsConstants.METRIC_FILTER_START_TIME;
-public class MetricsCollectExecutor {
+public class MethodMetricsInterceptor {
- public static void beforeExecute(String applicationName,
DefaultMetricsCollector collector, Invocation invocation) {
- collector.increaseTotalRequests(applicationName, invocation);
- collector.increaseProcessingRequests(applicationName, invocation);
+ private MethodMetricsSampler sampler;
+
+ public MethodMetricsInterceptor(MethodMetricsSampler sampler) {
+ this.sampler = sampler;
+ }
+
+ public void beforeExecute(Invocation invocation) {
+ sampler.incOnEvent(invocation, MetricsEvent.Type.TOTAL);
+ sampler.incOnEvent(invocation,MetricsEvent.Type.PROCESSING);
invocation.put(METRIC_FILTER_START_TIME, System.currentTimeMillis());
}
- public static void postExecute(String applicationName,
DefaultMetricsCollector collector, Invocation invocation, Result result) {
+ public void postExecute(Invocation invocation, Result result) {
if (result.hasException()) {
- throwExecute(applicationName, collector, invocation,
result.getException());
+ throwExecute(invocation, result.getException());
return;
}
- collector.increaseSucceedRequests(applicationName, invocation);
- endExecute(applicationName, collector, invocation);
+ sampler.incOnEvent(invocation,MetricsEvent.Type.SUCCEED);
+ endExecute(invocation);
}
- public static void throwExecute(String applicationName,
DefaultMetricsCollector collector, Invocation invocation, Throwable throwable) {
+ public void throwExecute(Invocation invocation, Throwable throwable) {
if (throwable instanceof RpcException) {
RpcException rpcException = (RpcException) throwable;
switch (rpcException.getCode()) {
case RpcException.TIMEOUT_EXCEPTION:
- collector.timeoutRequests(applicationName, invocation);
+
sampler.incOnEvent(invocation,MetricsEvent.Type.REQUEST_TIMEOUT);
break;
case RpcException.LIMIT_EXCEEDED_EXCEPTION:
- collector.limitRequests(applicationName, invocation);
+
sampler.incOnEvent(invocation,MetricsEvent.Type.REQUEST_LIMIT);
break;
case RpcException.BIZ_EXCEPTION:
- collector.businessFailedRequests(applicationName,
invocation);
+
sampler.incOnEvent(invocation,MetricsEvent.Type.BUSINESS_FAILED);
break;
default:
- collector.increaseUnknownFailedRequests(applicationName,
invocation);
+
sampler.incOnEvent(invocation,MetricsEvent.Type.UNKNOWN_FAILED);
}
}
- collector.totalFailedRequests(applicationName, invocation);
+ sampler.incOnEvent(invocation,MetricsEvent.Type.TOTAL_FAILED);
- endExecute(applicationName, collector, invocation, () -> throwable
instanceof RpcException && ((RpcException) throwable).isBiz());
+ endExecute(invocation, () -> throwable instanceof RpcException &&
((RpcException) throwable).isBiz());
}
- private static void endExecute(String applicationName,
DefaultMetricsCollector collector, Invocation invocation) {
- endExecute(applicationName, collector, invocation, () -> true);
+ private void endExecute(Invocation invocation) {
+ endExecute(invocation, () -> true);
}
- private static void endExecute(String applicationName,
DefaultMetricsCollector collector, Invocation invocation, Supplier<Boolean>
rtStat) {
+ private void endExecute(Invocation invocation, Supplier<Boolean> rtStat) {
if (rtStat.get()) {
Long endTime = System.currentTimeMillis();
Long beginTime = (Long) invocation.get(METRIC_FILTER_START_TIME);
Long rt = endTime - beginTime;
- collector.addRT(applicationName, invocation, rt);
+ sampler.addRT(invocation, rt);
}
- collector.decreaseProcessingRequests(applicationName, invocation);
+ sampler.dec(invocation,MetricsEvent.Type.PROCESSING);
}
}
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
index 852e318be0..2f428dc016 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
@@ -34,13 +34,17 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
private DefaultMetricsCollector collector = null;
- private ApplicationModel applicationModel;
+ private MethodMetricsInterceptor metricsInterceptor;
@Override
public void setApplicationModel(ApplicationModel applicationModel) {
- this.applicationModel = applicationModel;
collector =
applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
+
+ if (collector != null) {
+ metricsInterceptor = new
MethodMetricsInterceptor(collector.getMethodSampler());
+ }
+
}
@Override
@@ -48,9 +52,11 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return invoker.invoke(invocation);
}
-
MetricsCollectExecutor.beforeExecute(applicationModel.getApplicationName(),
collector, invocation);
+
+ metricsInterceptor.beforeExecute(invocation);
return invoker.invoke(invocation);
+
}
@Override
@@ -58,7 +64,7 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
-
MetricsCollectExecutor.postExecute(applicationModel.getApplicationName(),
collector, invocation, result);
+ metricsInterceptor.postExecute(invocation, result);
}
@Override
@@ -66,7 +72,7 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
-
MetricsCollectExecutor.throwExecute(applicationModel.getApplicationName(),
collector, invocation, t);
+ metricsInterceptor.throwExecute(invocation, t);
}
}
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 9c863db1f7..6359223805 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
@@ -20,12 +20,13 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.nested.AggregationConfig;
+import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
+import org.apache.dubbo.metrics.event.MetricsEvent;
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.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;
@@ -80,7 +81,6 @@ class AggregateMetricsCollectorTest {
invocation.setTargetServiceUniqueName(group + "/" + interfaceName +
":" + version);
invocation.setAttachment(GROUP_KEY, group);
invocation.setAttachment(VERSION_KEY, version);
-
}
@AfterEach
@@ -92,10 +92,14 @@ class AggregateMetricsCollectorTest {
void testRequestsMetrics() {
String applicationName = applicationModel.getApplicationName();
AggregateMetricsCollector collector = new
AggregateMetricsCollector(applicationModel);
- defaultCollector.increaseTotalRequests(applicationName,invocation);
- defaultCollector.increaseSucceedRequests(applicationName,invocation);
-
defaultCollector.increaseUnknownFailedRequests(applicationName,invocation);
- defaultCollector.businessFailedRequests(applicationName,invocation);
+
+ 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);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
@@ -124,7 +128,12 @@ class AggregateMetricsCollectorTest {
@Test
void testRTMetrics() {
AggregateMetricsCollector collector = new
AggregateMetricsCollector(applicationModel);
-
defaultCollector.addRT(applicationModel.getApplicationName(),invocation, 10L);
+
+
defaultCollector.setApplicationName(applicationModel.getApplicationName());
+
+ MethodMetricsSampler methodMetricsCountSampler =
defaultCollector.getMethodSampler();
+
+ methodMetricsCountSampler.addRT(invocation, 10L);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
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 12d9571a37..8e07144215 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
@@ -19,6 +19,7 @@ package org.apache.dubbo.metrics.metrics.collector;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.RTEvent;
import org.apache.dubbo.metrics.event.RequestEvent;
@@ -87,11 +88,14 @@ class DefaultMetricsCollectorTest {
void testRequestsMetrics() {
DefaultMetricsCollector collector = new DefaultMetricsCollector();
collector.setCollectEnabled(true);
- String applicationName = applicationModel.getApplicationName();
- collector.increaseTotalRequests(applicationName, invocation);
- collector.increaseProcessingRequests(applicationName, invocation);
- collector.increaseSucceedRequests(applicationName, invocation);
- collector.increaseUnknownFailedRequests(applicationName, invocation);
+ collector.setApplicationName(applicationModel.getApplicationName());
+
+ 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);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
@@ -110,7 +114,7 @@ class DefaultMetricsCollectorTest {
Assertions.assertEquals(supplier.get().longValue(), 1);
}
- collector.decreaseProcessingRequests(applicationName, invocation);
+ methodMetricsCountSampler.dec(invocation,MetricsEvent.Type.PROCESSING);
samples = collector.collect();
List<MetricSample> samples1 = new ArrayList<>();
for (MetricSample sample : samples) {
@@ -131,9 +135,13 @@ class DefaultMetricsCollectorTest {
void testRTMetrics() {
DefaultMetricsCollector collector = new DefaultMetricsCollector();
collector.setCollectEnabled(true);
+ MethodMetricsSampler methodMetricsCountSampler =
collector.getMethodSampler();
String applicationName = applicationModel.getApplicationName();
- collector.addRT(applicationName, invocation, 10L);
- collector.addRT(applicationName, invocation, 0L);
+
+ collector.setApplicationName(applicationName);
+
+ methodMetricsCountSampler.addRT(invocation, 10L);
+ methodMetricsCountSampler.addRT(invocation, 0L);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
@@ -169,18 +177,19 @@ class DefaultMetricsCollectorTest {
@Test
void testListener() {
DefaultMetricsCollector collector = new DefaultMetricsCollector();
+ MethodMetricsSampler methodMetricsCountSampler =
collector.getMethodSampler();
collector.setCollectEnabled(true);
MockListener mockListener = new MockListener();
collector.addListener(mockListener);
- String applicationName = applicationModel.getApplicationName();
+ collector.setApplicationName(applicationModel.getApplicationName());
- collector.increaseTotalRequests(applicationName, invocation);
+
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);
- collector.addRT(applicationName, invocation, 5L);
+ methodMetricsCountSampler.addRT(invocation, 5L);
Assertions.assertTrue(mockListener.getCurEvent() instanceof RTEvent);
Assertions.assertEquals(((RTEvent)
mockListener.getCurEvent()).getRt(), 5L);
}