From 5c9a8b576d02ee14c5f6e4edf31008d6013a178a Mon Sep 17 00:00:00 2001 From: aias00 Date: Sat, 20 Jul 2024 12:53:56 +0800 Subject: [PATCH] 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 Co-authored-by: xiaoyu Co-authored-by: moremind --- .../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 c84929a81110..1e806df8f50c 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 461234a6d711..460d113f9217 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 @@ 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 ShenyuWebHandler(final List plugins, final ShenyuLoaderServ } } + /** + * 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 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 ShenyuWebHandler(final List plugins, final ShenyuLoaderServ */ @Override public Mono handle(@NonNull final ServerWebExchange exchange) { - Mono execute = new DefaultShenyuPluginChain(plugins).execute(exchange); - if (scheduled) { - return execute.subscribeOn(scheduler); + try { + before(exchange); + Mono execute = new DefaultShenyuPluginChain(plugins).execute(exchange); + if (scheduled) { + return execute.subscribeOn(scheduler); + } + return execute; + } finally { + after(exchange); } - return execute; } /**