Skip to content

Commit

Permalink
perf: 优化local文件不存在时的报错信息 issue #2986
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyzh39 committed Dec 20, 2024
1 parent e7ff524 commit 0a30dd5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.artifactory.constants;

public class MetricsConstants {

/**
* 仅统计调用制品库 API的HTTP请求过程
*/
public static final String METRICS_NAME_BKREPO_API_HTTP = "job.client.bkrepo.api.http";
/**
* 统计调用制品库 API整个过程,含反序列化
*/
public static final String METRICS_NAME_BKREPO_API = "job.client.bkrepo.api";

public static final String TAG_KEY_API_NAME = "api_name";
public static final String TAG_KEY_STATUS = "status";

public static final String TAG_VALUE_OK = "ok";
public static final String TAG_VALUE_ERROR = "error";
public static final String TAG_VALUE_NONE = "none";
/**
* 用户请求参数错误:请求的节点路径在制品库中不存在
*/
public static final String TAG_VALUE_CLIENT_ERROR_NODE_NOT_FOUND = "client.nodeNotFound";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.bk.job.common.artifactory.constants.ArtifactoryInterfaceConsts;
import com.tencent.bk.job.common.artifactory.constants.MetricsConstants;
import com.tencent.bk.job.common.artifactory.model.dto.ArtifactoryResp;
import com.tencent.bk.job.common.artifactory.model.dto.NodeDTO;
import com.tencent.bk.job.common.artifactory.model.dto.PageData;
Expand Down Expand Up @@ -56,7 +57,6 @@
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.exception.NotImplementedException;
import com.tencent.bk.job.common.exception.ServiceException;
import com.tencent.bk.job.common.metrics.CommonMetricNames;
import com.tencent.bk.job.common.util.Base64Util;
import com.tencent.bk.job.common.util.StringUtil;
import com.tencent.bk.job.common.util.http.HttpHelper;
Expand Down Expand Up @@ -90,8 +90,8 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static com.tencent.bk.job.common.metrics.CommonMetricValues.STATUS_CLIENT_ERROR_NODE_NOT_FOUND;

@Slf4j
public class ArtifactoryClient {
Expand Down Expand Up @@ -257,10 +257,10 @@ private <R> R getArtifactoryRespByReq(
}
String respStr;
long start = System.nanoTime();
String status = "none";
AtomicReference<String> statusRef = new AtomicReference<>(MetricsConstants.TAG_VALUE_NONE);
try {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.BKREPO_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", urlTemplate));
HttpMetricUtil.setHttpMetricName(MetricsConstants.METRICS_NAME_BKREPO_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of(MetricsConstants.TAG_KEY_API_NAME, urlTemplate));
switch (method) {
case HttpGet.METHOD_NAME:
respStr = doHttpGet(url, reqBody, httpHelper);
Expand Down Expand Up @@ -288,7 +288,7 @@ private <R> R getArtifactoryRespByReq(
}
R result = JsonUtils.fromJson(respStr, typeReference);
checkResult(result, method, url, reqStr, respStr);
status = "ok";
statusRef.set(MetricsConstants.TAG_VALUE_OK);
return result;
} catch (Exception e) {
String msg = MessageFormatter.arrayFormat(
Expand All @@ -300,35 +300,20 @@ private <R> R getArtifactoryRespByReq(
}
).getMessage();
log.error(msg, e);
status = "error";
statusRef.set(MetricsConstants.TAG_VALUE_ERROR);

// 特殊处理文件 NotFound 导致的 HttpStatusException
if (e instanceof HttpStatusException) {
String httpStatusExceptionRespStr = ((HttpStatusException) e).getRespBodyStr();
ArtifactoryResp<Object> artifactoryResp = JsonUtils.fromJson(httpStatusExceptionRespStr,
new TypeReference<ArtifactoryResp<Object>>() {
});
if (artifactoryResp != null
&& artifactoryResp.getCode() == ArtifactoryInterfaceConsts.RESULT_CODE_NODE_NOT_FOUND) {
status = STATUS_CLIENT_ERROR_NODE_NOT_FOUND;
throw new InternalException(
artifactoryResp.getMessage(),
ErrorCode.CAN_NOT_FIND_NODE_IN_ARTIFACTORY
);
} else {
throw new InternalException(
"Fail to request ARTIFACTORY data",
ErrorCode.ARTIFACTORY_API_DATA_ERROR
);
}
} else {
throw new InternalException("Fail to request ARTIFACTORY data", ErrorCode.ARTIFACTORY_API_DATA_ERROR);
}
return convertException(statusRef, e);

} finally {
HttpMetricUtil.clearHttpMetric();
long end = System.nanoTime();
if (null != meterRegistry) {
meterRegistry.timer(CommonMetricNames.BKREPO_API, "api_name", urlTemplate, "status", status)
meterRegistry.timer(
MetricsConstants.METRICS_NAME_BKREPO_API,
MetricsConstants.TAG_KEY_API_NAME, urlTemplate,
MetricsConstants.TAG_KEY_STATUS, statusRef.get()
)
.record(end - start, TimeUnit.NANOSECONDS);
}
}
Expand Down Expand Up @@ -529,7 +514,7 @@ public Pair<InputStream, HttpRequestBase> getFileInputStream(String projectId, S
url = getCompleteUrl(url);
CloseableHttpResponse resp;
try {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.BKREPO_API_HTTP);
HttpMetricUtil.setHttpMetricName(MetricsConstants.METRICS_NAME_BKREPO_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", "download:" + URL_DOWNLOAD_GENERIC_FILE));
Pair<HttpRequestBase, CloseableHttpResponse> pair = longHttpHelper.getRawResp(false, url, getJsonHeaders());
resp = pair.getRight();
Expand Down Expand Up @@ -577,7 +562,7 @@ private NodeDTO uploadGenericFileWithEntity(
url = getCompleteUrl(url);
String respStr;
try {
HttpMetricUtil.setHttpMetricName(CommonMetricNames.BKREPO_API_HTTP);
HttpMetricUtil.setHttpMetricName(MetricsConstants.METRICS_NAME_BKREPO_API_HTTP);
HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", "upload:" + URL_UPLOAD_GENERIC_FILE));

respStr = longHttpHelper.requestForSuccessResp(
Expand Down Expand Up @@ -683,4 +668,28 @@ public void shutdown() {
private String getSimplifiedStrForLog(String rawStr) {
return StringUtil.substring(rawStr, 20000);
}

private <R> R convertException(AtomicReference<String> statusRef, Exception e) {
if (e instanceof HttpStatusException) {
String httpStatusExceptionRespStr = ((HttpStatusException) e).getRespBodyStr();
ArtifactoryResp<Object> artifactoryResp = JsonUtils.fromJson(httpStatusExceptionRespStr,
new TypeReference<ArtifactoryResp<Object>>() {
});
if (artifactoryResp != null
&& artifactoryResp.getCode() == ArtifactoryInterfaceConsts.RESULT_CODE_NODE_NOT_FOUND) {
statusRef.set(MetricsConstants.TAG_VALUE_CLIENT_ERROR_NODE_NOT_FOUND);
throw new InternalException(
artifactoryResp.getMessage(),
ErrorCode.CAN_NOT_FIND_NODE_IN_ARTIFACTORY
);
} else {
throw new InternalException(
"Fail to request ARTIFACTORY data",
ErrorCode.ARTIFACTORY_API_DATA_ERROR
);
}
} else {
throw new InternalException("Fail to request ARTIFACTORY data", ErrorCode.ARTIFACTORY_API_DATA_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ public class CommonMetricNames {
*/
public static final String ESB_CMDB_API_HTTP = "job.client.cmdb.api.http";

/**
* 仅统计调用制品库 API的HTTP请求过程
*/
public static final String BKREPO_API_HTTP = "job.client.bkrepo.api.http";
/**
* 统计调用制品库 API整个过程,含反序列化
*/
public static final String BKREPO_API = "job.client.bkrepo.api";


/**
* 仅统计调用权限中心后台 API的HTTP请求过程
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ public class CommonMetricValues {
* 标识一个不存在的值
*/
public static final String NONE = "none";

/**
* 用户请求参数错误:请求的节点路径在制品库中不存在
*/
public static final String STATUS_CLIENT_ERROR_NODE_NOT_FOUND = "client.nodeNotFound";
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ArtifactoryLocalFileService(

public FileDetailDTO getFileDetailFromArtifactory(String filePath) {
NodeDTO nodeDTO = getFileNodeAndHandleException(filePath);
log.debug("nodeDTpwO={}", nodeDTO);
log.debug("nodeDTO={}", nodeDTO);
if (nodeDTO == null) {
throw new InternalException(
"local file not found in artifactory",
Expand Down

0 comments on commit 0a30dd5

Please sign in to comment.