From 04ee896d6b2745410c525dc326c4f053c7187f69 Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Tue, 5 Sep 2023 17:46:28 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=96=B0=E5=A2=9E=20GSE2.0=20API=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8=20metrics=20#2426?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/esb/sdk/AbstractBkApiClient.java | 27 ++++++++++++++++--- .../common/gse/constants/GseConstants.java | 5 ++++ .../bk/job/common/gse/v2/GseV2ApiClient.java | 11 ++++++-- .../common/gse/v2/GseV2AutoConfiguration.java | 6 +++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java index 879cce9883..78c3d4e0c4 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/sdk/AbstractBkApiClient.java @@ -32,6 +32,7 @@ import com.tencent.bk.job.common.util.http.ExtHttpHelper; import com.tencent.bk.job.common.util.http.HttpHelperFactory; import com.tencent.bk.job.common.util.json.JsonUtils; +import io.micrometer.core.instrument.MeterRegistry; import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; import org.apache.http.message.BasicHeader; @@ -39,26 +40,37 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpMethod; +import java.util.concurrent.TimeUnit; + /** * 蓝鲸API调用客户端 - for BK API Gateway */ public abstract class AbstractBkApiClient { + private final Logger log = LoggerFactory.getLogger(this.getClass()); private final String bkApiGatewayUrl; private final String appSecret; private final String appCode; private final ExtHttpHelper defaultHttpHelper = HttpHelperFactory.getDefaultHttpHelper(); - - public AbstractBkApiClient(String bkApiGatewayUrl, + private final MeterRegistry meterRegistry; + /** + * API调用度量指标名称 + */ + private final String metricName; + + public AbstractBkApiClient(MeterRegistry meterRegistry, + String metricName, + String bkApiGatewayUrl, String appCode, String appSecret) { + this.meterRegistry = meterRegistry; + this.metricName = metricName; this.bkApiGatewayUrl = bkApiGatewayUrl; this.appCode = appCode; this.appSecret = appSecret; } private String postForString(String uri, T body, ExtHttpHelper httpHelper) { - if (httpHelper == null) { httpHelper = defaultHttpHelper; } @@ -141,6 +153,8 @@ private EsbResp requestApiAndWrapResponse(HttpMethod httpMethod, String reqStr = JsonUtils.toJsonWithoutSkippedFields(apiContext.getReq()); EsbResp esbResp; String respStr = null; + String status = "ok"; + long start = System.currentTimeMillis(); try { respStr = requestApi(httpMethod, uri, reqBody, httpHelper); apiContext.setOriginResp(respStr); @@ -149,6 +163,7 @@ private EsbResp requestApiAndWrapResponse(HttpMethod httpMethod, String errorMsg = "[AbstractBkApiClient] " + httpMethod.name() + " " + uri + ", error: " + "Response is blank"; log.error(errorMsg); + status = "error"; throw new InternalException(errorMsg, ErrorCode.API_ERROR); } @@ -166,6 +181,7 @@ private EsbResp requestApiAndWrapResponse(HttpMethod httpMethod, reqStr, respStr ); + status = "error"; } if (esbResp.getData() == null) { log.warn( @@ -189,7 +205,12 @@ private EsbResp requestApiAndWrapResponse(HttpMethod httpMethod, + "|respStr=" + respStr; log.error(errorMsg, e); apiContext.setSuccess(false); + status = "error"; throw new InternalException("Fail to request bk api", e, ErrorCode.API_ERROR); + } finally { + long end = System.currentTimeMillis(); + meterRegistry.timer(metricName, "api_name", uri, + "status", status).record(end - start, TimeUnit.MILLISECONDS); } } diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/GseConstants.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/GseConstants.java index 52b8e5470a..3e9fe1720e 100644 --- a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/GseConstants.java +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/constants/GseConstants.java @@ -35,6 +35,11 @@ public interface GseConstants { */ String GSE_API_METRICS_NAME_PREFIX = "job.client.gse.api"; + /** + * GSE V2 API 度量指标名称前缀 + */ + String GSE_V2_API_METRICS_NAME_PREFIX = "job.client.gse.v2.api"; + /** * GSE 获取文件任务执行结果协议版本V2 - 解除valuekey依赖版本 */ 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 7970b407eb..6d9ab51f92 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 @@ -22,6 +22,7 @@ import com.tencent.bk.job.common.gse.v2.model.resp.AgentState; import com.tencent.bk.job.common.util.StringUtil; import com.tencent.bk.job.common.util.json.JsonUtils; +import io.micrometer.core.instrument.MeterRegistry; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; @@ -37,8 +38,14 @@ public class GseV2ApiClient extends AbstractBkApiClient implements IGseClient { private static final String URI_ASYNC_TRANSFER_FILE = "/api/v2/task/async_transfer_file"; private static final String URI_GET_TRANSFER_FILE_RESULT = "/api/v2/task/async/get_transfer_file_result"; - public GseV2ApiClient(AppProperties appProperties, BkApiGatewayProperties bkApiGatewayProperties) { - super(bkApiGatewayProperties.getGse().getUrl(), appProperties.getCode(), appProperties.getSecret()); + public GseV2ApiClient(MeterRegistry meterRegistry, + AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties) { + super(meterRegistry, + GseConstants.GSE_V2_API_METRICS_NAME_PREFIX, + bkApiGatewayProperties.getGse().getUrl(), + appProperties.getCode(), + appProperties.getSecret()); log.info("Init GseV2ApiClient, bkGseApiGatewayUrl: {}, appCode: {}", bkApiGatewayProperties.getGse().getUrl(), appProperties.getCode()); } diff --git a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2AutoConfiguration.java b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2AutoConfiguration.java index cb575482b5..61a8eb2905 100644 --- a/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2AutoConfiguration.java +++ b/src/backend/commons/gse-sdk/src/main/java/com/tencent/bk/job/common/gse/v2/GseV2AutoConfiguration.java @@ -27,6 +27,7 @@ import com.tencent.bk.job.common.esb.config.AppProperties; import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; import com.tencent.bk.job.common.gse.config.GseV2Properties; +import io.micrometer.core.instrument.MeterRegistry; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -38,8 +39,9 @@ public class GseV2AutoConfiguration { @Bean("gseV2ApiClient") - public GseV2ApiClient gseV2ApiClient(AppProperties appProperties, + public GseV2ApiClient gseV2ApiClient(MeterRegistry meterRegistry, + AppProperties appProperties, BkApiGatewayProperties bkApiGatewayProperties) { - return new GseV2ApiClient(appProperties, bkApiGatewayProperties); + return new GseV2ApiClient(meterRegistry, appProperties, bkApiGatewayProperties); } }