Skip to content

Commit

Permalink
Merge pull request #2432 from wangyu096/issue_2426_3.8.x
Browse files Browse the repository at this point in the history
perf: 新增 GSE2.0 API 调用 metrics #2426
  • Loading branch information
wangyu096 authored Sep 6, 2023
2 parents 6526dca + 6f4c4ae commit 7d60ca0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,45 @@
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
*/
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 <T> String postForString(String uri, T body, ExtHttpHelper httpHelper) {

if (httpHelper == null) {
httpHelper = defaultHttpHelper;
}
Expand Down Expand Up @@ -141,6 +153,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 +163,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 +181,7 @@ private <T, R> EsbResp<R> requestApiAndWrapResponse(HttpMethod httpMethod,
reqStr,
respStr
);
status = "error";
}
if (esbResp.getData() == null) {
log.warn(
Expand All @@ -189,7 +205,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 @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 7d60ca0

Please sign in to comment.