Skip to content

Commit

Permalink
[huaweicloud#1066]Not generate trace id for invocations in external task
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 committed Sep 14, 2023
1 parent ba4e56b commit d18abbb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public RequestInterceptor decorateRequestInterceptor(
}

@Bean
public RequestInterceptor invocationContextRequestInterceptor() {
return new InvocationContextRequestInterceptor();
public RequestInterceptor invocationContextRequestInterceptor(ContextProperties contextProperties) {
return new InvocationContextRequestInterceptor(contextProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@

import org.springframework.core.Ordered;

import com.huaweicloud.common.configration.dynamic.ContextProperties;
import com.huaweicloud.common.context.InvocationContext;
import com.huaweicloud.common.context.InvocationContextHolder;

import feign.RequestInterceptor;
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public ClientHttpRequestInterceptor decorateClientHttpRequestInterceptor(
}

@Bean
public ClientHttpRequestInterceptor invocationContextClientHttpRequestInterceptor() {
return new InvocationContextClientHttpRequestInterceptor();
public ClientHttpRequestInterceptor invocationContextClientHttpRequestInterceptor(ContextProperties contextProperties) {
return new InvocationContextClientHttpRequestInterceptor(contextProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class ContextProperties {

private boolean useContextOperationForMetrics = false;

private boolean addTraceIdForFeign = false;

private boolean addTraceIdForTemplate = false;

public boolean isEnableTraceInfo() {
return enableTraceInfo;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d18abbb

Please sign in to comment.