Skip to content

Commit

Permalink
[collector] enhancement: support user define which http statusCode sh… (
Browse files Browse the repository at this point in the history
  • Loading branch information
l646505418 authored and tomsun28 committed Mar 10, 2024
1 parent fc4d4e3 commit 13d1a91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpMethod;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -87,8 +88,8 @@

/**
* http https collect
* @author tomsun28
*
* @author tomsun28
*/
@Slf4j
public class HttpCollectImpl extends AbstractCollect {
Expand All @@ -114,14 +115,15 @@ public void collect(CollectRep.MetricsData.Builder builder,
CloseableHttpResponse response = CommonHttpClient.getHttpClient()
.execute(request, httpContext);
int statusCode = response.getStatusLine().getStatusCode();
boolean isSuccessInvoke = checkSuccessInvoke(metrics, statusCode);
log.debug("http response status: {}", statusCode);
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_BAD_REQUEST) {
// 1XX 4XX 5XX 状态码 失败
if ((statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_BAD_REQUEST) && !isSuccessInvoke) {
// 1XX 4XX 5XX 状态码且不在successCodes中的状态码 失败
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg("StatusCode " + statusCode);
return;
} else {
// 2xx 3xx 状态码 成功
// 2xx 3xx 状态码 或在successCodes中的状态码成功
// todo 这里直接将InputStream转为了String, 对于prometheus exporter大数据来说, 会生成大对象, 可能会严重影响JVM内存空间
// todo 方法一、使用InputStream进行解析, 代码改动大; 方法二、手动触发gc, 可以参考dubbo for long i
String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
Expand Down Expand Up @@ -206,6 +208,10 @@ private void validateParams(Metrics metrics) throws Exception {
|| !httpProtocol.getUrl().startsWith(RIGHT_DASH)) {
httpProtocol.setUrl(httpProtocol.getUrl() == null ? RIGHT_DASH : RIGHT_DASH + httpProtocol.getUrl().trim());
}

if (CollectionUtils.isEmpty(httpProtocol.getSuccessCodes())) {
httpProtocol.setSuccessCodes(List.of(200));
}
}

private void parseResponseByWebsite(String resp, List<String> aliasFields, HttpProtocol http,
Expand Down Expand Up @@ -581,4 +587,8 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
}
return requestBuilder.build();
}

private boolean checkSuccessInvoke(Metrics metrics, int statusCode) {
return metrics.getHttp().getSuccessCodes().stream().parallel().filter(code -> code == statusCode).findAny().isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -91,6 +92,12 @@ public class HttpProtocol {
*/
private String keyword;

/**
* http success status code. default 200
* successCode means what http response status code we consider it collect success.
*/
private List<Integer> successCodes ;

/**
* 认证信息
*/
Expand Down
1 change: 1 addition & 0 deletions manager/src/main/resources/define/app-springboot2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,5 @@ metrics:
url: ^_^base_path^_^/health
method: GET
ssl: ^_^ssl^_^
successCodes: [200, 503]
parseType: default

0 comments on commit 13d1a91

Please sign in to comment.