From 639d931100b281d3ac80c1f08bb501f00555d0fc Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Thu, 2 Jan 2025 18:45:23 +0800 Subject: [PATCH] Fix the issue where the service name is not checked for null when adding to the request header. Signed-off-by: hanbingleixue --- .../flowcontrol/retry/FeignRequestInterceptor.java | 8 ++++++-- .../sermant/flowcontrol/retry/HttpRequestInterceptor.java | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/FeignRequestInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/FeignRequestInterceptor.java index 8b0c31bd6..c285eddcb 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/FeignRequestInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/FeignRequestInterceptor.java @@ -21,6 +21,7 @@ import feign.Response; import io.sermant.core.common.LoggerFactory; import io.sermant.core.plugin.agent.entity.ExecuteContext; +import io.sermant.core.utils.StringUtils; import io.sermant.flowcontrol.common.config.ConfigConst; import io.sermant.flowcontrol.common.entity.FlowControlResult; import io.sermant.flowcontrol.common.entity.FlowControlServiceMeta; @@ -121,9 +122,12 @@ protected final ExecuteContext doBefore(ExecuteContext context) throws Exception private Request getRequest(ExecuteContext context) { final Request request = (Request) context.getArguments()[0]; + String serviceName = FlowControlServiceMeta.getInstance().getServiceName(); + if (StringUtils.isEmpty(serviceName)) { + return request; + } final HashMap> headers = new HashMap<>(request.headers()); - headers.put(ConfigConst.FLOW_REMOTE_SERVICE_NAME_HEADER_KEY, - Collections.singletonList(FlowControlServiceMeta.getInstance().getServiceName())); + headers.put(ConfigConst.FLOW_REMOTE_SERVICE_NAME_HEADER_KEY, Collections.singletonList(serviceName)); final Request newRequest = Request .create(request.method(), request.url(), headers, request.body(), request.charset()); context.getArguments()[0] = newRequest; diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/HttpRequestInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/HttpRequestInterceptor.java index 1936630b1..1422258bc 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/HttpRequestInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/io/sermant/flowcontrol/retry/HttpRequestInterceptor.java @@ -20,6 +20,7 @@ import io.github.resilience4j.retry.Retry; import io.sermant.core.common.LoggerFactory; import io.sermant.core.plugin.agent.entity.ExecuteContext; +import io.sermant.core.utils.StringUtils; import io.sermant.flowcontrol.common.config.ConfigConst; import io.sermant.flowcontrol.common.entity.FlowControlResult; import io.sermant.flowcontrol.common.entity.FlowControlServiceMeta; @@ -88,8 +89,11 @@ private Optional convertToHttpEntity(HttpRequest request) { protected final ExecuteContext doBefore(ExecuteContext context) { final FlowControlResult flowControlResult = new FlowControlResult(); final HttpRequest request = (HttpRequest) context.getObject(); - request.getHeaders().put(ConfigConst.FLOW_REMOTE_SERVICE_NAME_HEADER_KEY, - Collections.singletonList(FlowControlServiceMeta.getInstance().getServiceName())); + String serviceName = FlowControlServiceMeta.getInstance().getServiceName(); + if (!StringUtils.isEmpty(serviceName)) { + request.getHeaders().put(ConfigConst.FLOW_REMOTE_SERVICE_NAME_HEADER_KEY, + Collections.singletonList(serviceName)); + } final Optional httpRequestEntity = convertToHttpEntity(request); if (!httpRequestEntity.isPresent()) { return context;