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

xiaoyu 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 42d9f11f06 [type:feature] enhance metrics-ratelimiter collect (#5461)
42d9f11f06 is described below

commit 42d9f11f06afe91e37569460f9101c6370641883
Author: moremind <hefen...@apache.org>
AuthorDate: Mon Mar 11 10:00:28 2024 +0800

    [type:feature] enhance metrics-ratelimiter collect (#5461)
    
    * [type:refactor] enhance metrics-ratelimiter collect
    
    * [type:refactor] enhance metrics-ratelimiter collect
    
    ---------
    
    Co-authored-by: xiaoyu <xia...@apache.org>
    Co-authored-by: loongs-zhang <zhangzich...@apache.org>
---
 .../main/java/org/apache/shenyu/common/constant/Constants.java    | 7 ++++++-
 .../org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java   | 4 ++++
 .../main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java | 8 ++++++++
 .../org/apache/shenyu/plugin/metrics/constant/LabelNames.java     | 5 +++++
 .../apache/shenyu/plugin/metrics/reporter/MetricsReporter.java    | 1 +
 .../shenyu/plugin/metrics/reporter/MetricsReporterTest.java       | 4 ++--
 6 files changed, 26 insertions(+), 3 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 10f97a5c94..a3c4193e70 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
@@ -799,7 +799,12 @@ public interface Constants {
      * the plugin end time of plugin lifecycle.
      */
     String PLUGIN_END_TIME = "pluginEndTime:";
-    
+
+    /**
+     * ratelimiter plugin metrics.
+     */
+    String METRICS_RATE_LIMITER = "metricsRateLimiter";
+
     /**
      * String q.
      */
diff --git 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
index 2eb2cbeb8f..888133c25f 100644
--- 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.plugin.ratelimiter;
 
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.dto.RuleData;
 import org.apache.shenyu.common.dto.SelectorData;
 import org.apache.shenyu.common.dto.convert.rule.RateLimiterHandle;
@@ -37,6 +38,7 @@ import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 
 import java.util.Optional;
+import java.util.function.Consumer;
 
 /**
  * RateLimiter Plugin.
@@ -75,6 +77,8 @@ public class RateLimiterPlugin extends AbstractShenyuPlugin {
                 .flatMap(response -> {
                     if (!response.isAllowed()) {
                         
exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
+                        final Consumer<HttpStatus> consumer = 
exchange.getAttribute(Constants.METRICS_RATE_LIMITER);
+                        Optional.ofNullable(consumer).ifPresent(c -> 
c.accept(exchange.getResponse().getStatusCode()));
                         Object error = ShenyuResultWrap.error(exchange, 
ShenyuResultEnum.TOO_MANY_REQUESTS);
                         return WebFluxResultUtils.result(exchange, error);
                     }
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 c8d2d54578..f9cc5f545d 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
@@ -25,12 +25,15 @@ import org.apache.shenyu.plugin.api.ShenyuPluginChain;
 import org.apache.shenyu.plugin.api.context.ShenyuContext;
 import org.apache.shenyu.plugin.metrics.constant.LabelNames;
 import org.apache.shenyu.plugin.metrics.reporter.MetricsReporter;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.server.reactive.ServerHttpResponse;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 
 import java.time.LocalDateTime;
+import java.util.Objects;
 import java.util.Optional;
+import java.util.function.Consumer;
 
 /**
  * the monitor plugin.
@@ -44,6 +47,11 @@ public class MetricsPlugin implements ShenyuPlugin {
         assert shenyuContext != null;
         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);
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 2d515df154..d73b4e3260 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
@@ -41,4 +41,9 @@ public final class LabelNames {
      * The constant EXECUTE_LATENCY_NAME.
      */
     public static final String EXECUTE_LATENCY_NAME = 
"shenyu_execute_latency_millis";
+
+    /**
+     * The constant RATELIMITER_REQUEST_RESTRICT_TOTAL.
+     */
+    public static final String RATELIMITER_REQUEST_RESTRICT_TOTAL = 
"shenyu_ratelimiter_request_restrict_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 737344566c..d18d7779d9 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
@@ -43,6 +43,7 @@ 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.RATELIMITER_REQUEST_RESTRICT_TOTAL, 
"shenyu ratelimiter request restrict total count");
     }
     
     /**
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 946bb72dd3..457b0f6266 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
@@ -57,7 +57,7 @@ public final class MetricsReporterTest {
         Field field1 = 
metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
         field1.setAccessible(true);
         Map<String, Counter> map1 = (Map<String, Counter>) 
field1.get(metricsRegister);
-        Assertions.assertEquals(map1.size(), 3);
+        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);
@@ -72,7 +72,7 @@ public final class MetricsReporterTest {
         Field field3 = 
metricsRegister.getClass().getDeclaredField("COUNTER_MAP");
         field3.setAccessible(true);
         Map<String, Counter> map3 = (Map<String, Counter>) 
field3.get(metricsRegister);
-        Assertions.assertEquals(map3.size(), 4);
+        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);

Reply via email to