Skip to content

Commit

Permalink
perf: 新增 GSE2.0 API 调用 metrics TencentBlueKing#2426
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Sep 6, 2023
1 parent 9ad6663 commit 281e695
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;

import java.util.concurrent.TimeUnit;

/**
* 蓝鲸API调用客户端 - for BK API Gateway
*/
Expand All @@ -48,17 +51,25 @@ public abstract class AbstractBkApiClient {
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 <T> String postForString(String uri, T body, ExtHttpHelper httpHelper) {

if (httpHelper == null) {
httpHelper = defaultHttpHelper;
}
Expand Down Expand Up @@ -141,6 +152,8 @@ private <T, R> EsbResp<R> requestApiAndWrapResponse(HttpMethod httpMethod,
String reqStr = JsonUtils.toJsonWithoutSkippedFields(apiContext.getReq());
EsbResp<R> esbResp;
String respStr = null;
String status = "ok";
long start = System.currentTimeMillis();
try {
respStr = requestApi(httpMethod, uri, reqBody, httpHelper);
apiContext.setOriginResp(respStr);
Expand All @@ -149,6 +162,7 @@ private <T, R> EsbResp<R> 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);
}

Expand All @@ -166,6 +180,7 @@ private <T, R> EsbResp<R> requestApiAndWrapResponse(HttpMethod httpMethod,
reqStr,
respStr
);
status = "error";
}
if (esbResp.getData() == null) {
log.warn(
Expand All @@ -189,7 +204,12 @@ private <T, R> EsbResp<R> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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依赖版本
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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;
Expand All @@ -40,8 +41,13 @@ public class GseV2ApiClient extends AbstractBkApiClient implements IGseClient {
private static final String URI_GET_TRANSFER_FILE_RESULT = "/api/v2/task/async/get_transfer_file_result";

@Autowired
public GseV2ApiClient(BkApiConfig bkApiConfig) {
super(bkApiConfig.getBkGseApiGatewayUrl(), bkApiConfig.getAppCode(), bkApiConfig.getAppSecret());
public GseV2ApiClient(MeterRegistry meterRegistry,
BkApiConfig bkApiConfig) {
super(meterRegistry,
GseConstants.GSE_V2_API_METRICS_NAME_PREFIX,
bkApiConfig.getBkGseApiGatewayUrl(),
bkApiConfig.getAppCode(),
bkApiConfig.getAppSecret());
log.info("Init GseV2ApiClient, bkGseApiGatewayUrl: {}, appCode: {}",
bkApiConfig.getBkGseApiGatewayUrl(), bkApiConfig.getAppCode());
}
Expand Down

0 comments on commit 281e695

Please sign in to comment.