diff --git a/.github/workflows/build-and-test-pr.yml b/.github/workflows/build-and-test-pr.yml index 28d9bae045a..3dd5536095c 100644 --- a/.github/workflows/build-and-test-pr.yml +++ b/.github/workflows/build-and-test-pr.yml @@ -79,15 +79,6 @@ jobs: with: name: "class-file" path: ${{ github.workspace }}/class.zip - - name: "Pack rat file if failure" - if: failure() - run: 7z a ${{ github.workspace }}/rat.zip *rat.txt -r - - name: "Upload rat file if failure" - if: failure() - uses: actions/upload-artifact@v3 - with: - name: "rat-file" - path: ${{ github.workspace }}/rat.zip - name: "Pack checkstyle file if failure" if: failure() run: 7z a ${{ github.workspace }}/checkstyle.zip *checkstyle* -r diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/FilterChainBuilder.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/FilterChainBuilder.java index 7b2c2849303..a837724819b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/FilterChainBuilder.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/FilterChainBuilder.java @@ -237,6 +237,10 @@ public Result invoke(Invocation invocation) throws RpcException { return asyncResult; } + public Invoker getFilterInvoker() { + return filterInvoker; + } + @Override public Class getInterface() { return filterInvoker.getInterface(); diff --git a/dubbo-common/src/main/resources/security/serialize.allowlist b/dubbo-common/src/main/resources/security/serialize.allowlist index e59accc397c..aa56eab53fe 100644 --- a/dubbo-common/src/main/resources/security/serialize.allowlist +++ b/dubbo-common/src/main/resources/security/serialize.allowlist @@ -124,3 +124,4 @@ java.util.UUID java.util.WeakHashMap org.apache.dubbo com.alibaba.dubbo +com.alibaba.com.caucho.hessian.io.java8 diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java index e2c781ec221..b55af853c75 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ReferenceConfigTest.java @@ -35,6 +35,7 @@ import org.apache.dubbo.rpc.Exporter; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder; import org.apache.dubbo.rpc.cluster.support.registry.ZoneAwareClusterInvoker; import org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker; import org.apache.dubbo.rpc.listener.ListenerInvokerWrapper; @@ -480,8 +481,18 @@ void testCreateInvokerForLocalRefer() { referenceConfig.init(); Assertions.assertTrue(referenceConfig.getInvoker() instanceof MockClusterInvoker); Invoker withFilter = ((MockClusterInvoker) referenceConfig.getInvoker()).getDirectory().getAllInvokers().get(0); - Assertions.assertTrue(withFilter instanceof ListenerInvokerWrapper); - Assertions.assertTrue(((ListenerInvokerWrapper) withFilter).getInvoker() instanceof InjvmInvoker); + Assertions.assertTrue(withFilter instanceof ListenerInvokerWrapper + || withFilter instanceof FilterChainBuilder.CallbackRegistrationInvoker); + if (withFilter instanceof ListenerInvokerWrapper) { + Assertions.assertTrue(((ListenerInvokerWrapper) withFilter).getInvoker() instanceof InjvmInvoker); + } + if (withFilter instanceof FilterChainBuilder.CallbackRegistrationInvoker) { + Invoker filterInvoker = ((FilterChainBuilder.CallbackRegistrationInvoker) withFilter).getFilterInvoker(); + FilterChainBuilder.CopyOfFilterChainNode filterInvoker1 = (FilterChainBuilder.CopyOfFilterChainNode) filterInvoker; + ListenerInvokerWrapper originalInvoker = (ListenerInvokerWrapper) filterInvoker1.getOriginalInvoker(); + Invoker invoker = originalInvoker.getInvoker(); + Assertions.assertTrue(invoker instanceof InjvmInvoker); + } URL url = withFilter.getUrl(); Assertions.assertEquals("application1", url.getParameter("application")); Assertions.assertEquals("value1", url.getParameter("key1")); 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 cee91211dd8..cbccdcf1887 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 @@ -17,14 +17,18 @@ package org.apache.dubbo.metrics.model; -import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcInvocation; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Optional; +import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; +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.MetricsConstants.TAG_IP; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; @@ -41,6 +45,7 @@ */ public class MethodMetric implements Metric { private String applicationName; + private String side; private String interfaceName; private String methodName; private String group; @@ -100,30 +105,6 @@ public Map getTags() { return tags; } - @Override - 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(interfaceName, that.interfaceName) && Objects.equals(methodName, that.methodName) - && Objects.equals(group, that.group) && Objects.equals(version, that.version); - } - - @Override - public int hashCode() { - return Objects.hash(interfaceName, methodName, group, version); - } - - @Override - public String toString() { - return "MethodMetric{" + - "interfaceName='" + interfaceName + '\'' + - ", methodName='" + methodName + '\'' + - ", group='" + group + '\'' + - ", version='" + version + '\'' + - '}'; - } - private void init(Invocation invocation) { String serviceUniqueName = invocation.getTargetServiceUniqueName(); String methodName = invocation.getMethodName(); @@ -135,21 +116,62 @@ && isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodNam } String group = null; String interfaceAndVersion; - String[] arr = serviceUniqueName.split(CommonConstants.PATH_SEPARATOR); + String[] arr = serviceUniqueName.split(PATH_SEPARATOR); if (arr.length == 2) { group = arr[0]; interfaceAndVersion = arr[1]; } else { interfaceAndVersion = arr[0]; } - - String[] ivArr = interfaceAndVersion.split(CommonConstants.GROUP_CHAR_SEPARATOR); + 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 getSide() { + return side; + } + + public void setSide(String side) { + this.side = side; + } + + @Override + public String toString() { + return "MethodMetric{" + + "applicationName='" + applicationName + '\'' + + ", side='" + side + '\'' + + ", interfaceName='" + interfaceName + '\'' + + ", methodName='" + methodName + '\'' + + ", group='" + group + '\'' + + ", version='" + version + '\'' + + '}'; + } + + @Override + 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); + } + + @Override + public int hashCode() { + return Objects.hash(applicationName, side, interfaceName, methodName, group, version); + } } diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java index 1d19b044c72..7649af788aa 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java @@ -21,32 +21,32 @@ public enum MetricsKey { APPLICATION_METRIC_INFO("dubbo.application.info.total", "Total Application Info"), // provider metrics key - PROVIDER_METRIC_REQUESTS("dubbo.provider.requests.total", "Total Requests"), - PROVIDER_METRIC_REQUESTS_SUCCEED("dubbo.provider.requests.succeed.total", "Succeed Requests"), - PROVIDER_METRIC_REQUEST_BUSINESS_FAILED("dubbo.provider.requests.business.failed.total","Failed Business Requests"), - PROVIDER_METRIC_REQUESTS_PROCESSING("dubbo.provider.requests.processing", "Processing Requests"), - PROVIDER_METRIC_REQUESTS_TIMEOUT("dubbo.provider.requests.timeout.total", "Total Timeout Failed Requests"), - PROVIDER_METRIC_REQUESTS_LIMIT("dubbo.provider.requests.limit.total", "Total Limit Failed Requests"), - PROVIDER_METRIC_REQUESTS_FAILED("dubbo.provider.requests.unknown.failed.total", "Unknown Failed Requests"), - PROVIDER_METRIC_REQUESTS_TOTAL_FAILED("dubbo.provider.requests.failed.total", "Total Failed Requests"), + METRIC_REQUESTS("dubbo.%s.requests.total", "Total Requests"), + METRIC_REQUESTS_SUCCEED("dubbo.%s.requests.succeed.total", "Succeed Requests"), + METRIC_REQUEST_BUSINESS_FAILED("dubbo.%s.requests.business.failed.total","Failed Business Requests"), + METRIC_REQUESTS_PROCESSING("dubbo.%s.requests.processing", "Processing Requests"), + METRIC_REQUESTS_TIMEOUT("dubbo.%s.requests.timeout.total", "Total Timeout Failed Requests"), + METRIC_REQUESTS_LIMIT("dubbo.%s.requests.limit.total", "Total Limit Failed Requests"), + METRIC_REQUESTS_FAILED("dubbo.%s.requests.unknown.failed.total", "Unknown Failed Requests"), + METRIC_REQUESTS_TOTAL_FAILED("dubbo.%s.requests.failed.total", "Total Failed Requests"), - PROVIDER_METRIC_REQUESTS_TOTAL_AGG("dubbo.provider.requests.total.aggregate", "Aggregated Total Requests"), - PROVIDER_METRIC_REQUESTS_SUCCEED_AGG("dubbo.provider.requests.succeed.aggregate", "Aggregated Succeed Requests"), - PROVIDER_METRIC_REQUESTS_FAILED_AGG("dubbo.provider.requests.failed.aggregate", "Aggregated Failed Requests"), - PROVIDER_METRIC_REQUESTS_BUSINESS_FAILED_AGG("dubbo.provider.requests.business.failed.aggregate", "Aggregated Business Failed Requests"), - PROVIDER_METRIC_REQUESTS_TIMEOUT_AGG("dubbo.provider.requests.timeout.failed.aggregate", "Aggregated timeout Failed Requests"), - PROVIDER_METRIC_REQUESTS_LIMIT_AGG("dubbo.provider.requests.limit.aggregate", "Aggregated limit Requests"), - PROVIDER_METRIC_REQUESTS_TOTAL_FAILED_AGG("dubbo.provider.requests.failed.total.aggregate", "Aggregated failed total Requests"), + METRIC_REQUESTS_TOTAL_AGG("dubbo.%s.requests.total.aggregate", "Aggregated Total Requests"), + METRIC_REQUESTS_SUCCEED_AGG("dubbo.%s.requests.succeed.aggregate", "Aggregated Succeed Requests"), + METRIC_REQUESTS_FAILED_AGG("dubbo.%s.requests.failed.aggregate", "Aggregated Failed Requests"), + METRIC_REQUESTS_BUSINESS_FAILED_AGG("dubbo.%s.requests.business.failed.aggregate", "Aggregated Business Failed Requests"), + METRIC_REQUESTS_TIMEOUT_AGG("dubbo.%s.requests.timeout.failed.aggregate", "Aggregated timeout Failed Requests"), + METRIC_REQUESTS_LIMIT_AGG("dubbo.%s.requests.limit.aggregate", "Aggregated limit Requests"), + METRIC_REQUESTS_TOTAL_FAILED_AGG("dubbo.%s.requests.failed.total.aggregate", "Aggregated failed total Requests"), - PROVIDER_METRIC_QPS("dubbo.provider.qps.seconds", "Query Per Seconds"), - PROVIDER_METRIC_RT_LAST("dubbo.provider.rt.seconds.last", "Last Response Time"), - PROVIDER_METRIC_RT_MIN("dubbo.provider.rt.seconds.min", "Min Response Time"), - PROVIDER_METRIC_RT_MAX("dubbo.provider.rt.seconds.max", "Max Response Time"), - PROVIDER_METRIC_RT_SUM("dubbo.provider.rt.seconds.sum", "Sum Response Time"), - PROVIDER_METRIC_RT_AVG("dubbo.provider.rt.seconds.avg", "Average Response Time"), - PROVIDER_METRIC_RT_P99("dubbo.provider.rt.seconds.p99", "Response Time P99"), - PROVIDER_METRIC_RT_P95("dubbo.provider.rt.seconds.p95", "Response Time P95"), + METRIC_QPS("dubbo.%s.qps.seconds", "Query Per Seconds"), + METRIC_RT_LAST("dubbo.%s.rt.seconds.last", "Last Response Time"), + METRIC_RT_MIN("dubbo.%s.rt.seconds.min", "Min Response Time"), + METRIC_RT_MAX("dubbo.%s.rt.seconds.max", "Max Response Time"), + METRIC_RT_SUM("dubbo.%s.rt.seconds.sum", "Sum Response Time"), + METRIC_RT_AVG("dubbo.%s.rt.seconds.avg", "Average Response Time"), + METRIC_RT_P99("dubbo.%s.rt.seconds.p99", "Response Time P99"), + METRIC_RT_P95("dubbo.%s.rt.seconds.p95", "Response Time P95"), GENERIC_METRIC_REQUESTS("dubbo.%s.requests.total", "Total %s Requests"), GENERIC_METRIC_REQUESTS_SUCCEED("dubbo.%s.requests.succeed.total", "Succeed %s Requests"), @@ -65,10 +65,7 @@ public enum MetricsKey { THREAD_POOL_MAX_SIZE("dubbo.thread.pool.max.size","Thread Pool Max Size"), THREAD_POOL_ACTIVE_SIZE("dubbo.thread.pool.active.size","Thread Pool Active Size"), THREAD_POOL_THREAD_COUNT("dubbo.thread.pool.thread.count","Thread Pool Thread Count"), - THREAD_POOL_QUEUE_SIZE("dubbo.thread.pool.queue.size","Thread Pool Queue Size"), - - // consumer metrics key - ; + THREAD_POOL_QUEUE_SIZE("dubbo.thread.pool.queue.size","Thread Pool Queue Size"); private final String name; private final String description; @@ -76,6 +73,9 @@ public enum MetricsKey { public final String getName() { return this.name; } + public final String getNameByType(String type) { + return String.format(name, type); + } public final String getDescription() { return this.description; diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java index d69e2e3eb2b..9277ab1d1e3 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java @@ -35,6 +35,11 @@ public GaugeMetricSample(MetricsKey metricsKey, Map tags, Metric this.supplier = supplier; } + public GaugeMetricSample(String name, String description, Map tags, MetricsCategory category, Supplier supplier) { + super(name, description, tags, Type.GAUGE, category); + this.supplier = supplier; + } + public GaugeMetricSample(String name, String description, Map tags, MetricsCategory category, String baseUnit, Supplier supplier) { super(name, description, tags, Type.GAUGE, category, baseUnit); this.supplier = supplier; diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/MetricSample.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/MetricSample.java index d2420b31563..826ade67ddd 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/MetricSample.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/MetricSample.java @@ -121,6 +121,7 @@ public String toString() { '}'; } + public enum Type { COUNTER, GAUGE, 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 963d3ddca44..a929194922b 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java @@ -17,6 +17,11 @@ package org.apache.dubbo.metrics.collector; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + import org.apache.dubbo.common.utils.ConcurrentHashMapUtils; import org.apache.dubbo.config.MetricsConfig; import org.apache.dubbo.config.context.ConfigManager; @@ -33,11 +38,6 @@ import org.apache.dubbo.metrics.model.sample.MetricSample; import org.apache.dubbo.rpc.model.ApplicationModel; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - import static org.apache.dubbo.metrics.model.MetricsCategory.QPS; import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS; import static org.apache.dubbo.metrics.model.MetricsCategory.RT; @@ -102,7 +102,7 @@ private void onRTEvent(RTEvent event) { private void onRequestEvent(RequestEvent event) { MethodMetric metric = (MethodMetric) event.getSource(); - MetricsEvent.Type type = event.getType(); + RequestEvent.Type type = event.getType(); TimeWindowCounter counter = null; switch (type) { case TOTAL: @@ -152,23 +152,39 @@ public List collect() { } private void collectRequests(List list) { - totalRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_AGG, k.getTags(), REQUESTS, v::get))); - succeedRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED_AGG, k.getTags(), REQUESTS, v::get))); - unknownFailedRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED_AGG, k.getTags(), REQUESTS, v::get))); - businessFailedRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_BUSINESS_FAILED_AGG, k.getTags(), REQUESTS, v::get))); - timeoutRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT_AGG, k.getTags(), REQUESTS, v::get))); - limitRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT_AGG, k.getTags(), REQUESTS, v::get))); - totalFailedRequests.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED_AGG, k.getTags(), REQUESTS, v::get))); + totalRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_TOTAL_AGG, k, v))); + succeedRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_SUCCEED_AGG, k, v))); + unknownFailedRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_FAILED_AGG, k, v))); + businessFailedRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_BUSINESS_FAILED_AGG, k, v))); + timeoutRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_TIMEOUT_AGG, k, v))); + limitRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_LIMIT_AGG, k, v))); + totalFailedRequests.forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_REQUESTS_TOTAL_FAILED_AGG, k, v))); + + } + + private GaugeMetricSample getGaugeMetricSample(MetricsKey metricRequestsTotalAgg, MethodMetric k, TimeWindowCounter v) { + return new GaugeMetricSample(metricRequestsTotalAgg.getNameByType(k.getSide()), + metricRequestsTotalAgg.getDescription(), k.getTags(), REQUESTS, v::get); } private void collectQPS(List list) { - qps.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_QPS, k.getTags(), QPS, () -> v.get() / v.bucketLivedSeconds()))); + qps.forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.METRIC_QPS.getNameByType(k.getSide()), + MetricsKey.METRIC_QPS.getDescription(), k.getTags(), QPS, () -> v.get() / v.bucketLivedSeconds()))); } private void collectRT(List list) { rt.forEach((k, v) -> { - list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_P99, k.getTags(), RT, () -> v.quantile(0.99))); - list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_P95, k.getTags(), RT, () -> v.quantile(0.95))); + list.add(new GaugeMetricSample(MetricsKey.METRIC_RT_P99.getNameByType(k.getSide()), + MetricsKey.METRIC_RT_P99.getDescription(), k.getTags(), RT, () -> v.quantile(0.99))); + list.add(new GaugeMetricSample(MetricsKey.METRIC_RT_P95.getNameByType(k.getSide()), + MetricsKey.METRIC_RT_P95.getDescription(), k.getTags(), RT, () -> v.quantile(0.95))); }); } } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java index b915e807928..82f06938210 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java @@ -23,6 +23,7 @@ import org.apache.dubbo.metrics.event.RequestEvent; import org.apache.dubbo.metrics.model.MethodMetric; import org.apache.dubbo.metrics.model.Metric; +import org.apache.dubbo.metrics.model.MetricsCategory; import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; @@ -31,11 +32,12 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Supplier; import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS; import static org.apache.dubbo.metrics.model.MetricsCategory.RT; -public class MethodMetricsSampler extends SimpleMetricsCountSampler{ +public class MethodMetricsSampler extends SimpleMetricsCountSampler { private DefaultMetricsCollector collector; @@ -45,14 +47,14 @@ public MethodMetricsSampler(DefaultMetricsCollector collector) { @Override protected void countConfigure( - MetricsCountSampleConfigurer sampleConfigure) { + MetricsCountSampleConfigurer sampleConfigure) { sampleConfigure.configureMetrics(configure -> new MethodMetric(collector.getApplicationName(), configure.getSource())); sampleConfigure.configureEventHandler(configure -> collector.getEventMulticaster().publishEvent(new RequestEvent(configure.getMetric(), configure.getMetricName()))); } @Override public void rtConfigure( - MetricsCountSampleConfigurer sampleConfigure) { + MetricsCountSampleConfigurer sampleConfigure) { sampleConfigure.configureMetrics(configure -> new MethodMetric(collector.getApplicationName(), configure.getSource())); sampleConfigure.configureEventHandler(configure -> collector.getEventMulticaster().publishEvent(new RTEvent(configure.getMetric(), configure.getRt()))); } @@ -62,41 +64,49 @@ public List sample() { List metricSamples = new ArrayList<>(); collect(metricSamples); - collectRT(metricSamples); return metricSamples; } private void collect(List list) { - count(list, MetricsEvent.Type.TOTAL, MetricsKey.PROVIDER_METRIC_REQUESTS); - count(list, MetricsEvent.Type.SUCCEED, MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED); - count(list, MetricsEvent.Type.UNKNOWN_FAILED, MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED); - count(list, MetricsEvent.Type.PROCESSING, MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING); - count(list, MetricsEvent.Type.BUSINESS_FAILED, MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED); - count(list, MetricsEvent.Type.REQUEST_TIMEOUT, MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT); - count(list, MetricsEvent.Type.REQUEST_LIMIT, MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT); - count(list, MetricsEvent.Type.TOTAL_FAILED, MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED); + count(list, MetricsEvent.Type.TOTAL, MetricsKey.METRIC_REQUESTS); + count(list, MetricsEvent.Type.SUCCEED, MetricsKey.METRIC_REQUESTS_SUCCEED); + count(list, MetricsEvent.Type.UNKNOWN_FAILED, MetricsKey.METRIC_REQUESTS_FAILED); + count(list, MetricsEvent.Type.PROCESSING, MetricsKey.METRIC_REQUESTS_PROCESSING); + count(list, MetricsEvent.Type.BUSINESS_FAILED, MetricsKey.METRIC_REQUEST_BUSINESS_FAILED); + count(list, MetricsEvent.Type.REQUEST_TIMEOUT, MetricsKey.METRIC_REQUESTS_TIMEOUT); + count(list, MetricsEvent.Type.REQUEST_LIMIT, MetricsKey.METRIC_REQUESTS_LIMIT); + count(list, MetricsEvent.Type.TOTAL_FAILED, MetricsKey.METRIC_REQUESTS_TOTAL_FAILED); } private void collectRT(List list) { - this.getLastRT().forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_LAST, k.getTags(), RT, v::get))); - this.getMinRT().forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MIN, k.getTags(), RT, v::get))); - this.getMaxRT().forEach((k, v) -> list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_MAX, k.getTags(), RT, v::get))); + this.getLastRT().forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_RT_LAST, k, RT, v::get) + )); + this.getMinRT().forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_RT_MIN, k, RT, v::get))); + this.getMaxRT().forEach((k, v) -> + list.add(getGaugeMetricSample(MetricsKey.METRIC_RT_MAX, k, RT, v::get))); this.getTotalRT().forEach((k, v) -> { - list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_SUM, k.getTags(), RT, v::get)); - + list.add(getGaugeMetricSample(MetricsKey.METRIC_RT_SUM, k, RT, v::get)); AtomicLong avg = this.getAvgRT().get(k); AtomicLong count = this.getRtCount().get(k); avg.set(v.get() / count.get()); - list.add(new GaugeMetricSample(MetricsKey.PROVIDER_METRIC_RT_AVG, k.getTags(), RT, avg::get)); + list.add(getGaugeMetricSample(MetricsKey.METRIC_RT_AVG, k, RT, avg::get)); }); } + private GaugeMetricSample getGaugeMetricSample(MetricsKey metricsKey, MethodMetric methodMetric, + MetricsCategory metricsCategory, Supplier get) { + return new GaugeMetricSample(metricsKey.getNameByType(methodMetric.getSide()), metricsKey.getDescription(), + methodMetric.getTags(), metricsCategory, get); + } + private void count(List list, MetricsEvent.Type eventType, MetricsKey metricsKey) { - getCount(eventType).filter(e->!e.isEmpty()) - .ifPresent(map -> map.forEach((k, v) -> list.add(new GaugeMetricSample(metricsKey, k.getTags(), - REQUESTS, v::get)))); + getCount(eventType).filter(e -> !e.isEmpty()) + .ifPresent(map -> map.forEach((k, v) -> + list.add(getGaugeMetricSample(metricsKey, k, REQUESTS, v::get)))); } } diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java index 2f428dc016e..51c89a40aee 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java +++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java @@ -27,9 +27,10 @@ import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.ScopeModelAware; +import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER; -@Activate(group = PROVIDER, order = -1) +@Activate(group = {CONSUMER, PROVIDER}, order = -1) public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAware { private DefaultMetricsCollector collector = null; diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/TestMetricsInvoker.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/TestMetricsInvoker.java new file mode 100644 index 00000000000..a9edf9507a8 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/TestMetricsInvoker.java @@ -0,0 +1,57 @@ +/* + * 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; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Result; +import org.apache.dubbo.rpc.RpcException; + +public class TestMetricsInvoker implements Invoker { + + private String side; + + public TestMetricsInvoker(String side) { + this.side = side; + } + + @Override + public Class getInterface() { + return null; + } + + @Override + public Result invoke(Invocation invocation) throws RpcException { + return null; + } + + @Override + public URL getUrl() { + return URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side="+side); + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public void destroy() { + + } +} 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 6baaa3af4e6..c12e3ef3355 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 @@ -17,14 +17,18 @@ package org.apache.dubbo.metrics.collector; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.MetricsConfig; import org.apache.dubbo.config.nested.AggregationConfig; +import org.apache.dubbo.metrics.TestMetricsInvoker; import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler; import org.apache.dubbo.metrics.event.MetricsEvent; import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; +import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.model.ApplicationModel; import org.junit.jupiter.api.AfterEach; @@ -53,6 +57,7 @@ class AggregateMetricsCollectorTest { private String group; private String version; private RpcInvocation invocation; + private String side; @BeforeEach public void setup() { @@ -81,6 +86,10 @@ public void setup() { invocation.setTargetServiceUniqueName(group + "/" + interfaceName + ":" + version); invocation.setAttachment(GROUP_KEY, group); invocation.setAttachment(VERSION_KEY, version); + side = CommonConstants.CONSUMER; + invocation.setInvoker(new TestMetricsInvoker(side)); + RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side=" + side)); + } @AfterEach @@ -117,12 +126,12 @@ void testRequestsMetrics() { return number.longValue(); })); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_AGG.getName()), 1L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED_AGG.getName()), 1L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED_AGG.getName()), 1L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_BUSINESS_FAILED_AGG.getName()), 1L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_AGG.getNameByType(side)), 1L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED_AGG.getNameByType(side)), 1L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_FAILED_AGG.getNameByType(side)), 1L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_BUSINESS_FAILED_AGG.getNameByType(side)), 1L); - Assertions.assertTrue(sampleMap.containsKey(MetricsKey.PROVIDER_METRIC_QPS.getName())); + Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_QPS.getNameByType(side))); } @Test @@ -150,7 +159,7 @@ void testRTMetrics() { return number.longValue(); })); - Assertions.assertTrue(sampleMap.containsKey(MetricsKey.PROVIDER_METRIC_RT_P99.getName())); - Assertions.assertTrue(sampleMap.containsKey(MetricsKey.PROVIDER_METRIC_RT_P95.getName())); + Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P99.getNameByType(side))); + Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P95.getNameByType(side))); } } diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/filter/MetricsFilterTest.java index 88b21773132..ea67b53677e 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 @@ -23,17 +23,15 @@ import java.util.function.Function; import java.util.stream.Collectors; -import org.apache.dubbo.common.constants.MetricsConstants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.metrics.TestMetricsInvoker; import org.apache.dubbo.metrics.collector.DefaultMetricsCollector; import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.apache.dubbo.rpc.AppResponse; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Result; -import org.apache.dubbo.rpc.RpcException; -import org.apache.dubbo.rpc.RpcInvocation; +import org.apache.dubbo.rpc.*; import org.apache.dubbo.rpc.model.ApplicationModel; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -61,11 +59,14 @@ class MetricsFilterTest { private static final String METHOD_NAME = "mockMethod"; private static final String GROUP = "mockGroup"; private static final String VERSION = "1.0.0"; + private String side; + @BeforeEach public void setup() { ApplicationConfig config = new ApplicationConfig(); config.setName("MockMetrics"); + //RpcContext.getContext().setAttachment("MockMetrics","MockMetrics"); applicationModel = ApplicationModel.defaultModel(); applicationModel.getApplicationConfigManager().setApplication(config); @@ -75,6 +76,10 @@ public void setup() { collector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); filter.setApplicationModel(applicationModel); + side = CommonConstants.CONSUMER; + invocation.setInvoker(new TestMetricsInvoker(side)); + RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side=" + side)); + } @AfterEach @@ -105,10 +110,10 @@ void testUnknownFailedRequests() { } Map metricsMap = getMetricsMap(); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED.getName())); - Assertions.assertFalse(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName())); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_FAILED.getNameByType(side))); + Assertions.assertFalse(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side))); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_FAILED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_FAILED.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); @@ -133,10 +138,10 @@ void testBusinessFailedRequests() { } Map metricsMap = getMetricsMap(); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED.getName())); - Assertions.assertFalse(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName())); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUEST_BUSINESS_FAILED.getNameByType(side))); + Assertions.assertFalse(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side))); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUEST_BUSINESS_FAILED.getNameByType(side)); Map tags = sample.getTags(); @@ -165,13 +170,13 @@ void testTimeoutRequests() { } } Map metricsMap = getMetricsMap(); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT.getName())); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED.getName())); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_TIMEOUT.getNameByType(side))); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_TOTAL_FAILED.getNameByType(side))); - MetricSample timeoutSample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_TIMEOUT.getName()); + MetricSample timeoutSample = metricsMap.get(MetricsKey.METRIC_REQUESTS_TIMEOUT.getNameByType(side)); Assertions.assertSame(((GaugeMetricSample) timeoutSample).getSupplier().get().longValue(), count); - GaugeMetricSample failedSample = (GaugeMetricSample)metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_TOTAL_FAILED.getName()); + GaugeMetricSample failedSample = (GaugeMetricSample) metricsMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_FAILED.getNameByType(side)); Assertions.assertSame(failedSample.getSupplier().get().longValue(), count); } @@ -193,9 +198,9 @@ void testLimitRequests() { } } Map metricsMap = getMetricsMap(); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT.getName())); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_LIMIT.getNameByType(side))); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_LIMIT.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_LIMIT.getNameByType(side)); Assertions.assertSame(((GaugeMetricSample) sample).getSupplier().get().longValue(), count); } @@ -211,10 +216,10 @@ void testSucceedRequests() { filter.onResponse(result, invoker, invocation); Map metricsMap = getMetricsMap(); - Assertions.assertFalse(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUEST_BUSINESS_FAILED.getName())); - Assertions.assertTrue(metricsMap.containsKey(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName())); + Assertions.assertFalse(metricsMap.containsKey(MetricsKey.METRIC_REQUEST_BUSINESS_FAILED.getNameByType(side))); + Assertions.assertTrue(metricsMap.containsKey(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side))); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); @@ -237,7 +242,7 @@ void testMissingGroup() { Map metricsMap = getMetricsMap(); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); @@ -260,7 +265,7 @@ void testMissingVersion() { Map metricsMap = getMetricsMap(); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); @@ -283,7 +288,7 @@ void testMissingGroupAndVersion() { Map metricsMap = getMetricsMap(); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_SUCCEED.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); @@ -310,7 +315,7 @@ void testGenericCall() { Map metricsMap = getMetricsMap(); - MetricSample sample = metricsMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING.getName()); + MetricSample sample = metricsMap.get(MetricsKey.METRIC_REQUESTS_PROCESSING.getNameByType(side)); Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME); diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java index 8e071442151..1dad4e792d2 100644 --- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java @@ -17,7 +17,10 @@ package org.apache.dubbo.metrics.metrics.collector; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.metrics.TestMetricsInvoker; import org.apache.dubbo.metrics.collector.DefaultMetricsCollector; import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler; import org.apache.dubbo.metrics.event.MetricsEvent; @@ -27,6 +30,7 @@ import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.model.sample.MetricSample; +import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.model.ApplicationModel; import org.apache.dubbo.rpc.model.FrameworkModel; @@ -35,7 +39,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Supplier; @@ -57,7 +60,7 @@ class DefaultMetricsCollectorTest { private String group; private String version; private RpcInvocation invocation; - public static final String DUBBO_THREAD_METRIC_MARK = "dubbo.thread.pool"; + private String side; @BeforeEach public void setup() { @@ -77,6 +80,11 @@ public void setup() { invocation.setTargetServiceUniqueName(group + "/" + interfaceName + ":" + version); invocation.setAttachment(GROUP_KEY, group); invocation.setAttachment(VERSION_KEY, version); + side = CommonConstants.CONSUMER; + + invocation.setInvoker(new TestMetricsInvoker(side)); + RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side="+side)); + } @AfterEach @@ -99,9 +107,6 @@ void testRequestsMetrics() { List samples = collector.collect(); for (MetricSample sample : samples) { - if (sample.getName().contains(DUBBO_THREAD_METRIC_MARK)) { - continue; - } Assertions.assertTrue(sample instanceof GaugeMetricSample); GaugeMetricSample gaugeSample = (GaugeMetricSample) sample; Map tags = gaugeSample.getTags(); @@ -116,19 +121,12 @@ void testRequestsMetrics() { methodMetricsCountSampler.dec(invocation,MetricsEvent.Type.PROCESSING); samples = collector.collect(); - List samples1 = new ArrayList<>(); - for (MetricSample sample : samples) { - if (sample.getName().contains(DUBBO_THREAD_METRIC_MARK)) { - continue; - } - samples1.add(sample); - } - Map sampleMap = samples1.stream().collect(Collectors.toMap(MetricSample::getName, k -> { + Map sampleMap = samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> { Number number = ((GaugeMetricSample) k).getSupplier().get(); return number.longValue(); })); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_REQUESTS_PROCESSING.getName()), 0L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_PROCESSING.getNameByType(side)), 0L); } @Test @@ -145,9 +143,6 @@ void testRTMetrics() { List samples = collector.collect(); for (MetricSample sample : samples) { - if (sample.getName().contains(DUBBO_THREAD_METRIC_MARK)) { - continue; - } Map tags = sample.getTags(); Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), interfaceName); @@ -155,23 +150,17 @@ void testRTMetrics() { Assertions.assertEquals(tags.get(TAG_GROUP_KEY), group); Assertions.assertEquals(tags.get(TAG_VERSION_KEY), version); } - List samples1 = new ArrayList<>(); - for (MetricSample sample : samples) { - if (sample.getName().contains(DUBBO_THREAD_METRIC_MARK)) { - continue; - } - samples1.add(sample); - } - Map sampleMap = samples1.stream().collect(Collectors.toMap(MetricSample::getName, k -> { + + Map sampleMap = samples.stream().collect(Collectors.toMap(MetricSample::getName, k -> { Number number = ((GaugeMetricSample) k).getSupplier().get(); return number.longValue(); })); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_RT_LAST.getName()), 0L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_RT_MIN.getName()), 0L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_RT_MAX.getName()), 10L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_RT_AVG.getName()), 5L); - Assertions.assertEquals(sampleMap.get(MetricsKey.PROVIDER_METRIC_RT_SUM.getName()), 10L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_LAST.getNameByType(side)), 0L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_MIN.getNameByType(side)), 0L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_MAX.getNameByType(side)), 10L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_AVG.getNameByType(side)), 5L); + Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_RT_SUM.getNameByType(side)), 10L); } @Test 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 c554c09d9bc..6266a6860eb 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 @@ -17,7 +17,9 @@ package org.apache.dubbo.metrics.metrics.model; +import org.apache.dubbo.common.URL; import org.apache.dubbo.metrics.model.MethodMetric; +import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcInvocation; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -53,9 +55,11 @@ public static void setup() { group = "mockGroup"; version = "1.0.0"; invocation = new RpcInvocation(methodName, interfaceName, "serviceKey", null, null); + invocation.setTargetServiceUniqueName(group + "/" + interfaceName + ":" + version); invocation.setAttachment(GROUP_KEY, group); invocation.setAttachment(VERSION_KEY, version); + RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side=consumer")); } @Test diff --git a/pom.xml b/pom.xml index eb13b0c110a..dfec4c3efb8 100644 --- a/pom.xml +++ b/pom.xml @@ -272,7 +272,7 @@ false - + checkstyle