From 192c5719065e87674d4d5bcc8909c5ffb68dfcc3 Mon Sep 17 00:00:00 2001 From: ZhangJian He Date: Fri, 9 Aug 2024 10:18:12 +0800 Subject: [PATCH] Fix exclude pattern initialization in InvocationMetrics and add relevant unit test (#1362) Signed-off-by: ZhangJian He --- .../common/metrics/InvocationMetrics.java | 2 +- .../common/metrics/InvocationMetricsTest.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/metrics/InvocationMetrics.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/metrics/InvocationMetrics.java index 3c5d3c968..b8fc8f02a 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/metrics/InvocationMetrics.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/metrics/InvocationMetrics.java @@ -72,7 +72,7 @@ public InvocationMetrics(MeterRegistry meterRegistry, MetricsProperties metricsP this.meterRegistry = meterRegistry; this.metricsProperties = metricsProperties; if (StringUtils.isNotEmpty(metricsProperties.getExcludePattern())) { - excludePattern = Pattern.compile(metricsProperties.getIncludePattern()); + excludePattern = Pattern.compile(metricsProperties.getExcludePattern()); } else { excludePattern = null; } diff --git a/spring-cloud-huawei-common/src/test/java/com/huaweicloud/common/metrics/InvocationMetricsTest.java b/spring-cloud-huawei-common/src/test/java/com/huaweicloud/common/metrics/InvocationMetricsTest.java index c8bc93126..e8e7c6a44 100644 --- a/spring-cloud-huawei-common/src/test/java/com/huaweicloud/common/metrics/InvocationMetricsTest.java +++ b/spring-cloud-huawei-common/src/test/java/com/huaweicloud/common/metrics/InvocationMetricsTest.java @@ -128,4 +128,45 @@ private void testMetricsIncludePatternImpl() { Assertions.assertEquals(4, count); } + + @Test + public void testMetricsExcludePattern() { + MeterRegistry meterRegistry = new SimpleMeterRegistry(); + MetricsProperties metricsProperties = new MetricsProperties(); + metricsProperties.setExcludePattern("/exclude.*"); + metricsProperties.setMaxMetricsCount(4); + + InvocationMetrics metrics = new InvocationMetrics(meterRegistry, metricsProperties); + metrics.recordInvocationDistribution("/include", InvocationStage.STAGE_ALL, 200, TimeUnit.MILLISECONDS.toNanos(1)); + metrics.recordInvocationDistribution("/include", InvocationStage.STAGE_ALL, 200, TimeUnit.MILLISECONDS.toNanos(2)); + metrics.recordInvocationDistribution("/exclude", InvocationStage.STAGE_ALL, 200, TimeUnit.MILLISECONDS.toNanos(3)); + metrics.recordInvocationDistribution("/exclude", InvocationStage.STAGE_ALL, 200, TimeUnit.MILLISECONDS.toNanos(4)); + metrics.recordInvocationDistribution("/include", InvocationStage.STAGE_ALL, 500, TimeUnit.MILLISECONDS.toNanos(1)); + + List meters = meterRegistry.getMeters(); + int count = 0; + for (Meter meter : meters) { + if (!InvocationMetrics.METRICS_CALLS.equals(meter.getId().getName())) { + continue; + } + count++; + + DistributionSummary summary = (DistributionSummary) meter; + + if (Integer.parseInt(meter.getId().getTag(InvocationMetrics.TAG_STATUS)) == 200 + && "/include".equals(meter.getId().getTag(InvocationMetrics.TAG_NAME))) { + Assertions.assertEquals(2, summary.count()); + Assertions.assertEquals(3, TimeUnit.NANOSECONDS.toMillis(Double.valueOf(summary.totalAmount()).longValue())); + } else if (Integer.parseInt(meter.getId().getTag(InvocationMetrics.TAG_STATUS)) == 500 + && "/include".equals(meter.getId().getTag(InvocationMetrics.TAG_NAME))) { + Assertions.assertEquals(1, summary.count()); + Assertions.assertEquals(1, TimeUnit.NANOSECONDS.toMillis(Double.valueOf(summary.totalAmount()).longValue())); + } else { + Assertions.fail("Unexpected meter: " + meter.getId().toString()); + } + } + + // Ensure that the "/exclude" paths did not get recorded + Assertions.assertEquals(2, count); + } }