From 71080eeb45506d0e46472814b60b0e588b6f2999 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Tue, 10 Dec 2024 09:48:00 +0800 Subject: [PATCH 1/5] =?UTF-8?q?perf:=20=E8=AF=B7=E6=B1=82GSE=E7=AD=89?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E9=9D=9E200=E6=97=B6=E6=89=93=E5=8D=B0Header=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E4=BF=A1=E6=81=AF=20#3179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 打印读取响应体异常请求的状态码与响应内容长度。 --- .../bk/job/common/util/http/BaseHttpHelper.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java index 1627804907..b053ea14fc 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java @@ -46,6 +46,7 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpCoreContext; import org.apache.http.util.EntityUtils; +import org.slf4j.helpers.MessageFormatter; import java.io.IOException; @@ -185,10 +186,12 @@ private HttpResponse execute(HttpRequestBase httpClientRequest, boolean throwExceptionWhenClientOrServerError) { int httpStatusCode = -1; String respStr = null; + Long contentLength = null; try (CloseableHttpResponse httpResponse = httpClient.execute(httpClientRequest, context)) { httpStatusCode = httpResponse.getStatusLine().getStatusCode(); HttpEntity entity = httpResponse.getEntity(); if (entity != null && entity.getContent() != null) { + contentLength = entity.getContentLength(); respStr = new String(EntityUtils.toByteArray(entity), CHARSET); } // 状态码>=400判定为失败 @@ -211,7 +214,12 @@ private HttpResponse execute(HttpRequestBase httpClientRequest, return new HttpResponse(httpStatusCode, respStr, httpResponse.getAllHeaders()); } } catch (IOException e) { - log.error("Request fail", e); + String message = MessageFormatter.format( + "Request fail, httpStatusCode={}, contentLength={}", + httpStatusCode, + contentLength + ).getMessage(); + log.error(message, e); throw new InternalException(e, ErrorCode.API_ERROR); } finally { httpClientRequest.releaseConnection(); From 6a5b173fd5d91591304ab47d2ef608586190117f Mon Sep 17 00:00:00 2001 From: jsonwan Date: Tue, 10 Dec 2024 11:18:50 +0800 Subject: [PATCH 2/5] =?UTF-8?q?perf:=20=E8=AF=B7=E6=B1=82GSE=E7=AD=89?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E9=9D=9E200=E6=97=B6=E6=89=93=E5=8D=B0Header=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E4=BF=A1=E6=81=AF=20#3179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 打印响应Header中的X-Bkapi-Request-Id,用于问题定位。 --- .../job/common/aidev/impl/BkAIDevClient.java | 4 +- .../job/common/iam/client/EsbIamClient.java | 9 +++- .../common/util/http/HttpHelperFactory.java | 22 ++++++-- .../LogBkApiRequestIdInterceptor.java | 52 +++++++++++++++++++ .../bk/job/common/esb/sdk/BkApiClient.java | 11 ++++ .../bk/job/common/gse/v2/GseV2ApiClient.java | 3 +- .../common/notice/impl/BkNoticeClient.java | 4 +- .../job/common/paas/cmsi/CmsiApiClient.java | 10 +++- .../paas/login/StandardLoginClient.java | 10 +++- 9 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java diff --git a/src/backend/commons/ai-dev-sdk/src/main/java/com/tencent/bk/job/common/aidev/impl/BkAIDevClient.java b/src/backend/commons/ai-dev-sdk/src/main/java/com/tencent/bk/job/common/aidev/impl/BkAIDevClient.java index b7a1b2ec7d..49da3a453b 100644 --- a/src/backend/commons/ai-dev-sdk/src/main/java/com/tencent/bk/job/common/aidev/impl/BkAIDevClient.java +++ b/src/backend/commons/ai-dev-sdk/src/main/java/com/tencent/bk/job/common/aidev/impl/BkAIDevClient.java @@ -73,7 +73,9 @@ public BkAIDevClient(MeterRegistry meterRegistry, meterRegistry, CommonMetricNames.BK_AI_DEV_API, getBkAIDevUrlSafely(bkApiGatewayProperties), - HttpHelperFactory.getDefaultHttpHelper() + HttpHelperFactory.getDefaultHttpHelper( + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) + ) ); this.appProperties = appProperties; this.bkAIDevConfig = bkApiGatewayProperties.getBkAIDev(); diff --git a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/EsbIamClient.java b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/EsbIamClient.java index 2e03388d89..d8bc7910a1 100644 --- a/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/EsbIamClient.java +++ b/src/backend/commons/common-iam/src/main/java/com/tencent/bk/job/common/iam/client/EsbIamClient.java @@ -79,7 +79,14 @@ public class EsbIamClient extends BkApiClient implements IIamClient { public EsbIamClient(MeterRegistry meterRegistry, AppProperties appProperties, EsbProperties esbProperties) { - super(meterRegistry, IAM_API, esbProperties.getService().getUrl(), HttpHelperFactory.getDefaultHttpHelper()); + super( + meterRegistry, + IAM_API, + esbProperties.getService().getUrl(), + HttpHelperFactory.getDefaultHttpHelper( + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) + ) + ); this.authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret(), "admin"); } diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/HttpHelperFactory.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/HttpHelperFactory.java index 33ed7d4799..9e35fa6466 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/HttpHelperFactory.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/HttpHelperFactory.java @@ -62,7 +62,21 @@ private static WatchableHttpHelper getWatchableExtHelper(HttpHelper httpHelper) } public static WatchableHttpHelper getDefaultHttpHelper() { - HttpHelper baseHttpHelper = new BaseHttpHelper(DEFAULT_HTTP_CLIENT); + return getDefaultHttpHelper(null); + } + + public static WatchableHttpHelper getDefaultHttpHelper(JobHttpClientFactory.HttpClientCustomizer customizer) { + HttpHelper baseHttpHelper = createHttpHelper( + 15000, + 15000, + 15000, + 500, + 1000, + 60, + false, + null, + customizer + ); return getWatchableExtHelper(baseHttpHelper); } @@ -84,7 +98,8 @@ public static HttpHelper createHttpHelper(int connRequestTimeout, int maxConnTotal, int timeToLive, boolean allowRetry, - HttpRequestRetryHandler retryHandler) { + HttpRequestRetryHandler retryHandler, + JobHttpClientFactory.HttpClientCustomizer customizer) { CloseableHttpClient httpClient = JobHttpClientFactory.createHttpClient( connRequestTimeout, connTimeout, @@ -93,7 +108,8 @@ public static HttpHelper createHttpHelper(int connRequestTimeout, maxConnTotal, timeToLive, allowRetry, - retryHandler); + retryHandler, + customizer); return new BaseHttpHelper(httpClient); } } diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java new file mode 100644 index 0000000000..1ac801cef5 --- /dev/null +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java @@ -0,0 +1,52 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.esb.interceptor; + +import com.tencent.bk.job.common.constant.JobCommonHeaders; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpResponse; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.protocol.HttpContext; + +/** + * 打印APIGW请求响应Header中的X-Bkapi-Request-Id,用于问题定位 + */ +@Slf4j +public class LogBkApiRequestIdInterceptor implements HttpResponseInterceptor { + @Override + public void process(HttpResponse response, HttpContext context) { + // 获取并打印响应头X-Bkapi-Request-Id + String headerName = JobCommonHeaders.BK_GATEWAY_REQUEST_ID; + try { + if (response.containsHeader(headerName)) { + log.info(headerName + "=" + response.getFirstHeader(headerName).getValue()); + } else { + log.debug(headerName + " not found in the response"); + } + } catch (Throwable t) { + log.warn("Failed to log header " + headerName, t); + } + } +} diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/BkApiClient.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/BkApiClient.java index afc332d1bc..4888ff8aec 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/BkApiClient.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/BkApiClient.java @@ -29,6 +29,7 @@ import com.tencent.bk.job.common.constant.HttpMethodEnum; import com.tencent.bk.job.common.constant.JobCommonHeaders; import com.tencent.bk.job.common.esb.constants.EsbLang; +import com.tencent.bk.job.common.esb.interceptor.LogBkApiRequestIdInterceptor; import com.tencent.bk.job.common.esb.metrics.EsbMetricTags; import com.tencent.bk.job.common.esb.model.BkApiAuthorization; import com.tencent.bk.job.common.esb.model.EsbResp; @@ -46,6 +47,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; +import org.apache.http.HttpResponseInterceptor; import org.apache.http.message.BasicHeader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -391,4 +393,13 @@ protected Collection getExtraMetricsTags() { return null; } + /** + * 获取打印APIGW RequestId的响应拦截器 + * + * @return 响应拦截器 + */ + protected static HttpResponseInterceptor getLogBkApiRequestIdInterceptor() { + return new LogBkApiRequestIdInterceptor(); + } + } diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2ApiClient.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2ApiClient.java index a604b57f50..bba3048092 100644 --- a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2ApiClient.java +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2ApiClient.java @@ -89,7 +89,8 @@ public GseV2ApiClient(MeterRegistry meterRegistry, 2000, 60, true, - new JobHttpRequestRetryHandler() + new JobHttpRequestRetryHandler(), + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) ) ); gseBkApiAuthorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret()); diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java index bb5cb53d04..8fb20b625e 100644 --- a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java @@ -66,7 +66,9 @@ public BkNoticeClient(MeterRegistry meterRegistry, meterRegistry, CommonMetricNames.BK_NOTICE_API, getBkNoticeUrlSafely(bkApiGatewayProperties), - HttpHelperFactory.getDefaultHttpHelper() + HttpHelperFactory.getDefaultHttpHelper( + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) + ) ); this.appProperties = appProperties; authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret()); diff --git a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/cmsi/CmsiApiClient.java b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/cmsi/CmsiApiClient.java index 599495f900..b3ff7d292c 100644 --- a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/cmsi/CmsiApiClient.java +++ b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/cmsi/CmsiApiClient.java @@ -67,8 +67,14 @@ public class CmsiApiClient extends BkApiClient { public CmsiApiClient(EsbProperties esbProperties, AppProperties appProperties, MeterRegistry meterRegistry) { - super(meterRegistry, ESB_CMSI_API, esbProperties.getService().getUrl(), - HttpHelperFactory.getDefaultHttpHelper()); + super( + meterRegistry, + ESB_CMSI_API, + esbProperties.getService().getUrl(), + HttpHelperFactory.getDefaultHttpHelper( + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) + ) + ); this.authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret(), "admin"); } diff --git a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/login/StandardLoginClient.java b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/login/StandardLoginClient.java index eda0287a18..7e88597d6f 100644 --- a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/login/StandardLoginClient.java +++ b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/login/StandardLoginClient.java @@ -61,8 +61,14 @@ public class StandardLoginClient extends BkApiClient implements ILoginClient { private final AppProperties appProperties; public StandardLoginClient(EsbProperties esbProperties, AppProperties appProperties, MeterRegistry meterRegistry) { - super(meterRegistry, ESB_BK_LOGIN_API, esbProperties.getService().getUrl(), - HttpHelperFactory.getDefaultHttpHelper()); + super( + meterRegistry, + ESB_BK_LOGIN_API, + esbProperties.getService().getUrl(), + HttpHelperFactory.getDefaultHttpHelper( + httpClientBuilder -> httpClientBuilder.addInterceptorLast(getLogBkApiRequestIdInterceptor()) + ) + ); this.appProperties = appProperties; } From 4ffc3ac38c7b8677bfd479048b968ceec1a96872 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Tue, 10 Dec 2024 16:14:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?perf:=20=E8=AF=B7=E6=B1=82GSE=E7=AD=89?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E9=9D=9E200=E6=97=B6=E6=89=93=E5=8D=B0Header=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E4=BF=A1=E6=81=AF=20#3179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 状态码为200的请求不打印响应Header中的X-Bkapi-Request-Id,减少日志量。 --- .../esb/interceptor/LogBkApiRequestIdInterceptor.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java index 1ac801cef5..35dc533c28 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java @@ -28,6 +28,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; import org.apache.http.protocol.HttpContext; /** @@ -37,6 +39,11 @@ public class LogBkApiRequestIdInterceptor implements HttpResponseInterceptor { @Override public void process(HttpResponse response, HttpContext context) { + StatusLine statusLine = response.getStatusLine(); + // 状态码为200的请求会在上层逻辑打印响应头X-Bkapi-Request-Id,此处不打印减少日志量 + if (statusLine != null && statusLine.getStatusCode() == HttpStatus.SC_OK) { + return; + } // 获取并打印响应头X-Bkapi-Request-Id String headerName = JobCommonHeaders.BK_GATEWAY_REQUEST_ID; try { From ff1c92d5fa03d07f216511a622332adef6e91dba Mon Sep 17 00:00:00 2001 From: jsonwan Date: Wed, 11 Dec 2024 10:00:15 +0800 Subject: [PATCH 4/5] =?UTF-8?q?perf:=20=E8=AF=B7=E6=B1=82GSE=E7=AD=89?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E9=9D=9E200=E6=97=B6=E6=89=93=E5=8D=B0Header=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E4=BF=A1=E6=81=AF=20#3179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 状态码非200的请求打印响应Header中不存在X-Bkapi-Request-Id提示。 --- .../common/esb/interceptor/LogBkApiRequestIdInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java index 35dc533c28..e27148907a 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java @@ -50,7 +50,7 @@ public void process(HttpResponse response, HttpContext context) { if (response.containsHeader(headerName)) { log.info(headerName + "=" + response.getFirstHeader(headerName).getValue()); } else { - log.debug(headerName + " not found in the response"); + log.info(headerName + " not found in the response"); } } catch (Throwable t) { log.warn("Failed to log header " + headerName, t); From 6801a667d4232bdb1b3543acd626f2b2c188dc1e Mon Sep 17 00:00:00 2001 From: jsonwan Date: Wed, 11 Dec 2024 10:11:13 +0800 Subject: [PATCH 5/5] =?UTF-8?q?perf:=20=E8=AF=B7=E6=B1=82GSE=E7=AD=89?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E9=9D=9E200=E6=97=B6=E6=89=93=E5=8D=B0Header=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E4=BF=A1=E6=81=AF=20#3179?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 状态码为2xx的请求不打印X-Bkapi-Request-Id。 --- .../LogBkApiRequestIdInterceptor.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java index e27148907a..e9c5b82edf 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/interceptor/LogBkApiRequestIdInterceptor.java @@ -40,8 +40,8 @@ public class LogBkApiRequestIdInterceptor implements HttpResponseInterceptor { @Override public void process(HttpResponse response, HttpContext context) { StatusLine statusLine = response.getStatusLine(); - // 状态码为200的请求会在上层逻辑打印响应头X-Bkapi-Request-Id,此处不打印减少日志量 - if (statusLine != null && statusLine.getStatusCode() == HttpStatus.SC_OK) { + // 状态码为2xx的请求会在上层逻辑打印响应头X-Bkapi-Request-Id,此处不打印减少日志量 + if (isStatusCodeSuccess(statusLine)) { return; } // 获取并打印响应头X-Bkapi-Request-Id @@ -56,4 +56,16 @@ public void process(HttpResponse response, HttpContext context) { log.warn("Failed to log header " + headerName, t); } } + + /** + * 判断Http状态码是否为成功(2xx)系列状态码 + * + * @param statusLine 状态行 + * @return 是则返回true,否则返回false + */ + private boolean isStatusCodeSuccess(StatusLine statusLine) { + return statusLine != null + && statusLine.getStatusCode() >= HttpStatus.SC_OK + && statusLine.getStatusCode() < HttpStatus.SC_MULTIPLE_CHOICES; + } }