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 5c9a8b576d Plugin lifecycle and Chain lifecycle #5475 (#5491)
5c9a8b576d is described below

commit 5c9a8b576d02ee14c5f6e4edf31008d6013a178a
Author: aias00 <rok...@163.com>
AuthorDate: Sat Jul 20 12:53:56 2024 +0800

    Plugin lifecycle and Chain lifecycle #5475 (#5491)
    
    * Plugin lifecycle and Chain lifecycle #5475
    
    * Export Selector Data should be combined with Discovery Data #5492
    
    * Revert "Export Selector Data should be combined with Discovery Data #5492"
    
    This reverts commit 457869e942a4768be5b05ca32955bb9d02be3360.
    
    ---------
    
    Co-authored-by: loongs-zhang <zhangzich...@apache.org>
    Co-authored-by: xiaoyu <xia...@apache.org>
    Co-authored-by: moremind <hefen...@apache.org>
---
 .../apache/shenyu/common/constant/Constants.java   | 10 ++++++
 .../shenyu/web/handler/ShenyuWebHandler.java       | 36 +++++++++++++++++++---
 2 files changed, 42 insertions(+), 4 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 c84929a811..1e806df8f5 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
@@ -800,6 +800,16 @@ public interface Constants {
      */
     String PLUGIN_END_TIME = "pluginEndTime:";
 
+    /**
+     * the chain start time of chain lifecycle.
+     */
+    String CHAIN_START_TIME = "chainStartTime:";
+
+    /**
+     * the chain end time of chain lifecycle.
+     */
+    String CHAIN_END_TIME = "chainEndTime:";
+
     /**
      * ratelimiter plugin metrics.
      */
diff --git 
a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/ShenyuWebHandler.java 
b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/ShenyuWebHandler.java
index 461234a6d7..460d113f92 100644
--- 
a/shenyu-web/src/main/java/org/apache/shenyu/web/handler/ShenyuWebHandler.java
+++ 
b/shenyu-web/src/main/java/org/apache/shenyu/web/handler/ShenyuWebHandler.java
@@ -20,6 +20,7 @@ package org.apache.shenyu.web.handler;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.common.config.ShenyuConfig;
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.dto.PluginData;
 import org.apache.shenyu.common.enums.PluginHandlerEventEnum;
 import org.apache.shenyu.plugin.api.ShenyuPlugin;
@@ -90,6 +91,28 @@ public final class ShenyuWebHandler implements WebHandler, 
ApplicationListener<P
         }
     }
 
+    /**
+     * Chain before operation.
+     *
+     * @param exchange context
+     */
+    public void before(final ServerWebExchange exchange) {
+        exchange.getAttributes().put(Constants.CHAIN_START_TIME, 
System.currentTimeMillis());
+    }
+
+    /**
+     * Plugin after operation.
+     *
+     * @param exchange context
+     */
+    public void after(final ServerWebExchange exchange) {
+        Map<String, Object> attributes = exchange.getAttributes();
+        long currentTimeMillis = System.currentTimeMillis();
+        long startTime = (long) attributes.get(Constants.CHAIN_START_TIME);
+        LOG.debug("shenyu chain handle uri:{}, traceId:{}, cost:{}", 
exchange.getRequest().getPath(), exchange.getLogPrefix(), currentTimeMillis - 
startTime);
+        attributes.remove(Constants.CHAIN_START_TIME);
+    }
+
     /**
      * Handle the web server exchange.
      *
@@ -98,11 +121,16 @@ public final class ShenyuWebHandler implements WebHandler, 
ApplicationListener<P
      */
     @Override
     public Mono<Void> handle(@NonNull final ServerWebExchange exchange) {
-        Mono<Void> execute = new 
DefaultShenyuPluginChain(plugins).execute(exchange);
-        if (scheduled) {
-            return execute.subscribeOn(scheduler);
+        try {
+            before(exchange);
+            Mono<Void> execute = new 
DefaultShenyuPluginChain(plugins).execute(exchange);
+            if (scheduled) {
+                return execute.subscribeOn(scheduler);
+            }
+            return execute;
+        } finally {
+            after(exchange);
         }
-        return execute;
     }
     
     /**

Reply via email to