diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/filter/MetricsClusterFilterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/filter/MetricsClusterFilterTest.java index 629e3fc6f1c..a0c00adb2fe 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/filter/MetricsClusterFilterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/filter/MetricsClusterFilterTest.java @@ -80,7 +80,7 @@ public void setup() { collector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); if(!initApplication.get()) { - collector.collectApplication(applicationModel); + collector.collectApplication(); initApplication.set(true); } filter.setApplicationModel(applicationModel); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java index afd0bedfe53..f349e18d2f4 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/MetricsConstants.java @@ -28,6 +28,8 @@ public interface MetricsConstants { String TAG_APPLICATION_NAME = "application.name"; + String TAG_APPLICATION_MODULE = "application.module.id"; + String TAG_INTERFACE_KEY = "interface"; String TAG_METHOD_KEY = "method"; 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 faf81237172..ef6306eac97 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 @@ -391,7 +391,7 @@ private void initMetricsReporter() { metricsConfig.setProtocol(PROTOCOL_PROMETHEUS); } collector.setCollectEnabled(true); - collector.collectApplication(applicationModel); + collector.collectApplication(); collector.setThreadpoolCollectEnabled(Optional.ofNullable(metricsConfig.getEnableThreadpool()).orElse(true)); MetricsReporterFactory metricsReporterFactory = getExtensionLoader(MetricsReporterFactory.class).getAdaptiveExtension(); MetricsReporter metricsReporter = metricsReporterFactory.createMetricsReporter(metricsConfig.toUrl()); diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ApplicationMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ApplicationMetricsCollector.java index a9ac60df2e1..3f2427cf8c5 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ApplicationMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ApplicationMetricsCollector.java @@ -28,9 +28,9 @@ */ public interface ApplicationMetricsCollector extends MetricsCollector { - void increment(String applicationName, MetricsKey metricsKey); + void increment(MetricsKey metricsKey); - void addRt(String applicationName, String registryOpType, Long responseTime); + void addRt(String registryOpType, Long responseTime); } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java index 03f9478052e..d8c6e07e04c 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java @@ -46,36 +46,36 @@ protected void setEventMulticaster(MetricsEventMulticaster eventMulticaster) { } @Override - public void setNum(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num) { - this.stats.setServiceKey(metricsKey, applicationName, serviceKey, num); + public void setNum(MetricsKeyWrapper metricsKey, String serviceKey, int num) { + this.stats.setServiceKey(metricsKey, serviceKey, num); } @Override - public void increment(String applicationName, MetricsKey metricsKey) { - this.stats.incrementApp(metricsKey, applicationName, SELF_INCREMENT_SIZE); + public void increment(MetricsKey metricsKey) { + this.stats.incrementApp(metricsKey, SELF_INCREMENT_SIZE); } - public void increment(String applicationName, String serviceKey, MetricsKeyWrapper metricsKeyWrapper, int size) { - this.stats.incrementServiceKey(metricsKeyWrapper, applicationName, serviceKey, size); + public void increment(String serviceKey, MetricsKeyWrapper metricsKeyWrapper, int size) { + this.stats.incrementServiceKey(metricsKeyWrapper, serviceKey, size); } @Override - public void addRt(String applicationName, String registryOpType, Long responseTime) { - stats.calcApplicationRt(applicationName, registryOpType, responseTime); + public void addRt(String registryOpType, Long responseTime) { + stats.calcApplicationRt(registryOpType, responseTime); } - public void addRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) { - stats.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime); + public void addRt(String serviceKey, String registryOpType, Long responseTime) { + stats.calcServiceKeyRt(serviceKey, registryOpType, responseTime); } @Override - public void increment(String applicationName, Invocation invocation, MetricsKeyWrapper wrapper, int size) { - this.stats.incrementMethodKey(wrapper, applicationName, invocation, size); + public void increment(Invocation invocation, MetricsKeyWrapper wrapper, int size) { + this.stats.incrementMethodKey(wrapper, invocation, size); } @Override - public void addRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) { - stats.calcMethodKeyRt(applicationName, invocation, registryOpType, responseTime); + public void addRt(Invocation invocation, String registryOpType, Long responseTime) { + stats.calcMethodKeyRt(invocation, registryOpType, responseTime); } protected List export(MetricsCategory category) { diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java index 25732f3416e..836de4dcd32 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java @@ -26,8 +26,8 @@ */ public interface MethodMetricsCollector extends MetricsCollector { - void increment(String applicationName, Invocation invocation, MetricsKeyWrapper wrapper, int size); + void increment(Invocation invocation, MetricsKeyWrapper wrapper, int size); - void addRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime); + void addRt(Invocation invocation, String registryOpType, Long responseTime); } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java index af0aabc635c..37cd96ce829 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java @@ -26,10 +26,10 @@ */ public interface ServiceMetricsCollector extends MetricsCollector { - void increment(String applicationName, String serviceKey, MetricsKeyWrapper wrapper, int size); + void increment(String serviceKey, MetricsKeyWrapper wrapper, int size); - void setNum(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num); + void setNum(MetricsKeyWrapper metricsKey, String serviceKey, int num); - void addRt(String applicationName, String serviceKey, String registryOpType, Long responseTime); + void addRt(String serviceKey, String registryOpType, Long responseTime); } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java index 72b23eb234a..47a432e4804 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java @@ -23,7 +23,8 @@ import org.apache.dubbo.metrics.model.key.MetricsKey; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.apache.dubbo.metrics.report.MetricsExport; +import org.apache.dubbo.metrics.report.AbstractMetricsExport; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.List; @@ -31,42 +32,49 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -public class ApplicationStatComposite implements MetricsExport { +/** + * Application-level data container, for the initialized MetricsKey, + * different from the null value of the Map type (the key is not displayed when there is no data), + * the key is displayed and the initial data is 0 value of the AtomicLong type + */ +public class ApplicationStatComposite extends AbstractMetricsExport { + + + public ApplicationStatComposite(ApplicationModel applicationModel) { + super(applicationModel); + } - private final Map> applicationNumStats = new ConcurrentHashMap<>(); + private final Map applicationNumStats = new ConcurrentHashMap<>(); public void init(List appKeys) { if (CollectionUtils.isEmpty(appKeys)) { return; } - appKeys.forEach(appKey -> applicationNumStats.put(appKey, new ConcurrentHashMap<>())); + appKeys.forEach(appKey -> applicationNumStats.put(appKey, new AtomicLong(0L))); } - public void incrementSize(MetricsKey metricsKey, String applicationName, int size) { + public void incrementSize(MetricsKey metricsKey, int size) { if (!applicationNumStats.containsKey(metricsKey)) { return; } - applicationNumStats.get(metricsKey).computeIfAbsent(applicationName, k -> new AtomicLong(0L)).getAndAdd(size); + applicationNumStats.get(metricsKey).getAndAdd(size); } public List export(MetricsCategory category) { List list = new ArrayList<>(); for (MetricsKey type : applicationNumStats.keySet()) { - Map stringAtomicLongMap = applicationNumStats.get(type); - for (String applicationName : stringAtomicLongMap.keySet()) { - list.add(convertToSample(applicationName, type, category, stringAtomicLongMap.get(applicationName))); - } + list.add(convertToSample(type, category, applicationNumStats.get(type))); } return list; } @SuppressWarnings({"rawtypes"}) - private GaugeMetricSample convertToSample(String applicationName, MetricsKey type, MetricsCategory category, AtomicLong targetNumber) { - return new GaugeMetricSample<>(type, MetricsSupport.applicationTags(applicationName), category, targetNumber, AtomicLong::get); + private GaugeMetricSample convertToSample(MetricsKey type, MetricsCategory category, AtomicLong targetNumber) { + return new GaugeMetricSample<>(type, MetricsSupport.applicationTags(getApplicationModel()), category, targetNumber, AtomicLong::get); } - public Map> getApplicationNumStats() { + public Map getApplicationNumStats() { return applicationNumStats; } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java index 7f99f5e85c0..b4798b83efd 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java @@ -24,6 +24,7 @@ import org.apache.dubbo.metrics.model.sample.MetricSample; import org.apache.dubbo.metrics.report.MetricsExport; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.List; @@ -36,60 +37,63 @@ */ public abstract class BaseStatComposite implements MetricsExport { - private final ApplicationStatComposite applicationStatComposite = new ApplicationStatComposite(); - private final ServiceStatComposite serviceStatComposite = new ServiceStatComposite(); + private ApplicationStatComposite applicationStatComposite; + private ServiceStatComposite serviceStatComposite; - private final MethodStatComposite methodStatComposite = new MethodStatComposite(); - private final RtStatComposite rtStatComposite = new RtStatComposite(); + private MethodStatComposite methodStatComposite; + private RtStatComposite rtStatComposite; - public BaseStatComposite() { - init(applicationStatComposite); - init(serviceStatComposite); - init(methodStatComposite); - init(rtStatComposite); + public BaseStatComposite(ApplicationModel applicationModel) { + init(new ApplicationStatComposite(applicationModel)); + init(new ServiceStatComposite(applicationModel)); + init(new MethodStatComposite(applicationModel)); + init(new RtStatComposite(applicationModel)); } protected void init(ApplicationStatComposite applicationStatComposite) { + this.applicationStatComposite = applicationStatComposite; } protected void init(ServiceStatComposite serviceStatComposite) { - + this.serviceStatComposite = serviceStatComposite; } protected void init(MethodStatComposite methodStatComposite) { + this.methodStatComposite = methodStatComposite; } protected void init(RtStatComposite rtStatComposite) { + this.rtStatComposite = rtStatComposite; } - public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) { - rtStatComposite.calcApplicationRt(applicationName, registryOpType, responseTime); + public void calcApplicationRt(String registryOpType, Long responseTime) { + rtStatComposite.calcApplicationRt(registryOpType, responseTime); } - public void calcServiceKeyRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) { - rtStatComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime); + public void calcServiceKeyRt(String serviceKey, String registryOpType, Long responseTime) { + rtStatComposite.calcServiceKeyRt(serviceKey, registryOpType, responseTime); } - public void calcMethodKeyRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) { - rtStatComposite.calcMethodKeyRt(applicationName, invocation, registryOpType, responseTime); + public void calcMethodKeyRt(Invocation invocation, String registryOpType, Long responseTime) { + rtStatComposite.calcMethodKeyRt(invocation, registryOpType, responseTime); } - public void setServiceKey(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num) { - serviceStatComposite.setServiceKey(metricsKey, applicationName, serviceKey, num); + public void setServiceKey(MetricsKeyWrapper metricsKey, String serviceKey, int num) { + serviceStatComposite.setServiceKey(metricsKey, serviceKey, num); } - public void incrementApp(MetricsKey metricsKey, String applicationName, int size) { - applicationStatComposite.incrementSize(metricsKey, applicationName, size); + public void incrementApp(MetricsKey metricsKey, int size) { + applicationStatComposite.incrementSize(metricsKey, size); } - public void incrementServiceKey(MetricsKeyWrapper metricsKeyWrapper, String applicationName, String attServiceKey, int size) { - serviceStatComposite.incrementServiceKey(metricsKeyWrapper, applicationName, attServiceKey, size); + public void incrementServiceKey(MetricsKeyWrapper metricsKeyWrapper, String attServiceKey, int size) { + serviceStatComposite.incrementServiceKey(metricsKeyWrapper, attServiceKey, size); } - public void incrementMethodKey(MetricsKeyWrapper metricsKeyWrapper, String applicationName, Invocation invocation, int size) { - methodStatComposite.incrementMethodKey(metricsKeyWrapper, applicationName, invocation, size); + public void incrementMethodKey(MetricsKeyWrapper metricsKeyWrapper, Invocation invocation, int size) { + methodStatComposite.incrementMethodKey(metricsKeyWrapper, invocation, size); } @Override diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java index ad099221bbc..7b029e8f951 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java @@ -24,8 +24,9 @@ import org.apache.dubbo.metrics.model.sample.CounterMetricSample; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.apache.dubbo.metrics.report.MetricsExport; +import org.apache.dubbo.metrics.report.AbstractMetricsExport; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.List; @@ -33,8 +34,16 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -public class MethodStatComposite implements MetricsExport { +/** + * Method-level data container, + * if there is no actual call to the existing call method, + * the key will not be displayed when exporting (to be optimized) + */ +public class MethodStatComposite extends AbstractMetricsExport { + public MethodStatComposite(ApplicationModel applicationModel) { + super(applicationModel); + } private final Map> methodNumStats = new ConcurrentHashMap<>(); public void initWrapper(List metricsKeyWrappers) { @@ -44,11 +53,11 @@ public void initWrapper(List metricsKeyWrappers) { metricsKeyWrappers.forEach(appKey -> methodNumStats.put(appKey, new ConcurrentHashMap<>())); } - public void incrementMethodKey(MetricsKeyWrapper wrapper, String applicationName, Invocation invocation, int size) { + public void incrementMethodKey(MetricsKeyWrapper wrapper, Invocation invocation, int size) { if (!methodNumStats.containsKey(wrapper)) { return; } - methodNumStats.get(wrapper).computeIfAbsent(new MethodMetric(applicationName, invocation), k -> new AtomicLong(0L)).getAndAdd(size); + methodNumStats.get(wrapper).computeIfAbsent(new MethodMetric(getApplicationModel(), invocation), k -> new AtomicLong(0L)).getAndAdd(size); } public List export(MetricsCategory category) { diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java index 27ba8fe97a3..b49a2864c7d 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java @@ -19,16 +19,17 @@ import org.apache.dubbo.common.utils.ConcurrentHashMapUtils; import org.apache.dubbo.metrics.model.MetricsCategory; -import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper; import org.apache.dubbo.metrics.model.container.AtomicLongContainer; import org.apache.dubbo.metrics.model.container.LongAccumulatorContainer; import org.apache.dubbo.metrics.model.container.LongContainer; import org.apache.dubbo.metrics.model.key.MetricsKey; +import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper; import org.apache.dubbo.metrics.model.key.MetricsPlaceValue; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.apache.dubbo.metrics.report.MetricsExport; +import org.apache.dubbo.metrics.report.AbstractMetricsExport; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.Arrays; @@ -38,8 +39,17 @@ import java.util.concurrent.atomic.LongAccumulator; import java.util.stream.Collectors; +/** + * The data container of the rt dimension, including application, service, and method levels, + * if there is no actual call to the existing call method, + * the key will not be displayed when exporting (to be optimized) + */ @SuppressWarnings({"rawtypes", "unchecked"}) -public class RtStatComposite implements MetricsExport { +public class RtStatComposite extends AbstractMetricsExport { + + public RtStatComposite(ApplicationModel applicationModel) { + super(applicationModel); + } private final List> rtStats = new ArrayList<>(); @@ -68,23 +78,23 @@ private List> initStats(MetricsPlaceValue placeV return singleRtStats; } - public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) { + public void calcApplicationRt(String registryOpType, Long responseTime) { for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) { - Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName, container.getInitFunc()); + Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, getAppName(), container.getInitFunc()); container.getConsumerFunc().accept(responseTime, current); } } - public void calcServiceKeyRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) { + public void calcServiceKeyRt(String serviceKey, String registryOpType, Long responseTime) { for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) { - Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName + "_" + serviceKey, container.getInitFunc()); + Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, serviceKey, container.getInitFunc()); container.getConsumerFunc().accept(responseTime, current); } } - public void calcMethodKeyRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) { + public void calcMethodKeyRt(Invocation invocation, String registryOpType, Long responseTime) { for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) { - Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName + "_" + invocation.getServiceName() + "_" + invocation.getMethodName(), container.getInitFunc()); + Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, invocation.getServiceName() + "_" + invocation.getMethodName(), container.getInitFunc()); container.getConsumerFunc().accept(responseTime, current); } } @@ -94,7 +104,7 @@ public List export(MetricsCategory category) { for (LongContainer rtContainer : rtStats) { MetricsKeyWrapper metricsKeyWrapper = rtContainer.getMetricsKeyWrapper(); for (Map.Entry entry : rtContainer.entrySet()) { - list.add(new GaugeMetricSample<>(metricsKeyWrapper.targetKey(), metricsKeyWrapper.targetDesc(), metricsKeyWrapper.tagName(entry.getKey()), category, entry.getKey().intern(), value -> rtContainer.getValueSupplier().apply(value.intern()))); + list.add(new GaugeMetricSample<>(metricsKeyWrapper.targetKey(), metricsKeyWrapper.targetDesc(), metricsKeyWrapper.tagName(getApplicationModel(), entry.getKey()), category, entry.getKey().intern(), value -> rtContainer.getValueSupplier().apply(value.intern()))); } } return list; diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java index f1bfc32b1c4..ef635666362 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java @@ -23,7 +23,8 @@ import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.apache.dubbo.metrics.report.MetricsExport; +import org.apache.dubbo.metrics.report.AbstractMetricsExport; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.ArrayList; import java.util.List; @@ -31,7 +32,16 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; -public class ServiceStatComposite implements MetricsExport { +/** + * Service-level data container, for the initialized MetricsKey, + * different from the null value of the Map type (the key is not displayed when there is no data), + * the key is displayed and the initial data is 0 value of the AtomicLong type + */ +public class ServiceStatComposite extends AbstractMetricsExport { + + public ServiceStatComposite(ApplicationModel applicationModel) { + super(applicationModel); + } private final Map> serviceWrapperNumStats = new ConcurrentHashMap<>(); @@ -42,18 +52,18 @@ public void initWrapper(List metricsKeyWrappers) { metricsKeyWrappers.forEach(appKey -> serviceWrapperNumStats.put(appKey, new ConcurrentHashMap<>())); } - public void incrementServiceKey(MetricsKeyWrapper wrapper, String applicationName, String serviceKey, int size) { + public void incrementServiceKey(MetricsKeyWrapper wrapper, String serviceKey, int size) { if (!serviceWrapperNumStats.containsKey(wrapper)) { return; } - serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).getAndAdd(size); + serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(getApplicationModel(), serviceKey), k -> new AtomicLong(0L)).getAndAdd(size); } - public void setServiceKey(MetricsKeyWrapper wrapper, String applicationName, String serviceKey, int num) { + public void setServiceKey(MetricsKeyWrapper wrapper, String serviceKey, int num) { if (!serviceWrapperNumStats.containsKey(wrapper)) { return; } - serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).set(num); + serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(getApplicationModel(), serviceKey), k -> new AtomicLong(0L)).set(num); } public List export(MetricsCategory category) { diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java index 956b518c26d..27835e59eb6 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java @@ -21,7 +21,7 @@ import org.apache.dubbo.metrics.model.key.MetricsKey; import org.apache.dubbo.metrics.model.key.MetricsPlaceValue; -public class MetricsApplicationListener extends AbstractMetricsKeyListener { +public class MetricsApplicationListener extends AbstractMetricsKeyListener { public MetricsApplicationListener(MetricsKey metricsKey) { super(metricsKey); @@ -29,25 +29,25 @@ public MetricsApplicationListener(MetricsKey metricsKey) { public static AbstractMetricsKeyListener onPostEventBuild(MetricsKey metricsKey, CombMetricsCollector collector) { return AbstractMetricsKeyListener.onEvent(metricsKey, - event -> collector.increment(event.appName(), metricsKey) + event -> collector.increment(metricsKey) ); } public static AbstractMetricsKeyListener onFinishEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, CombMetricsCollector collector) { return AbstractMetricsKeyListener.onFinish(metricsKey, - event -> { - collector.increment(event.appName(), metricsKey); - collector.addRt(event.appName(), placeType.getType(), event.getTimePair().calc()); - } + event -> { + collector.increment(metricsKey); + collector.addRt(placeType.getType(), event.getTimePair().calc()); + } ); } public static AbstractMetricsKeyListener onErrorEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, CombMetricsCollector collector) { return AbstractMetricsKeyListener.onError(metricsKey, - event -> { - collector.increment(event.appName(), metricsKey); - collector.addRt(event.appName(), placeType.getType(), event.getTimePair().calc()); - } + event -> { + collector.increment(metricsKey); + collector.addRt(placeType.getType(), event.getTimePair().calc()); + } ); } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java index ed0da87e613..3a7cc9da7f3 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ApplicationMetric.java @@ -19,29 +19,33 @@ import org.apache.dubbo.common.Version; import org.apache.dubbo.metrics.model.key.MetricsKey; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.HashMap; import java.util.Map; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_VERSION_KEY; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; public class ApplicationMetric implements Metric { - private final String applicationName; + private final ApplicationModel applicationModel; private static final String version = Version.getVersion(); private static final String commitId = Version.getLastCommitId(); - public ApplicationMetric(String applicationName) { - this.applicationName = applicationName; + public ApplicationMetric(ApplicationModel applicationModel) { + this.applicationModel = applicationModel; + } + + public ApplicationModel getApplicationModel() { + return applicationModel; } public String getApplicationName() { - return applicationName; + return getApplicationModel().getApplicationName(); } public String getData() { @@ -50,23 +54,12 @@ public String getData() { @Override public Map getTags() { - return getTagsByName(this.getApplicationName()); - } - - public static Map getTagsByName(String applicationName) { Map tags = new HashMap<>(); tags.put(TAG_IP, getLocalHost()); tags.put(TAG_HOSTNAME, getLocalHostName()); - tags.put(TAG_APPLICATION_NAME, applicationName); + tags.put(TAG_APPLICATION_NAME, getApplicationName()); tags.put(TAG_APPLICATION_VERSION_KEY, version); tags.put(MetricsKey.METADATA_GIT_COMMITID_METRIC.getName(), commitId); return tags; } - - public static Map getServiceTags(String appAndServiceName) { - String[] keys = appAndServiceName.split("_"); - Map tags = getTagsByName(keys[0]); - tags.put(TAG_INTERFACE_KEY, keys[1]); - return tags; - } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java index 3ab9a911104..1985dae2286 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java @@ -19,71 +19,38 @@ import org.apache.dubbo.metrics.model.sample.MetricSample; import org.apache.dubbo.rpc.Invocation; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; -import java.util.HashMap; import java.util.Map; import java.util.Objects; -import java.util.Optional; -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR; -import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR; -import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; import static org.apache.dubbo.metrics.MetricsConstants.INVOCATION_METRICS_COUNTER; -import static org.apache.dubbo.rpc.support.RpcUtils.isGenericCall; /** * Metric class for method. */ -public class MethodMetric implements Metric { - private String applicationName; +public class MethodMetric extends ServiceKeyMetric { private String side; - private String interfaceName; - private String methodName; + private final String methodName; private String group; private String version; - - private MetricSample.Type sampleType; - - public MethodMetric() { - } - - public MethodMetric(String applicationName, Invocation invocation) { - this.applicationName = applicationName; + private final MetricSample.Type sampleType; + + public MethodMetric(ApplicationModel applicationModel, Invocation invocation) { + super(applicationModel, MetricsSupport.getInterfaceName(invocation)); + this.methodName = MetricsSupport.getMethodName(invocation); + this.side = MetricsSupport.getSide(invocation); + this.group = MetricsSupport.getGroup(invocation); + this.version = MetricsSupport.getVersion(invocation); this.sampleType = (MetricSample.Type) invocation.get(INVOCATION_METRICS_COUNTER); - init(invocation); } public MetricSample.Type getSampleType() { return sampleType; } - public String getInterfaceName() { - return interfaceName; - } - - public void setInterfaceName(String interfaceName) { - this.interfaceName = interfaceName; - } - - public String getMethodName() { - return methodName; - } - - public void setMethodName(String methodName) { - this.methodName = methodName; - } - public String getGroup() { return group; } @@ -101,52 +68,14 @@ public void setVersion(String version) { } public Map getTags() { - Map tags = new HashMap<>(); - tags.put(TAG_IP, getLocalHost()); - tags.put(TAG_HOSTNAME, getLocalHostName()); - tags.put(TAG_APPLICATION_NAME, applicationName); - tags.put(TAG_INTERFACE_KEY, interfaceName); - tags.put(TAG_METHOD_KEY, methodName); + Map tags = MetricsSupport.methodTags(getApplicationModel(), getInterfaceName(), methodName); tags.put(TAG_GROUP_KEY, group); tags.put(TAG_VERSION_KEY, version); return tags; } - private void init(Invocation invocation) { - String serviceUniqueName = invocation.getTargetServiceUniqueName(); - String methodName = invocation.getMethodName(); - if (invocation instanceof RpcInvocation - && isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName) - && invocation.getArguments() != null - && invocation.getArguments().length == 3) { - methodName = ((String) invocation.getArguments()[0]).trim(); - } - String group = null; - String interfaceAndVersion; - String[] arr = serviceUniqueName.split(PATH_SEPARATOR); - if (arr.length == 2) { - group = arr[0]; - interfaceAndVersion = arr[1]; - } else { - interfaceAndVersion = arr[0]; - } - String[] ivArr = interfaceAndVersion.split(GROUP_CHAR_SEPARATOR); - String interfaceName = ivArr[0]; - String version = ivArr.length == 2 ? ivArr[1] : null; - Optional> invoker = Optional.ofNullable(invocation.getInvoker()); - this.side = invoker.isPresent() ? invoker.get().getUrl().getSide() : PROVIDER_SIDE; - this.interfaceName = interfaceName; - this.methodName = methodName; - this.group = group; - this.version = version; - } - - public String getApplicationName() { - return applicationName; - } - - public void setApplicationName(String applicationName) { - this.applicationName = applicationName; + public String getMethodName() { + return methodName; } public String getSide() { @@ -160,9 +89,9 @@ public void setSide(String side) { @Override public String toString() { return "MethodMetric{" + - "applicationName='" + applicationName + '\'' + + "applicationName='" + getApplicationName() + '\'' + ", side='" + side + '\'' + - ", interfaceName='" + interfaceName + '\'' + + ", interfaceName='" + getInterfaceName() + '\'' + ", methodName='" + methodName + '\'' + ", group='" + group + '\'' + ", version='" + version + '\'' + @@ -174,11 +103,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MethodMetric that = (MethodMetric) o; - return Objects.equals(applicationName, that.applicationName) && Objects.equals(side, that.side) && Objects.equals(interfaceName, that.interfaceName) && Objects.equals(methodName, that.methodName) && Objects.equals(group, that.group) && Objects.equals(version, that.version); + return Objects.equals(getApplicationName(), that.getApplicationName()) && Objects.equals(side, that.side) && Objects.equals(getInterfaceName(), that.getInterfaceName()) && Objects.equals(methodName, that.methodName) && Objects.equals(group, that.group) && Objects.equals(version, that.version); } @Override public int hashCode() { - return Objects.hash(applicationName, side, interfaceName, methodName, group, version); + return Objects.hash(getApplicationName(), side, getInterfaceName(), methodName, group, version); } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java index 30ff4ea9a2e..dde1c958b46 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java @@ -29,6 +29,8 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.HashMap; import java.util.Map; @@ -37,6 +39,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; +import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_MODULE; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_VERSION_KEY; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; @@ -48,40 +51,42 @@ import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE; import static org.apache.dubbo.metrics.MetricsConstants.INVOCATION; import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE; +import static org.apache.dubbo.rpc.support.RpcUtils.isGenericCall; public class MetricsSupport { private static final String version = Version.getVersion(); private static final String commitId = Version.getLastCommitId(); - public static Map applicationTags(String applicationName) { + public static Map applicationTags(ApplicationModel applicationModel) { Map tags = new HashMap<>(); tags.put(TAG_IP, getLocalHost()); tags.put(TAG_HOSTNAME, getLocalHostName()); - tags.put(TAG_APPLICATION_NAME, applicationName); + tags.put(TAG_APPLICATION_NAME, applicationModel.getApplicationName()); + tags.put(TAG_APPLICATION_MODULE, applicationModel.getInternalId()); tags.put(TAG_APPLICATION_VERSION_KEY, version); tags.put(MetricsKey.METADATA_GIT_COMMITID_METRIC.getName(), commitId); return tags; } - public static Map serviceTags(String appAndServiceName) { - String[] keys = appAndServiceName.split("_"); - if (keys.length != 2) { - throw new MetricsNeverHappenException("Error service name: " + appAndServiceName); - } - Map tags = applicationTags(keys[0]); - tags.put(TAG_INTERFACE_KEY, keys[1]); + public static Map serviceTags(ApplicationModel applicationModel, String serviceKey) { + Map tags = applicationTags(applicationModel); + tags.put(TAG_INTERFACE_KEY, serviceKey); return tags; } - public static Map methodTags(String names) { + public static Map methodTags(ApplicationModel applicationModel, String names) { String[] keys = names.split("_"); - if (keys.length != 3) { + if (keys.length != 2) { throw new MetricsNeverHappenException("Error names: " + names); } - Map tags = applicationTags(keys[0]); - tags.put(TAG_INTERFACE_KEY, keys[1]); - tags.put(TAG_METHOD_KEY, keys[2]); + return methodTags(applicationModel, keys[0], keys[1]); + } + + public static Map methodTags(ApplicationModel applicationModel, String serviceKey, String methodName) { + Map tags = applicationTags(applicationModel); + tags.put(TAG_INTERFACE_KEY, serviceKey); + tags.put(TAG_METHOD_KEY, methodName); return tags; } @@ -150,47 +155,73 @@ public static String getInterfaceName(Invocation invocation) { return ivArr[0]; } - /** - * Incr service num - */ - public static void increment(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, MetricsEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); + public static String getMethodName(Invocation invocation) { + String methodName = invocation.getMethodName(); + if (invocation instanceof RpcInvocation + && isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName) + && invocation.getArguments() != null + && invocation.getArguments().length == 3) { + methodName = ((String) invocation.getArguments()[0]).trim(); + } + return methodName; + } + + public static String getGroup(Invocation invocation) { + String serviceUniqueName = invocation.getTargetServiceUniqueName(); + String group = null; + String[] arr = serviceUniqueName.split(PATH_SEPARATOR); + if (arr.length == 2) { + group = arr[0]; + } + return group; + } + + public static String getVersion(Invocation invocation) { + String interfaceAndVersion; + String[] arr = invocation.getTargetServiceUniqueName().split(PATH_SEPARATOR); + if (arr.length == 2) { + interfaceAndVersion = arr[1]; + } else { + interfaceAndVersion = arr[0]; + } + String[] ivArr = interfaceAndVersion.split(GROUP_CHAR_SEPARATOR); + return ivArr.length == 2 ? ivArr[1] : null; } /** - * Dec service num + * Incr service num */ - public static void dec(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, MetricsEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), -SELF_INCREMENT_SIZE); + public static void increment(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, MetricsEvent event) { + collector.increment(event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); } /** * Incr service num&&rt */ public static void incrAndAddRt(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, TimeCounterEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); - collector.addRt(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), placeType.getType(), event.getTimePair().calc()); + collector.increment(event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); + collector.addRt(event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), placeType.getType(), event.getTimePair().calc()); } /** * Incr method num */ public static void increment(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, MetricsEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); + collector.increment(event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); } /** * Dec method num */ public static void dec(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, MetricsEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), -SELF_INCREMENT_SIZE); + collector.increment(event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), -SELF_INCREMENT_SIZE); } /** * Incr method num&&rt */ public static void incrAndAddRt(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, TimeCounterEvent event) { - collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); - collector.addRt(event.appName(), event.getAttachmentValue(INVOCATION), placeType.getType(), event.getTimePair().calc()); + collector.increment(event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE); + collector.addRt(event.getAttachmentValue(INVOCATION), placeType.getType(), event.getTimePair().calc()); } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ServiceKeyMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ServiceKeyMetric.java index 94eaa30edbf..b380bdb77fb 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ServiceKeyMetric.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/ServiceKeyMetric.java @@ -17,36 +17,28 @@ package org.apache.dubbo.metrics.model; -import java.util.HashMap; -import java.util.Map; +import org.apache.dubbo.rpc.model.ApplicationModel; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; +import java.util.Map; /** * Metric class for service. */ -public class ServiceKeyMetric implements Metric { - private final String applicationName; - private final String serviceKey; +public class ServiceKeyMetric extends ApplicationMetric { + private final String interfaceName; - public ServiceKeyMetric(String applicationName, String serviceKey) { - this.applicationName = applicationName; - this.serviceKey = serviceKey; + public ServiceKeyMetric(ApplicationModel applicationModel, String serviceKey) { + super(applicationModel); + this.interfaceName = serviceKey; } @Override public Map getTags() { - Map tags = new HashMap<>(); - tags.put(TAG_IP, getLocalHost()); - tags.put(TAG_HOSTNAME, getLocalHostName()); - tags.put(TAG_APPLICATION_NAME, applicationName); - tags.put(TAG_INTERFACE_KEY, serviceKey); - return tags; + return MetricsSupport.serviceTags(getApplicationModel(), interfaceName); + } + + public String getInterfaceName() { + return interfaceName; } @Override @@ -60,24 +52,24 @@ public boolean equals(Object o) { ServiceKeyMetric that = (ServiceKeyMetric) o; - if (!applicationName.equals(that.applicationName)) { + if (!getApplicationName().equals(that.getApplicationName())) { return false; } - return serviceKey.equals(that.serviceKey); + return interfaceName.equals(that.interfaceName); } @Override public int hashCode() { - int result = applicationName.hashCode(); - result = 31 * result + serviceKey.hashCode(); + int result = getApplicationName().hashCode(); + result = 31 * result + interfaceName.hashCode(); return result; } @Override public String toString() { return "ServiceKeyMetric{" + - "applicationName='" + applicationName + '\'' + - ", serviceKey='" + serviceKey + '\'' + - '}'; + "applicationName='" + getApplicationName() + '\'' + + ", serviceKey='" + interfaceName + '\'' + + '}'; } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java index e213be48cbd..95d7a31d69e 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java @@ -19,6 +19,7 @@ import io.micrometer.common.lang.Nullable; import org.apache.dubbo.metrics.model.MetricsSupport; +import org.apache.dubbo.rpc.model.ApplicationModel; import java.util.Map; import java.util.Objects; @@ -89,17 +90,17 @@ public String targetDesc() { } } - public Map tagName(String key) { + public Map tagName(ApplicationModel applicationModel, String key) { MetricsLevel level = getLevel(); switch (level) { case APP: - return MetricsSupport.applicationTags(key); + return MetricsSupport.applicationTags(applicationModel); case SERVICE: - return MetricsSupport.serviceTags(key); + return MetricsSupport.serviceTags(applicationModel, key); case METHOD: - return MetricsSupport.methodTags(key); + return MetricsSupport.methodTags(applicationModel, key); } - return MetricsSupport.applicationTags(key); + return MetricsSupport.applicationTags(applicationModel); } public static MetricsKeyWrapper wrapper(MetricsKey metricsKey) { diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsExport.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsExport.java new file mode 100644 index 00000000000..94acf74de9c --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/AbstractMetricsExport.java @@ -0,0 +1,40 @@ +/* + * 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.report; + +import org.apache.dubbo.rpc.model.ApplicationModel; + +/** + * Store public information such as application + */ +public abstract class AbstractMetricsExport implements MetricsExport { + + private final ApplicationModel applicationModel; + + public AbstractMetricsExport(ApplicationModel applicationModel) { + this.applicationModel = applicationModel; + } + + public ApplicationModel getApplicationModel() { + return applicationModel; + } + + public String getAppName() { + return getApplicationModel().getApplicationName(); + } +} 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 f09654f36f3..960e19b1fd4 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 @@ -132,7 +132,7 @@ public void onEventError(RequestEvent event) { } private void onRTEvent(RequestEvent event) { - MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION)); + MethodMetric metric = new MethodMetric(applicationModel, event.getAttachmentValue(MetricsConstants.INVOCATION)); long responseTime = event.getTimePair().calc(); TimeWindowQuantile quantile = ConcurrentHashMapUtils.computeIfAbsent(rt, metric, k -> new TimeWindowQuantile(DEFAULT_COMPRESSION, bucketNum, timeWindowSeconds)); quantile.add(responseTime); @@ -142,7 +142,7 @@ private void onRTEvent(RequestEvent event) { private MethodMetric calcWindowCounter(RequestEvent event, MetricsKey targetKey) { MetricsPlaceValue placeType = MetricsPlaceValue.of(event.getAttachmentValue(MetricsConstants.INVOCATION_SIDE), MetricsLevel.SERVICE); MetricsKeyWrapper metricsKeyWrapper = new MetricsKeyWrapper(targetKey, placeType); - MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION)); + MethodMetric metric = new MethodMetric(applicationModel, event.getAttachmentValue(MetricsConstants.INVOCATION)); ConcurrentMap counter = methodTypeCounter.computeIfAbsent(metricsKeyWrapper, k -> new ConcurrentHashMap<>()); 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 937893310ce..af3f7bcfe6d 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 @@ -56,18 +56,20 @@ public class DefaultMetricsCollector extends CombMetricsCollector private volatile boolean threadpoolCollectEnabled = false; private final ThreadPoolMetricsSampler threadPoolSampler = new ThreadPoolMetricsSampler(this); private String applicationName; - private ApplicationModel applicationModel; + private final ApplicationModel applicationModel; private final List samplers = new ArrayList<>(); - public DefaultMetricsCollector() { - super(new BaseStatComposite() { + public DefaultMetricsCollector(ApplicationModel applicationModel) { + super(new BaseStatComposite(applicationModel) { @Override protected void init(MethodStatComposite methodStatComposite) { + super.init(methodStatComposite); methodStatComposite.initWrapper(DefaultConstants.METHOD_LEVEL_KEYS); } @Override protected void init(RtStatComposite rtStatComposite) { + super.init(rtStatComposite); rtStatComposite.init(MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD), MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)); } @@ -75,6 +77,7 @@ protected void init(RtStatComposite rtStatComposite) { super.setEventMulticaster(new DefaultSubDispatcher(this)); samplers.add(applicationSampler); samplers.add(threadPoolSampler); + this.applicationModel = applicationModel; } public void addSampler(MetricsSampler sampler) { @@ -109,9 +112,8 @@ public void setThreadpoolCollectEnabled(boolean threadpoolCollectEnabled) { this.threadpoolCollectEnabled = threadpoolCollectEnabled; } - public void collectApplication(ApplicationModel applicationModel) { + public void collectApplication() { this.setApplicationName(applicationModel.getApplicationName()); - this.applicationModel = applicationModel; applicationSampler.inc(applicationName, MetricsEvent.Type.APPLICATION_INFO); } @@ -155,7 +157,7 @@ public List sample() { @Override protected void countConfigure( MetricsCountSampleConfigurer sampleConfigure) { - sampleConfigure.configureMetrics(configure -> new ApplicationMetric(sampleConfigure.getSource())); + sampleConfigure.configureMetrics(configure -> new ApplicationMetric(applicationModel)); } }; } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java index 3a17033a975..c7a152c12e3 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java @@ -86,7 +86,7 @@ public void onEventError(RequestEvent event) { private void onRTEvent(RequestEvent event) { if (metricRegister != null) { - MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION)); + MethodMetric metric = new MethodMetric(applicationModel, event.getAttachmentValue(MetricsConstants.INVOCATION)); long responseTime = event.getTimePair().calc(); HistogramMetricSample sample = new HistogramMetricSample(MetricsKey.METRIC_RT_HISTOGRAM.getNameByType(metric.getSide()), 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 ff6e7a75827..b8daaef3680 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 @@ -86,12 +86,9 @@ class AggregateMetricsCollectorTest { private AggregateMetricsCollector collector; private MetricsFilter metricsFilter; - public static MethodMetric getTestMethodMetric() { + public MethodMetric getTestMethodMetric() { - MethodMetric methodMetric = new MethodMetric(); - methodMetric.setApplicationName("TestApp"); - methodMetric.setInterfaceName("TestInterface"); - methodMetric.setMethodName("TestMethod"); + MethodMetric methodMetric = new MethodMetric(applicationModel, invocation); methodMetric.setGroup("TestGroup"); methodMetric.setVersion("1.0.0"); methodMetric.setSide("PROVIDER"); @@ -118,7 +115,7 @@ public void setup() { collector = applicationModel.getBeanFactory().getOrRegisterBean(AggregateMetricsCollector.class); collector.setCollectEnabled(true); - defaultCollector = new DefaultMetricsCollector(); + defaultCollector = new DefaultMetricsCollector(applicationModel); defaultCollector.setCollectEnabled(true); metricsFilter = new MetricsFilter(); @@ -171,7 +168,6 @@ void testRequestsMetrics() { metricsFilter.onResponse(result, new TestMetricsInvoker(side), invocation); - List samples = collector.collect(); for (MetricSample sample : samples) { Map tags = sample.getTags(); @@ -201,7 +197,7 @@ public void testQPS() { when(applicationModel.getApplicationConfigManager()).thenReturn(configManager); when(applicationModel.getBeanFactory()).thenReturn(beanFactory); - when(beanFactory.getBean(DefaultMetricsCollector.class)).thenReturn(new DefaultMetricsCollector()); + when(beanFactory.getBean(DefaultMetricsCollector.class)).thenReturn(new DefaultMetricsCollector(applicationModel)); when(configManager.getMetrics()).thenReturn(Optional.of(metricsConfig)); when(metricsConfig.getAggregation()).thenReturn(aggregationConfig); when(aggregationConfig.getEnabled()).thenReturn(Boolean.TRUE); @@ -271,16 +267,16 @@ void testP95AndP99() throws InterruptedException { List samples = collector.collect(); GaugeMetricSample p95Sample = samples.stream() - .filter(sample -> sample.getName().endsWith("p95")) - .map(sample -> (GaugeMetricSample) sample) - .findFirst() - .orElse(null); + .filter(sample -> sample.getName().endsWith("p95")) + .map(sample -> (GaugeMetricSample) sample) + .findFirst() + .orElse(null); GaugeMetricSample p99Sample = samples.stream() - .filter(sample -> sample.getName().endsWith("p99")) - .map(sample -> (GaugeMetricSample) sample) - .findFirst() - .orElse(null); + .filter(sample -> sample.getName().endsWith("p99")) + .map(sample -> (GaugeMetricSample) sample) + .findFirst() + .orElse(null); Assertions.assertNotNull(p95Sample); Assertions.assertNotNull(p99Sample); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/DefaultCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/DefaultCollectorTest.java index 6451214e551..9beb258115b 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/DefaultCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/DefaultCollectorTest.java @@ -110,7 +110,7 @@ public void setup() { @Test void testListener() { - DefaultMetricsCollector metricsCollector = new DefaultMetricsCollector(); + DefaultMetricsCollector metricsCollector = new DefaultMetricsCollector(applicationModel); RequestEvent event = RequestEvent.toRequestEvent(applicationModel, invocation); RequestBeforeEvent beforeEvent = new RequestBeforeEvent(applicationModel, new TypeWrapper(MetricsLevel.METHOD, MetricsKey.METRIC_REQUESTS)); Assertions.assertTrue(metricsCollector.isSupport(event)); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java index 4b336d91f64..2c8e2f984d8 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java @@ -50,7 +50,7 @@ public class ThreadPoolMetricsSamplerTest { @BeforeEach void setUp() { - DefaultMetricsCollector collector = new DefaultMetricsCollector(); + DefaultMetricsCollector collector = new DefaultMetricsCollector(applicationModel); sampler = new ThreadPoolMetricsSampler(collector); } @@ -135,12 +135,12 @@ void testSample() { public void setUp2() { MockitoAnnotations.openMocks(this); - collector = new DefaultMetricsCollector(); + collector = new DefaultMetricsCollector(applicationModel); sampler2 = new ThreadPoolMetricsSampler(collector); when(scopeBeanFactory.getBean(FrameworkExecutorRepository.class)).thenReturn(new FrameworkExecutorRepository()); - collector.collectApplication(applicationModel); + collector.collectApplication(); when(applicationModel.getBeanFactory()).thenReturn(scopeBeanFactory); when(applicationModel.getExtensionLoader(DataStore.class)).thenReturn(extensionLoader); when(extensionLoader.getDefaultExtension()).thenReturn(dataStore); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java index b0d06dc16b4..887b7855552 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java @@ -77,8 +77,6 @@ class MetricsFilterTest { public void setup() { ApplicationConfig config = new ApplicationConfig(); config.setName("MockMetrics"); - //RpcContext.getContext().setAttachment("MockMetrics","MockMetrics"); - applicationModel = ApplicationModel.defaultModel(); applicationModel.getApplicationConfigManager().setApplication(config); @@ -87,7 +85,7 @@ public void setup() { collector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); if (!initApplication.get()) { - collector.collectApplication(applicationModel); + collector.collectApplication(); initApplication.set(true); } filter.setApplicationModel(applicationModel); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java index 6266a6860eb..2b993468166 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/model/MethodMetricTest.java @@ -18,9 +18,11 @@ package org.apache.dubbo.metrics.metrics.model; import org.apache.dubbo.common.URL; +import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.metrics.model.MethodMetric; import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.model.ApplicationModel; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -41,7 +43,7 @@ class MethodMetricTest { - private static final String applicationName = null; + private static ApplicationModel applicationModel; private static String interfaceName; private static String methodName; private static String group; @@ -50,6 +52,12 @@ class MethodMetricTest { @BeforeAll public static void setup() { + + ApplicationConfig config = new ApplicationConfig(); + config.setName("MockMetrics"); + applicationModel = ApplicationModel.defaultModel(); + applicationModel.getApplicationConfigManager().setApplication(config); + interfaceName = "org.apache.dubbo.MockInterface"; methodName = "mockMethod"; group = "mockGroup"; @@ -64,7 +72,7 @@ public static void setup() { @Test void test() { - MethodMetric metric = new MethodMetric(applicationName, invocation); + MethodMetric metric = new MethodMetric(applicationModel, invocation); Assertions.assertEquals(metric.getInterfaceName(), interfaceName); Assertions.assertEquals(metric.getMethodName(), methodName); Assertions.assertEquals(metric.getGroup(), group); @@ -73,7 +81,7 @@ void test() { Map tags = metric.getTags(); Assertions.assertEquals(tags.get(TAG_IP), getLocalHost()); Assertions.assertEquals(tags.get(TAG_HOSTNAME), getLocalHostName()); - Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME), applicationName); + Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME), applicationModel.getApplicationName()); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), interfaceName); Assertions.assertEquals(tags.get(TAG_METHOD_KEY), methodName); diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java index 6281efebc40..f3127f1ab4a 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java @@ -51,19 +51,22 @@ public class MetadataMetricsCollector extends CombMetricsCollector metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); - Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.METADATA_PUSH_METRIC_NUM.getName()); + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size(), metricSamples.size()); + Assertions.assertTrue(metricSamples.stream().allMatch(metricSample -> metricSample instanceof GaugeMetricSample)); + Assertions.assertTrue(metricSamples.stream().anyMatch(metricSample -> ((GaugeMetricSample) metricSample).applyAsDouble() == 1)); return null; } ); // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // App(6) + rt(5) = 7 + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); long c1 = pushEvent.getTimePair().calc(); pushEvent = MetadataEvent.toPushEvent(applicationModel); @@ -121,8 +121,8 @@ void testPushMetrics() { long c2 = lastTimePair.calc(); metricSamples = collector.collect(); - // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + // App(6) + rt(5) + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -150,9 +150,9 @@ void testSubscribeMetrics() { List metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); - Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM.getName()); + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size(), metricSamples.size()); + Assertions.assertTrue(metricSamples.stream().allMatch(metricSample -> metricSample instanceof GaugeMetricSample)); + Assertions.assertTrue(metricSamples.stream().anyMatch(metricSample -> ((GaugeMetricSample) metricSample).applyAsDouble() == 1)); return null; } ); @@ -160,8 +160,9 @@ void testSubscribeMetrics() { // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // App(6) + rt(5) = 7 + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); + subscribeEvent = MetadataEvent.toSubscribeEvent(applicationModel); TimePair lastTimePair = subscribeEvent.getTimePair(); MetricsEventBus.post(subscribeEvent, @@ -179,8 +180,8 @@ void testSubscribeMetrics() { long c2 = lastTimePair.calc(); metricSamples = collector.collect(); - // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + // App(6) + rt(5) + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -209,19 +210,19 @@ void testStoreProviderMetadataMetrics() { () -> { List metricSamples = collector.collect(); - // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); - Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.STORE_PROVIDER_METADATA.getName()); - Assertions.assertEquals(metricSamples.get(0).getTags().get("interface"), serviceKey); + // App(6) + service success(1) + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 1, metricSamples.size()); + Assertions.assertTrue(metricSamples.stream().allMatch(metricSample -> metricSample instanceof GaugeMetricSample)); + Assertions.assertTrue(metricSamples.stream().anyMatch(metricSample -> ((GaugeMetricSample) metricSample).applyAsDouble() == 1)); return null; } ); // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // App(6) + service total/success(2) + rt(5) = 7 + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() + 2 + 5, metricSamples.size()); + long c1 = metadataEvent.getTimePair().calc(); metadataEvent = MetadataEvent.toServiceSubscribeEvent(applicationModel, serviceKey); TimePair lastTimePair = metadataEvent.getTimePair(); @@ -240,8 +241,8 @@ void testStoreProviderMetadataMetrics() { metricSamples = collector.collect(); - // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + // App(6) + service total/success/failed(3) + rt(5) + Assertions.assertEquals(MetadataMetricsConstants.APP_LEVEL_KEYS.size() +3 + 5, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java index bf1a021d637..0a1cd611ffa 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java @@ -17,43 +17,58 @@ package org.apache.dubbo.metrics.metadata; +import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.metrics.data.ApplicationStatComposite; import org.apache.dubbo.metrics.data.BaseStatComposite; import org.apache.dubbo.metrics.data.RtStatComposite; import org.apache.dubbo.metrics.data.ServiceStatComposite; import org.apache.dubbo.metrics.model.container.LongContainer; import org.apache.dubbo.metrics.model.key.MetricsKey; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_PUSH; import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_STORE_PROVIDER_INTERFACE; import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_SUBSCRIBE; public class MetadataStatCompositeTest { + private ApplicationModel applicationModel; + private BaseStatComposite statComposite; - private final String applicationName = "app1"; - - private final BaseStatComposite statComposite = new BaseStatComposite() { - @Override - protected void init(ApplicationStatComposite applicationStatComposite) { - applicationStatComposite.init(MetadataMetricsConstants.APP_LEVEL_KEYS); - } + @BeforeEach + public void setup() { + FrameworkModel frameworkModel = FrameworkModel.defaultModel(); + applicationModel = frameworkModel.newApplication(); + ApplicationConfig application = new ApplicationConfig(); + application.setName("App1"); + applicationModel.getApplicationConfigManager().setApplication(application); + statComposite = new BaseStatComposite(applicationModel) { + @Override + protected void init(ApplicationStatComposite applicationStatComposite) { + super.init(applicationStatComposite); + applicationStatComposite.init(MetadataMetricsConstants.APP_LEVEL_KEYS); + } - @Override - protected void init(ServiceStatComposite serviceStatComposite) { - serviceStatComposite.initWrapper(MetadataMetricsConstants.SERVICE_LEVEL_KEYS); - } + @Override + protected void init(ServiceStatComposite serviceStatComposite) { + super.init(serviceStatComposite); + serviceStatComposite.initWrapper(MetadataMetricsConstants.SERVICE_LEVEL_KEYS); + } - @Override - protected void init(RtStatComposite rtStatComposite) { - rtStatComposite.init(OP_TYPE_PUSH, OP_TYPE_SUBSCRIBE, OP_TYPE_STORE_PROVIDER_INTERFACE); - } - }; + @Override + protected void init(RtStatComposite rtStatComposite) { + super.init(rtStatComposite); + rtStatComposite.init(OP_TYPE_PUSH, OP_TYPE_SUBSCRIBE, OP_TYPE_STORE_PROVIDER_INTERFACE); + } + }; + } @Test void testInit() { @@ -62,7 +77,7 @@ void testInit() { //(rt)5 * (push,subscribe,service)3 Assertions.assertEquals(5 * 3, statComposite.getRtStatComposite().getRtStats().size()); statComposite.getApplicationStatComposite().getApplicationNumStats().values().forEach((v -> - Assertions.assertEquals(v, new ConcurrentHashMap<>()))); + Assertions.assertEquals(v.get(), new AtomicLong(0L).get()))); statComposite.getRtStatComposite().getRtStats().forEach(rtContainer -> { for (Map.Entry entry : rtContainer.entrySet()) { @@ -73,16 +88,16 @@ void testInit() { @Test void testIncrement() { - statComposite.incrementApp(MetricsKey.METADATA_PUSH_METRIC_NUM, applicationName, 1); + statComposite.incrementApp(MetricsKey.METADATA_PUSH_METRIC_NUM, 1); - Assertions.assertEquals(1L, statComposite.getApplicationStatComposite().getApplicationNumStats().get(MetricsKey.METADATA_PUSH_METRIC_NUM).get(applicationName).get()); + Assertions.assertEquals(1L, statComposite.getApplicationStatComposite().getApplicationNumStats().get(MetricsKey.METADATA_PUSH_METRIC_NUM).get()); } @Test void testCalcRt() { - statComposite.calcApplicationRt(applicationName, OP_TYPE_SUBSCRIBE.getType(), 10L); + statComposite.calcApplicationRt( OP_TYPE_SUBSCRIBE.getType(), 10L); Assertions.assertTrue(statComposite.getRtStatComposite().getRtStats().stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE.getType()))); Optional> subContainer = statComposite.getRtStatComposite().getRtStats().stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE.getType())).findFirst(); - subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationName).longValue())); + subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationModel.getApplicationName()).longValue())); } } diff --git a/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsThreadPoolTest.java b/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsThreadPoolTest.java index 05ab7b1e8a3..6c73838ef14 100644 --- a/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsThreadPoolTest.java +++ b/dubbo-metrics/dubbo-metrics-prometheus/src/test/java/org/apache/dubbo/metrics/prometheus/PrometheusMetricsThreadPoolTest.java @@ -15,6 +15,7 @@ * limitations under the License. */ package org.apache.dubbo.metrics.prometheus; + import com.sun.net.httpserver.HttpServer; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.MetricsConfig; @@ -24,7 +25,6 @@ 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.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -33,6 +33,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -43,19 +44,18 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; + import static org.apache.dubbo.common.constants.MetricsConstants.PROTOCOL_PROMETHEUS; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_THREAD_NAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; +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 PrometheusMetricsThreadPoolTest { - private FrameworkModel frameworkModel; - private ApplicationModel applicationModel; private MetricsConfig metricsConfig; @@ -71,8 +71,7 @@ public void setup() { applicationModel.getApplicationConfigManager().setApplication(config); metricsConfig = new MetricsConfig(); metricsConfig.setProtocol(PROTOCOL_PROMETHEUS); - frameworkModel = FrameworkModel.defaultModel(); - metricsCollector = frameworkModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); + metricsCollector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); } @AfterEach @@ -92,7 +91,7 @@ void testExporterThreadpoolName() { metricsConfig.setEnableJvm(false); metricsCollector.setCollectEnabled(true); metricsConfig.setEnableThreadpool(true); - metricsCollector.collectApplication(applicationModel); + metricsCollector.collectApplication(); PrometheusMetricsReporter reporter = new PrometheusMetricsReporter(metricsConfig.toUrl(), applicationModel); reporter.init(); exportHttpServer(reporter,port); @@ -142,7 +141,7 @@ private void exportHttpServer(PrometheusMetricsReporter reporter, int port) { @Test @SuppressWarnings("rawtypes") void testThreadPoolRejectMetrics() { - DefaultMetricsCollector collector = new DefaultMetricsCollector(); + DefaultMetricsCollector collector = new DefaultMetricsCollector(applicationModel); collector.setCollectEnabled(true); collector.setApplicationName(applicationModel.getApplicationName()); String threadPoolExecutorName="DubboServerHandler-20816"; diff --git a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java index eddcaf1af33..b18ed313f71 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java @@ -53,19 +53,22 @@ public class RegistryMetricsCollector extends CombMetricsCollector lastNumMap = Collections.unmodifiableMap(event.getAttachmentValue(ATTACHMENT_KEY_LAST_NUM_MAP)); lastNumMap.forEach( - (k, v) -> collector.setNum(new MetricsKeyWrapper(key, OP_TYPE_NOTIFY), event.appName(), k, v)); + (k, v) -> collector.setNum(new MetricsKeyWrapper(key, OP_TYPE_NOTIFY), k, v)); } )); @@ -101,7 +101,7 @@ interface MCat { Map> summaryMap = event.getAttachmentValue(ATTACHMENT_DIRECTORY_MAP); summaryMap.forEach((metricsKey, map) -> map.forEach( - (k, v) -> collector.setNum(new MetricsKeyWrapper(key, OP_TYPE_DIRECTORY), event.appName(), k, v))); + (k, v) -> collector.setNum(new MetricsKeyWrapper(key, OP_TYPE_DIRECTORY), k, v))); } )); diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java index 2c042c91148..3dc6dad7d30 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java @@ -25,6 +25,7 @@ import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; +import org.apache.dubbo.metrics.registry.RegistryMetricsConstants; import org.apache.dubbo.metrics.registry.collector.RegistryMetricsCollector; import org.apache.dubbo.metrics.registry.event.RegistryEvent; import org.apache.dubbo.rpc.model.ApplicationModel; @@ -76,17 +77,18 @@ void testRegisterMetrics() { MetricsEventBus.post(registryEvent, () -> { List metricSamples = collector.collect(); - // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); + // push success +1 -> other default 0 = RegistryMetricsConstants.APP_LEVEL_KEYS.size() + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size(), metricSamples.size()); + Assertions.assertTrue(metricSamples.stream().allMatch(metricSample -> metricSample instanceof GaugeMetricSample)); + Assertions.assertTrue(metricSamples.stream().anyMatch(metricSample -> ((GaugeMetricSample) metricSample).applyAsDouble() == 1)); return null; } ); // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // RegistryMetricsConstants.APP_LEVEL_KEYS.size() + rt(5) = 12 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); long c1 = registryEvent.getTimePair().calc(); @@ -108,7 +110,7 @@ void testRegisterMetrics() { metricSamples = collector.collect(); // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -138,18 +140,17 @@ void testServicePushMetrics() { List metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); - Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.SERVICE_REGISTER_METRIC_REQUESTS.getName()); - Assertions.assertEquals(metricSamples.get(0).getTags().get("interface"), serviceName); + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 1, metricSamples.size()); + // Service num only 1 and contains tag of interface + Assertions.assertEquals(1, metricSamples.stream().filter(metricSample -> serviceName.equals(metricSample.getTags().get("interface"))).count()); return null; } ); // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // App(7) + rt(5) + service(total/success) = 14 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5 + 2, metricSamples.size()); long c1 = registryEvent.getTimePair().calc(); registryEvent = RegistryEvent.toRsEvent(applicationModel, serviceName, 2); @@ -169,8 +170,8 @@ void testServicePushMetrics() { metricSamples = collector.collect(); - // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + // App(7) + rt(5) + service(total/success/failed) = 15 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5 + 3, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -198,20 +199,20 @@ void testServiceSubscribeMetrics() { MetricsEventBus.post(subscribeEvent, () -> { List metricSamples = collector.collect(); - - // push success +1 - Assertions.assertEquals(1, metricSamples.size()); - Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); - Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.SERVICE_SUBSCRIBE_METRIC_NUM.getName()); - Assertions.assertEquals(metricSamples.get(0).getTags().get("interface"), serviceName); + Assertions.assertTrue(metricSamples.stream().allMatch(metricSample -> metricSample instanceof GaugeMetricSample)); + Assertions.assertTrue(metricSamples.stream().anyMatch(metricSample -> ((GaugeMetricSample) metricSample).applyAsDouble() == 1)); + // App(7) + (service success +1) + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 1, metricSamples.size()); + // Service num only 1 and contains tag of interface + Assertions.assertEquals(1, metricSamples.stream().filter(metricSample -> serviceName.equals(metricSample.getTags().get("interface"))).count()); return null; } ); // push finish rt +1 List metricSamples = collector.collect(); - //num(total+success) + rt(5) = 7 - Assertions.assertEquals(7, metricSamples.size()); + // App(7) + rt(5) + service(total/success) = 14 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5 + 2, metricSamples.size()); long c1 = subscribeEvent.getTimePair().calc(); subscribeEvent = RegistryEvent.toSsEvent(applicationModel, serviceName); @@ -231,8 +232,8 @@ void testServiceSubscribeMetrics() { metricSamples = collector.collect(); - // num(total+success+error) + rt(5) - Assertions.assertEquals(8, metricSamples.size()); + // App(7) + rt(5) + service(total/success/failed) = 15 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 5 + 3, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -269,8 +270,8 @@ public void testNotify() { } ); List metricSamples = collector.collect(); - // num(total+service*3) + rt(5) = 9 - Assertions.assertEquals(9, metricSamples.size()); + // App(7) + num(service*3) + rt(5) = 9 + Assertions.assertEquals(RegistryMetricsConstants.APP_LEVEL_KEYS.size() + 3 + 5, metricSamples.size()); } } diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java index 579a859ffe9..73d58159483 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsSampleTest.java @@ -87,8 +87,7 @@ void testRTMetrics() { void testListener() { RegistryMetricsCollector collector = new RegistryMetricsCollector(applicationModel); collector.setCollectEnabled(true); - String applicationName = applicationModel.getApplicationName(); - collector.increment(applicationName, MetricsKey.REGISTER_METRIC_REQUESTS); + collector.increment(MetricsKey.REGISTER_METRIC_REQUESTS); } } diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java index ebd4c599e65..193d0403a18 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java @@ -17,6 +17,7 @@ package org.apache.dubbo.metrics.registry.metrics.collector; +import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.metrics.data.ApplicationStatComposite; import org.apache.dubbo.metrics.data.BaseStatComposite; import org.apache.dubbo.metrics.data.RtStatComposite; @@ -26,13 +27,16 @@ import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; import org.apache.dubbo.metrics.registry.RegistryMetricsConstants; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_RT_AVG; import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_RT_MAX; @@ -46,24 +50,38 @@ public class RegistryStatCompositeTest { + private ApplicationModel applicationModel; + private String applicationName; + private BaseStatComposite statComposite; + + @BeforeEach + public void setup() { + FrameworkModel frameworkModel = FrameworkModel.defaultModel(); + applicationModel = frameworkModel.newApplication(); + ApplicationConfig application = new ApplicationConfig(); + application.setName("App1"); + applicationModel.getApplicationConfigManager().setApplication(application); + applicationName = applicationModel.getApplicationName(); + statComposite = new BaseStatComposite(applicationModel) { + @Override + protected void init(ApplicationStatComposite applicationStatComposite) { + super.init(applicationStatComposite); + applicationStatComposite.init(RegistryMetricsConstants.APP_LEVEL_KEYS); + } - private final String applicationName = "app1"; - private final BaseStatComposite statComposite = new BaseStatComposite() { - @Override - protected void init(ApplicationStatComposite applicationStatComposite) { - applicationStatComposite.init(RegistryMetricsConstants.APP_LEVEL_KEYS); - } - - @Override - protected void init(ServiceStatComposite serviceStatComposite) { - serviceStatComposite.initWrapper(RegistryMetricsConstants.SERVICE_LEVEL_KEYS); - } + @Override + protected void init(ServiceStatComposite serviceStatComposite) { + super.init(serviceStatComposite); + serviceStatComposite.initWrapper(RegistryMetricsConstants.SERVICE_LEVEL_KEYS); + } - @Override - protected void init(RtStatComposite rtStatComposite) { - rtStatComposite.init(OP_TYPE_REGISTER, OP_TYPE_SUBSCRIBE, OP_TYPE_NOTIFY, OP_TYPE_REGISTER_SERVICE, OP_TYPE_SUBSCRIBE_SERVICE); - } - }; + @Override + protected void init(RtStatComposite rtStatComposite) { + super.init(rtStatComposite); + rtStatComposite.init(OP_TYPE_REGISTER, OP_TYPE_SUBSCRIBE, OP_TYPE_NOTIFY, OP_TYPE_REGISTER_SERVICE, OP_TYPE_SUBSCRIBE_SERVICE); + } + }; + } @Test void testInit() { @@ -71,7 +89,7 @@ void testInit() { //(rt)5 * (applicationRegister,subscribe,notify,applicationRegister.service,subscribe.service) Assertions.assertEquals(5 * 5, statComposite.getRtStatComposite().getRtStats().size()); statComposite.getApplicationStatComposite().getApplicationNumStats().values().forEach((v -> - Assertions.assertEquals(v, new ConcurrentHashMap<>()))); + Assertions.assertEquals(v.get(), new AtomicLong(0L).get()))); statComposite.getRtStatComposite().getRtStats().forEach(rtContainer -> { for (Map.Entry entry : rtContainer.entrySet()) { @@ -82,13 +100,13 @@ void testInit() { @Test void testIncrement() { - statComposite.incrementApp(REGISTER_METRIC_REQUESTS, applicationName, 1); - Assertions.assertEquals(1L, statComposite.getApplicationStatComposite().getApplicationNumStats().get(REGISTER_METRIC_REQUESTS).get(applicationName).get()); + statComposite.incrementApp(REGISTER_METRIC_REQUESTS, 1); + Assertions.assertEquals(1L, statComposite.getApplicationStatComposite().getApplicationNumStats().get(REGISTER_METRIC_REQUESTS).get()); } @Test void testCalcRt() { - statComposite.calcApplicationRt(applicationName, OP_TYPE_NOTIFY.getType(), 10L); + statComposite.calcApplicationRt(OP_TYPE_NOTIFY.getType(), 10L); Assertions.assertTrue(statComposite.getRtStatComposite().getRtStats().stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY.getType()))); Optional> subContainer = statComposite.getRtStatComposite().getRtStats().stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY.getType())).findFirst(); subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationName).longValue())); @@ -97,29 +115,28 @@ void testCalcRt() { @Test @SuppressWarnings("rawtypes") void testCalcServiceKeyRt() { - String applicationName = "TestApp"; String serviceKey = "TestService"; String registryOpType = OP_TYPE_REGISTER_SERVICE.getType(); Long responseTime1 = 100L; Long responseTime2 = 200L; - statComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime1); - statComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime2); + statComposite.calcServiceKeyRt(serviceKey, registryOpType, responseTime1); + statComposite.calcServiceKeyRt(serviceKey, registryOpType, responseTime2); List exportedRtMetrics = statComposite.export(MetricsCategory.RT); GaugeMetricSample minSample = (GaugeMetricSample) exportedRtMetrics.stream() - .filter(sample -> sample.getTags().containsValue(applicationName)) - .filter(sample -> sample.getName().equals(METRIC_RT_MIN.getNameByType("register.service"))) - .findFirst().orElse(null); + .filter(sample -> sample.getTags().containsValue(applicationName)) + .filter(sample -> sample.getName().equals(METRIC_RT_MIN.getNameByType("register.service"))) + .findFirst().orElse(null); GaugeMetricSample maxSample = (GaugeMetricSample) exportedRtMetrics.stream() - .filter(sample -> sample.getTags().containsValue(applicationName)) - .filter(sample -> sample.getName().equals(METRIC_RT_MAX.getNameByType("register.service"))) - .findFirst().orElse(null); + .filter(sample -> sample.getTags().containsValue(applicationName)) + .filter(sample -> sample.getName().equals(METRIC_RT_MAX.getNameByType("register.service"))) + .findFirst().orElse(null); GaugeMetricSample avgSample = (GaugeMetricSample) exportedRtMetrics.stream() - .filter(sample -> sample.getTags().containsValue(applicationName)) - .filter(sample -> sample.getName().equals(METRIC_RT_AVG.getNameByType("register.service"))) - .findFirst().orElse(null); + .filter(sample -> sample.getTags().containsValue(applicationName)) + .filter(sample -> sample.getName().equals(METRIC_RT_AVG.getNameByType("register.service"))) + .findFirst().orElse(null); Assertions.assertNotNull(minSample); Assertions.assertNotNull(maxSample);