You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by he...@apache.org on 2024/03/18 05:44:40 UTC

(shenyu) branch master updated: [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix (#5468)

This is an automated email from the ASF dual-hosted git repository.

hefengen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 9c04861a5f [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix (#5468)
9c04861a5f is described below

commit 9c04861a5f61bf603579b9ff67fd09da2a79e12d
Author: JJellyfish <24...@qq.com>
AuthorDate: Mon Mar 18 13:44:36 2024 +0800

    [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix (#5468)
    
    * [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix
    
    * [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix
    
    * [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix
    
    * [type:feature] Enhance metrics collection for Sentinel, Resilience4j, and Hystrix
    
    ---------
    
    Co-authored-by: loongs-zhang <zh...@apache.org>
    Co-authored-by: moremind <he...@apache.org>
---
 .../apache/shenyu/common/constant/Constants.java   | 15 +++++
 .../dto/convert/rule/Resilience4JHandle.java       |  4 +-
 .../shenyu/plugin/hystrix/HystrixPlugin.java       |  8 ++-
 .../plugin/resilience4j/Resilience4JPlugin.java    |  7 ++-
 .../shenyu/plugin/sentinel/SentinelPlugin.java     |  7 ++-
 .../shenyu/plugin/metrics/MetricsPlugin.java       | 41 +++++++++++---
 .../shenyu/plugin/metrics/constant/LabelNames.java | 25 +++++++++
 .../plugin/metrics/reporter/MetricsReporter.java   | 45 ++++++++-------
 .../metrics/reporter/MetricsReporterTest.java      | 64 +++++++++++++++-------
 9 files changed, 163 insertions(+), 53 deletions(-)

diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
index a3c4193e70..c84929a811 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
@@ -805,6 +805,21 @@ public interface Constants {
      */
     String METRICS_RATE_LIMITER = "metricsRateLimiter";
 
+    /**
+     * sentinel plugin metrics.
+     */
+    String METRICS_SENTINEL = "metricsSentinel";
+
+    /**
+     * resilience4j plugin metrics.
+     */
+    String METRICS_RESILIENCE4J = "metricsResilience4j";
+
+    /**
+     * Hystrix plugin metrics.
+     */
+    String METRICS_HYSTRIX = "metricsHystrix";
+
     /**
      * String q.
      */
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/Resilience4JHandle.java b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/Resilience4JHandle.java
index 8093f8417a..22439044f2 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/Resilience4JHandle.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/Resilience4JHandle.java
@@ -52,7 +52,7 @@ public class Resilience4JHandle {
     private long timeoutDuration = Constants.TIMEOUT_DURATION;
 
     /**
-     * circuitBreaker timeoutDuration.
+     * circuitBreaker fallbackUri.
      */
     private String fallbackUri = "/fallback/resilience4j";
 
@@ -77,7 +77,7 @@ public class Resilience4JHandle {
     private int waitIntervalFunctionInOpenState = Constants.WAIT_INTERVAL_FUNCTION_IN_OPEN_STATE;
 
     /**
-     * circuitBreaker waitIntervalFunctionInOpenState.
+     * circuitBreaker permittedNumberOfCallsInHalfOpenState.
      */
     private int permittedNumberOfCallsInHalfOpenState = Constants.PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE;
 
diff --git a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
index 4ceaff4d9d..c99f3e2190 100644
--- a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-hystrix/src/main/java/org/apache/shenyu/plugin/hystrix/HystrixPlugin.java
@@ -36,11 +36,14 @@ import org.apache.shenyu.plugin.hystrix.command.HystrixCommandOnThread;
 import org.apache.shenyu.plugin.hystrix.handler.HystrixPluginDataHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 import rx.Subscription;
 
 import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Consumer;
 
 /**
  * Hystrix Plugin.
@@ -74,7 +77,10 @@ public class HystrixPlugin extends AbstractShenyuPlugin {
             LOG.error("hystrix execute exception:", throwable);
             exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.ERROR.getName());
             chain.execute(exchange);
-        }).then();
+        }).then().doFinally(monoV -> {
+            final Consumer<HttpStatus> consumer = exchange.getAttribute(Constants.METRICS_HYSTRIX);
+            Optional.ofNullable(consumer).ifPresent(c -> c.accept(exchange.getResponse().getStatusCode()));
+        });
     }
 
     private Command fetchCommand(final HystrixHandle hystrixHandle, final ServerWebExchange exchange, final ShenyuPluginChain chain, final String commandKey, final String groupKey) {
diff --git a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-resilience4j/src/main/java/org/apache/shenyu/plugin/resilience4j/Resilience4JPlugin.java b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-resilience4j/src/main/java/org/apache/shenyu/plugin/resilience4j/Resilience4JPlugin.java
index 5683b89989..04773a1c36 100644
--- a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-resilience4j/src/main/java/org/apache/shenyu/plugin/resilience4j/Resilience4JPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-resilience4j/src/main/java/org/apache/shenyu/plugin/resilience4j/Resilience4JPlugin.java
@@ -39,6 +39,8 @@ import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 
 import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Consumer;
 import java.util.function.Function;
 
 /**
@@ -88,7 +90,10 @@ public class Resilience4JPlugin extends AbstractShenyuPlugin {
 
     private Function<Throwable, Mono<Void>> fallback(final Executor executor,
                                                      final ServerWebExchange exchange, final String uri) {
-        return throwable -> executor.fallback(exchange, UriUtils.createUri(uri), throwable);
+        return throwable -> executor.fallback(exchange, UriUtils.createUri(uri), throwable).doFinally(monoV -> {
+            final Consumer<HttpStatus> consumer = exchange.getAttribute(Constants.METRICS_RESILIENCE4J);
+            Optional.ofNullable(consumer).ifPresent(c -> c.accept(exchange.getResponse().getStatusCode()));
+        });
     }
 
     @Override
diff --git a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java
index 95895e02dc..443d5bfa91 100644
--- a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-sentinel/src/main/java/org/apache/shenyu/plugin/sentinel/SentinelPlugin.java
@@ -35,6 +35,7 @@ import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 
+import java.util.Optional;
 import java.util.function.Consumer;
 
 /**
@@ -61,7 +62,11 @@ public class SentinelPlugin extends AbstractShenyuPlugin {
             }
         });
         return chain.execute(exchange).transform(new SentinelReactorTransformer<>(resourceName)).onErrorResume(throwable ->
-                fallbackHandler.fallback(exchange, UriUtils.createUri(sentinelHandle.getFallbackUri()), throwable));
+                fallbackHandler.fallback(exchange, UriUtils.createUri(sentinelHandle.getFallbackUri()), throwable)).doFinally(monoV -> {
+                    final Consumer<HttpStatus> consumer = exchange.getAttribute(Constants.METRICS_SENTINEL);
+                    Optional.ofNullable(consumer).ifPresent(c -> c.accept(exchange.getResponse().getStatusCode()));
+                }
+        );
     }
 
     @Override
diff --git a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java
index f9cc5f545d..5fffdea850 100644
--- a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java
@@ -39,19 +39,15 @@ import java.util.function.Consumer;
  * the monitor plugin.
  */
 public class MetricsPlugin implements ShenyuPlugin {
-    
+
     @Override
     public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) {
         MetricsReporter.counterIncrement(LabelNames.REQUEST_TOTAL);
         ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
         assert shenyuContext != null;
+        setMetricsCallbacks(exchange);
         MetricsReporter.counterIncrement(LabelNames.REQUEST_TYPE_TOTAL, new String[]{exchange.getRequest().getURI().getRawPath(), shenyuContext.getRpcType()});
         LocalDateTime startDateTime = Optional.of(shenyuContext).map(ShenyuContext::getStartDateTime).orElseGet(LocalDateTime::now);
-        exchange.getAttributes().put(Constants.METRICS_RATE_LIMITER, (Consumer<HttpStatus>) status -> {
-            if (Objects.nonNull(status) && HttpStatus.TOO_MANY_REQUESTS.equals(status)) {
-                MetricsReporter.counterIncrement(LabelNames.RATELIMITER_REQUEST_RESTRICT_TOTAL);
-            }
-        });
         return chain.execute(exchange).doOnSuccess(e -> responseCommitted(exchange, startDateTime))
                 .doOnError(throwable -> {
                     MetricsReporter.counterIncrement(LabelNames.REQUEST_THROW_TOTAL);
@@ -59,16 +55,43 @@ public class MetricsPlugin implements ShenyuPlugin {
                 });
     }
 
+    private void setMetricsCallbacks(final ServerWebExchange exchange) {
+        exchange.getAttributes().put(Constants.METRICS_SENTINEL, (Consumer<HttpStatus>) status -> {
+            if (Objects.equals(HttpStatus.TOO_MANY_REQUESTS, status)) {
+                MetricsReporter.counterIncrement(LabelNames.SENTINEL_REQUEST_RESTRICT_TOTAL);
+            } else if (Objects.equals(HttpStatus.INTERNAL_SERVER_ERROR, status)) {
+                MetricsReporter.counterIncrement(LabelNames.SENTINEL_REQUEST_CIRCUITBREAKER_TOTAL);
+            }
+        });
+        exchange.getAttributes().put(Constants.METRICS_RESILIENCE4J, (Consumer<HttpStatus>) status -> {
+            if (Objects.equals(HttpStatus.TOO_MANY_REQUESTS, status)) {
+                MetricsReporter.counterIncrement(LabelNames.RESILIENCE4J_REQUEST_RESTRICT_TOTAL);
+            } else if (Objects.equals(HttpStatus.INTERNAL_SERVER_ERROR, status)) {
+                MetricsReporter.counterIncrement(LabelNames.RESILIENCE4J_REQUEST_CIRCUITBREAKER_TOTAL);
+            }
+        });
+        exchange.getAttributes().put(Constants.METRICS_HYSTRIX, (Consumer<HttpStatus>) status -> {
+            if (Objects.equals(HttpStatus.INTERNAL_SERVER_ERROR, status)) {
+                MetricsReporter.counterIncrement(LabelNames.HYSTRIX_REQUEST_CIRCUITBREAKER_TOTAL);
+            }
+        });
+        exchange.getAttributes().put(Constants.METRICS_RATE_LIMITER, (Consumer<HttpStatus>) status -> {
+            if (Objects.nonNull(status) && HttpStatus.TOO_MANY_REQUESTS.equals(status)) {
+                MetricsReporter.counterIncrement(LabelNames.RATELIMITER_REQUEST_RESTRICT_TOTAL);
+            }
+        });
+    }
+
     @Override
     public int getOrder() {
         return PluginEnum.METRICS.getCode();
     }
-    
+
     @Override
     public String named() {
         return PluginEnum.METRICS.getName();
     }
-    
+
     private void responseCommitted(final ServerWebExchange exchange, final LocalDateTime startDateTime) {
         ServerHttpResponse response = exchange.getResponse();
         if (response.isCommitted()) {
@@ -80,7 +103,7 @@ public class MetricsPlugin implements ShenyuPlugin {
             });
         }
     }
-    
+
     private void recordTime(final LocalDateTime startDateTime) {
         long millisBetween = DateUtils.acquireMillisBetween(startDateTime, LocalDateTime.now());
         MetricsReporter.recordTime(LabelNames.EXECUTE_LATENCY_NAME, millisBetween);
diff --git a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
index d73b4e3260..7db4b6d8e2 100644
--- a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
+++ b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/constant/LabelNames.java
@@ -46,4 +46,29 @@ public final class LabelNames {
      * The constant RATELIMITER_REQUEST_RESTRICT_TOTAL.
      */
     public static final String RATELIMITER_REQUEST_RESTRICT_TOTAL = "shenyu_ratelimiter_request_restrict_total";
+
+    /**
+     * The constant SENTINEL_REQUEST_RESTRICT_TOTAL.
+     */
+    public static final String SENTINEL_REQUEST_RESTRICT_TOTAL = "shenyu_sentinel_request_restrict_total";
+
+    /**
+     * The constant SENTINEL_REQUEST_CIRCUITBREAKER_TOTAL.
+     */
+    public static final String SENTINEL_REQUEST_CIRCUITBREAKER_TOTAL = "shenyu_sentinel_request_circuitbreaker_total";
+
+    /**
+     * The constant RESILIENCE4J_REQUEST_RESTRICT_TOTAL.
+     */
+    public static final String RESILIENCE4J_REQUEST_RESTRICT_TOTAL = "shenyu_resilience4j_request_restrict_total";
+
+    /**
+     * The constant RESILIENCE4J_REQUEST_CIRCUITBREAKER_TOTAL.
+     */
+    public static final String RESILIENCE4J_REQUEST_CIRCUITBREAKER_TOTAL = "shenyu_resilience4j_request_circuitbreaker_total";
+
+    /**
+     * The constant HYSTRIX_REQUEST_CIRCUITBREAKER_TOTAL.
+     */
+    public static final String HYSTRIX_REQUEST_CIRCUITBREAKER_TOTAL = "shenyu_hystrix_request_circuitbreaker_total";
 }
diff --git a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java
index d18d7779d9..5c926ed98e 100644
--- a/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java
+++ b/shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java
@@ -29,9 +29,9 @@ import java.util.Optional;
  * Metrics reporter.
  */
 public final class MetricsReporter {
-    
+
     private static MetricsRegister metricsRegister;
-    
+
     /**
      * Register.
      *
@@ -43,9 +43,14 @@ public final class MetricsReporter {
         MetricsReporter.registerCounter(LabelNames.REQUEST_TYPE_TOTAL, new String[]{"path", "type"}, "shenyu http request type total count");
         MetricsReporter.registerCounter(LabelNames.REQUEST_THROW_TOTAL, "shenyu request error total count");
         MetricsReporter.registerHistogram(LabelNames.EXECUTE_LATENCY_NAME, "the shenyu executor latency millis");
+        MetricsReporter.registerCounter(LabelNames.SENTINEL_REQUEST_RESTRICT_TOTAL, "shenyu sentinel request restrict total count");
+        MetricsReporter.registerCounter(LabelNames.SENTINEL_REQUEST_CIRCUITBREAKER_TOTAL, "shenyu sentinel circuitbreaker request total count");
+        MetricsReporter.registerCounter(LabelNames.RESILIENCE4J_REQUEST_RESTRICT_TOTAL, "shenyu resilience4j request restrict total count");
+        MetricsReporter.registerCounter(LabelNames.RESILIENCE4J_REQUEST_CIRCUITBREAKER_TOTAL, "shenyu resilience4j circuitbreaker request total count");
+        MetricsReporter.registerCounter(LabelNames.HYSTRIX_REQUEST_CIRCUITBREAKER_TOTAL, "shenyu hystrix circuitbreaker request total count");
         MetricsReporter.registerCounter(LabelNames.RATELIMITER_REQUEST_RESTRICT_TOTAL, "shenyu ratelimiter request restrict total count");
     }
-    
+
     /**
      * Register metrics.
      *
@@ -68,7 +73,7 @@ public final class MetricsReporter {
             }
         }
     }
-    
+
     /**
      * Register counter.
      *
@@ -79,7 +84,7 @@ public final class MetricsReporter {
     public static void registerCounter(final String name, final String[] labelNames, final String document) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.registerCounter(name, labelNames, document));
     }
-    
+
     /**
      * Register counter.
      *
@@ -89,7 +94,7 @@ public final class MetricsReporter {
     public static void registerCounter(final String name, final String document) {
         registerCounter(name, null, document);
     }
-    
+
     /**
      * Register gauge.
      *
@@ -100,7 +105,7 @@ public final class MetricsReporter {
     public static void registerGauge(final String name, final String[] labelNames, final String document) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.registerGauge(name, labelNames, document));
     }
-    
+
     /**
      * Register gauge.
      *
@@ -110,7 +115,7 @@ public final class MetricsReporter {
     public static void registerGauge(final String name, final String document) {
         registerGauge(name, null, document);
     }
-    
+
     /**
      * Register histogram by label names.
      *
@@ -121,7 +126,7 @@ public final class MetricsReporter {
     public static void registerHistogram(final String name, final String[] labelNames, final String document) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.registerHistogram(name, labelNames, document));
     }
-    
+
     /**
      * Register histogram.
      *
@@ -131,7 +136,7 @@ public final class MetricsReporter {
     public static void registerHistogram(final String name, final String document) {
         registerHistogram(name, null, document);
     }
-    
+
     /**
      * Counter increment.
      *
@@ -141,7 +146,7 @@ public final class MetricsReporter {
     public static void counterIncrement(final String name, final String[] labelValues) {
         counterIncrement(name, labelValues, 1);
     }
-    
+
     /**
      * Counter increment.
      *
@@ -150,7 +155,7 @@ public final class MetricsReporter {
     public static void counterIncrement(final String name) {
         counterIncrement(name, null, 1);
     }
-    
+
     /**
      * Counter increment by count.
      *
@@ -161,7 +166,7 @@ public final class MetricsReporter {
     public static void counterIncrement(final String name, final String[] labelValues, final long count) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.counterIncrement(name, labelValues, count));
     }
-    
+
     /**
      * Gauge increment.
      *
@@ -171,7 +176,7 @@ public final class MetricsReporter {
     public static void gaugeIncrement(final String name, final String[] labelValues) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.gaugeIncrement(name, labelValues));
     }
-    
+
     /**
      * Gauge increment.
      *
@@ -180,7 +185,7 @@ public final class MetricsReporter {
     public static void gaugeIncrement(final String name) {
         gaugeIncrement(name, null);
     }
-    
+
     /**
      * Gauge decrement.
      *
@@ -190,7 +195,7 @@ public final class MetricsReporter {
     public static void gaugeDecrement(final String name, final String[] labelValues) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.gaugeDecrement(name, labelValues));
     }
-    
+
     /**
      * Gauge decrement.
      *
@@ -199,7 +204,7 @@ public final class MetricsReporter {
     public static void gaugeDecrement(final String name) {
         gaugeDecrement(name, null);
     }
-    
+
     /**
      * Record time by duration.
      *
@@ -210,7 +215,7 @@ public final class MetricsReporter {
     public static void recordTime(final String name, final String[] labelValues, final long duration) {
         Optional.ofNullable(metricsRegister).ifPresent(register -> register.recordTime(name, labelValues, duration));
     }
-    
+
     /**
      * Record time by duration.
      *
@@ -220,7 +225,7 @@ public final class MetricsReporter {
     public static void recordTime(final String name, final long duration) {
         recordTime(name, null, duration);
     }
-    
+
     /**
      * Clean.
      */
@@ -228,7 +233,7 @@ public final class MetricsReporter {
         Optional.ofNullable(metricsRegister).ifPresent(MetricsRegister::clean);
         metricsRegister = null;
     }
-    
+
     private static String[] getLabelNames(final List<String> labels) {
         return labels.toArray(new String[0]);
     }
diff --git a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
index 457b0f6266..4bb6f35dec 100644
--- a/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
+++ b/shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporterTest.java
@@ -25,10 +25,12 @@ import org.apache.shenyu.plugin.metrics.config.Metric;
 import org.apache.shenyu.plugin.metrics.enums.MetricType;
 import org.apache.shenyu.plugin.metrics.prometheus.PrometheusMetricsRegister;
 import org.apache.shenyu.plugin.metrics.spi.MetricsRegister;
+import org.junit.FixMethodOrder;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
+import org.junit.runners.MethodSorters;
 import org.springframework.util.CollectionUtils;
 
 import java.lang.reflect.Field;
@@ -40,6 +42,7 @@ import java.util.Map;
 /**
  * The Test Case For MetricsReporter.
  */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public final class MetricsReporterTest {
 
     private static final String DOCUMENT = "testDocument";
@@ -54,13 +57,9 @@ public final class MetricsReporterTest {
 
     @Test
     public void testRegister() throws Exception {
-        Field field1 = metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
-        field1.setAccessible(true);
-        Map<String, Counter> map1 = (Map<String, Counter>) field1.get(metricsRegister);
-        Assertions.assertEquals(map1.size(), 4);
-        Field field2 = metricsRegister.getClass().getDeclaredField("HISTOGRAM_MAP");
-        field2.setAccessible(true);
-        Map<String, Histogram> map2 = (Map<String, Histogram>) field2.get(metricsRegister);
+        Map<String, Counter> map1 = getPrivateField(metricsRegister, "COUNTER_MAP", Map.class);
+        Assertions.assertEquals(map1.size(), 9);
+        Map<String, Histogram> map2 = getPrivateField(metricsRegister, "HISTOGRAM_MAP", Map.class);
         Assertions.assertEquals(map2.size(), 3);
         List<String> labels = new ArrayList<>();
         labels.add("shenyu_request_total");
@@ -69,20 +68,12 @@ public final class MetricsReporterTest {
         metrics.add(new Metric(MetricType.GAUGE, "name2", DOCUMENT, labels));
         metrics.add(new Metric(MetricType.HISTOGRAM, "name3", DOCUMENT, labels));
         MetricsReporter.registerMetrics(metrics);
-        Field field3 = metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
-        field3.setAccessible(true);
-        Map<String, Counter> map3 = (Map<String, Counter>) field3.get(metricsRegister);
-        Assertions.assertEquals(map3.size(), 5);
-        Field field4 = metricsRegister.getClass().getDeclaredField("HISTOGRAM_MAP");
-        field4.setAccessible(true);
-        Map<String, Histogram> map4 = (Map<String, Histogram>) field4.get(metricsRegister);
+        Map<String, Counter> map3 = getPrivateField(metricsRegister, "COUNTER_MAP", Map.class);
+        Assertions.assertEquals(map3.size(), 10);
+        Map<String, Histogram> map4 = getPrivateField(metricsRegister, "HISTOGRAM_MAP", Map.class);
         Assertions.assertEquals(map4.size(), 4);
-        Field field5 = metricsRegister.getClass().getDeclaredField("GAUGE_MAP");
-        field5.setAccessible(true);
-        Map<String, Gauge> map5 = (Map<String, Gauge>) field5.get(metricsRegister);
+        Map<String, Gauge> map5 = getPrivateField(metricsRegister, "GAUGE_MAP", Map.class);
         Assertions.assertEquals(map5.size(), 3);
-        MetricsReporter.clean();
-        Assertions.assertTrue(CollectionUtils.isEmpty(map3));
     }
 
     @Test
@@ -118,6 +109,30 @@ public final class MetricsReporterTest {
         Assertions.assertEquals(gauge4.get(), 0.0);
     }
 
+    @Test
+    public void testCounter() throws Exception {
+        String counterName1 = "request_total1";
+        String counterName2 = "request_total2";
+        String[] labelNames = new String[]{"name1", "name2"};
+        MetricsReporter.registerCounter(counterName1, DOCUMENT);
+        MetricsReporter.registerCounter(counterName2, labelNames, DOCUMENT);
+        MetricsReporter.counterIncrement(counterName1);
+        Field field1 = metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
+        field1.setAccessible(true);
+        Map<String, Counter> counterMap = getPrivateField(metricsRegister, "COUNTER_MAP", Map.class);
+        Assertions.assertEquals(counterMap.get(counterName1).get(), 1);
+        MetricsReporter.counterIncrement(counterName2, labelNames);
+        Assertions.assertEquals(counterMap.get(counterName2).labels(labelNames).get(), 1);
+        MetricsReporter.counterIncrement(counterName2, labelNames, 2);
+        Assertions.assertEquals(counterMap.get(counterName2).labels(labelNames).get(), 3);
+    }
+
+    private <T> T getPrivateField(final Object object, final String fieldName, final Class<T> fieldType) throws NoSuchFieldException, IllegalAccessException {
+        Field field = object.getClass().getDeclaredField(fieldName);
+        field.setAccessible(true);
+        return fieldType.cast(field.get(object));
+    }
+
     @Test
     public void testRecordTime() {
         String name1 = "requests_latency_histogram_millis1";
@@ -135,6 +150,17 @@ public final class MetricsReporterTest {
         Assertions.assertEquals(histogram2.labels().get().sum, 1000.0);
     }
 
+    @Test
+    public void testZClean() throws Exception {
+        String name = "test_metrics_name";
+        MetricsReporter.registerCounter(name, DOCUMENT);
+        Map<String, Counter> counterMap1 = getPrivateField(metricsRegister, "COUNTER_MAP", Map.class);
+        Assertions.assertFalse(CollectionUtils.isEmpty(counterMap1));
+        MetricsReporter.clean();
+        Map<String, Counter> counterMap2 = getPrivateField(metricsRegister, "COUNTER_MAP", Map.class);
+        Assertions.assertTrue(CollectionUtils.isEmpty(counterMap2));
+    }
+
     @AfterAll
     public static void clean() {
         metricsRegister.clean();