From d18abbbfdac7e11acefaefec5e8f47674b1deb53 Mon Sep 17 00:00:00 2001 From: liubao Date: Thu, 14 Sep 2023 11:11:01 +0800 Subject: [PATCH] [#1066]Not generate trace id for invocations in external task --- .../adapters/feign/FeignConfiguration.java | 4 ++-- .../InvocationContextRequestInterceptor.java | 9 ++++++++- ...onContextClientHttpRequestInterceptor.java | 8 +++++++- .../common/adapters/web/WebConfiguration.java | 4 ++-- .../webmvc/InvocationContextFilter.java | 15 +++++++------- .../dynamic/ContextProperties.java | 20 +++++++++++++++++++ .../context/InvocationContextHolder.java | 4 ++++ 7 files changed, 51 insertions(+), 13 deletions(-) diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java index 3455a0a20..ceb8e884c 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/FeignConfiguration.java @@ -42,8 +42,8 @@ public RequestInterceptor decorateRequestInterceptor( } @Bean - public RequestInterceptor invocationContextRequestInterceptor() { - return new InvocationContextRequestInterceptor(); + public RequestInterceptor invocationContextRequestInterceptor(ContextProperties contextProperties) { + return new InvocationContextRequestInterceptor(contextProperties); } @Bean diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/InvocationContextRequestInterceptor.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/InvocationContextRequestInterceptor.java index f338b3ee4..ae8a6e541 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/InvocationContextRequestInterceptor.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/feign/InvocationContextRequestInterceptor.java @@ -19,6 +19,7 @@ import org.springframework.core.Ordered; +import com.huaweicloud.common.configration.dynamic.ContextProperties; import com.huaweicloud.common.context.InvocationContext; import com.huaweicloud.common.context.InvocationContextHolder; @@ -26,11 +27,17 @@ import feign.RequestTemplate; public class InvocationContextRequestInterceptor implements RequestInterceptor, Ordered { - public InvocationContextRequestInterceptor() { + private final ContextProperties contextProperties; + + public InvocationContextRequestInterceptor(ContextProperties contextProperties) { + this.contextProperties = contextProperties; } @Override public void apply(RequestTemplate requestTemplate) { + if (!contextProperties.isAddTraceIdForFeign()) { + return; + } InvocationContext context = InvocationContextHolder.getOrCreateInvocationContext(); if (context.getContext(InvocationContext.CONTEXT_TRACE_ID) == null) { context.putContext(InvocationContext.CONTEXT_TRACE_ID, InvocationContext.generateTraceId()); diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/InvocationContextClientHttpRequestInterceptor.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/InvocationContextClientHttpRequestInterceptor.java index d63ed7f97..09cd16875 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/InvocationContextClientHttpRequestInterceptor.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/InvocationContextClientHttpRequestInterceptor.java @@ -25,18 +25,24 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; +import com.huaweicloud.common.configration.dynamic.ContextProperties; import com.huaweicloud.common.context.InvocationContext; import com.huaweicloud.common.context.InvocationContextHolder; public class InvocationContextClientHttpRequestInterceptor implements ClientHttpRequestInterceptor, Ordered { + private final ContextProperties contextProperties; - public InvocationContextClientHttpRequestInterceptor() { + public InvocationContextClientHttpRequestInterceptor(ContextProperties contextProperties) { + this.contextProperties = contextProperties; } @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + if (!contextProperties.isAddTraceIdForTemplate()) { + return execution.execute(request, body); + } InvocationContext context = InvocationContextHolder.getOrCreateInvocationContext(); if (context.getContext(InvocationContext.CONTEXT_TRACE_ID) == null) { context.putContext(InvocationContext.CONTEXT_TRACE_ID, InvocationContext.generateTraceId()); diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/WebConfiguration.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/WebConfiguration.java index 998654805..d078802f3 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/WebConfiguration.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/web/WebConfiguration.java @@ -50,8 +50,8 @@ public ClientHttpRequestInterceptor decorateClientHttpRequestInterceptor( } @Bean - public ClientHttpRequestInterceptor invocationContextClientHttpRequestInterceptor() { - return new InvocationContextClientHttpRequestInterceptor(); + public ClientHttpRequestInterceptor invocationContextClientHttpRequestInterceptor(ContextProperties contextProperties) { + return new InvocationContextClientHttpRequestInterceptor(contextProperties); } @Bean diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/InvocationContextFilter.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/InvocationContextFilter.java index b0b33bd8e..baf2e3f03 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/InvocationContextFilter.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/adapters/webmvc/InvocationContextFilter.java @@ -19,6 +19,13 @@ import java.io.IOException; +import org.slf4j.MDC; + +import com.huaweicloud.common.configration.dynamic.ContextProperties; +import com.huaweicloud.common.context.InvocationContext; +import com.huaweicloud.common.context.InvocationContextHolder; +import com.huaweicloud.common.context.InvocationStage; + import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -27,13 +34,6 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.slf4j.MDC; - -import com.huaweicloud.common.configration.dynamic.ContextProperties; -import com.huaweicloud.common.context.InvocationContext; -import com.huaweicloud.common.context.InvocationContextHolder; -import com.huaweicloud.common.context.InvocationStage; - public class InvocationContextFilter implements Filter { private final ContextProperties contextProperties; @@ -72,6 +72,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha } finally { stage.finish(((HttpServletResponse) response).getStatus()); MDC.remove(InvocationContext.CONTEXT_TRACE_ID); + InvocationContextHolder.clearInvocationContext(); } } diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/ContextProperties.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/ContextProperties.java index a584071fb..012f5eb39 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/ContextProperties.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/configration/dynamic/ContextProperties.java @@ -47,6 +47,10 @@ public class ContextProperties { private boolean useContextOperationForMetrics = false; + private boolean addTraceIdForFeign = false; + + private boolean addTraceIdForTemplate = false; + public boolean isEnableTraceInfo() { return enableTraceInfo; } @@ -110,4 +114,20 @@ public boolean isUseContextOperationForMetrics() { public void setUseContextOperationForMetrics(boolean useContextOperationForMetrics) { this.useContextOperationForMetrics = useContextOperationForMetrics; } + + public boolean isAddTraceIdForFeign() { + return addTraceIdForFeign; + } + + public void setAddTraceIdForFeign(boolean addTraceIdForFeign) { + this.addTraceIdForFeign = addTraceIdForFeign; + } + + public boolean isAddTraceIdForTemplate() { + return addTraceIdForTemplate; + } + + public void setAddTraceIdForTemplate(boolean addTraceIdForTemplate) { + this.addTraceIdForTemplate = addTraceIdForTemplate; + } } diff --git a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/context/InvocationContextHolder.java b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/context/InvocationContextHolder.java index 9d9972d82..b96f7fe4a 100644 --- a/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/context/InvocationContextHolder.java +++ b/spring-cloud-huawei-common/src/main/java/com/huaweicloud/common/context/InvocationContextHolder.java @@ -54,6 +54,10 @@ public static String serialize(InvocationContext context) { return HeaderUtil.serialize(context.getContext()); } + public static void clearInvocationContext() { + INVOCATION_CONTEXT.set(null); + } + public static void setInvocationContext(InvocationContext context) { INVOCATION_CONTEXT.set(context); }