Skip to content

Commit

Permalink
improve: modify the hard-coded name of the data metrics obtained from…
Browse files Browse the repository at this point in the history
… the response header
  • Loading branch information
LiuTianyou committed Dec 15, 2024
1 parent 44daebd commit b9402cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public void collect(CollectRep.MetricsData.Builder builder,
parseResponseByWebsite(resp, metrics, metrics.getHttp(), builder, responseTime, response);
case DispatchConstants.PARSE_SITE_MAP ->
parseResponseBySiteMap(resp, metrics.getAliasFields(), builder);
case DispatchConstants.PARSE_HEADER ->
parseResponseByHeader(builder, metrics.getAliasFields(), response);
default ->
parseResponseByDefault(resp, metrics.getAliasFields(), metrics.getHttp(), builder, responseTime);
}
Expand Down Expand Up @@ -196,6 +198,24 @@ public void collect(CollectRep.MetricsData.Builder builder,
}
}

private void parseResponseByHeader(CollectRep.MetricsData.Builder builder, List<String> aliases, CloseableHttpResponse response) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String alias : aliases) {
if (!StringUtils.hasText(alias)) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
continue;
}
final Header firstHeader = response.getFirstHeader(alias);
if (Objects.isNull(firstHeader)) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
continue;
}

valueRowBuilder.addColumns(firstHeader.getValue());
}
builder.addValues(valueRowBuilder.build());
}

@Override
public String supportProtocol() {
return DispatchConstants.PROTOCOL_HTTP;
Expand All @@ -207,30 +227,11 @@ private void parseResponseByWebsite(String resp, Metrics metrics, HttpProtocol h
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
int keywordNum = CollectUtil.countMatchKeyword(resp, http.getKeyword());
for (String alias : metrics.getAliasFields()) {
if ("summary".equalsIgnoreCase(metrics.getName())) {
addColumnForSummary(responseTime, valueRowBuilder, keywordNum, alias);
} else if ("header".equalsIgnoreCase(metrics.getName())) {
addColumnFromHeader(valueRowBuilder, alias, response);
}
addColumnForSummary(responseTime, valueRowBuilder, keywordNum, alias);
}
builder.addValues(valueRowBuilder.build());
}

private void addColumnFromHeader(CollectRep.ValueRow.Builder valueRowBuilder, String alias, CloseableHttpResponse response) {
if (!StringUtils.hasText(alias)) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
return;
}

final Header firstHeader = response.getFirstHeader(alias);
if (Objects.isNull(firstHeader)) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
return;
}

valueRowBuilder.addColumns(firstHeader.getValue());
}

private void addColumnForSummary(Long responseTime, CollectRep.ValueRow.Builder valueRowBuilder, int keywordNum, String alias) {
if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
valueRowBuilder.addColumns(responseTime.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public interface DispatchConstants {
* Analysis method Sitemap site-wide availability monitoring rules
*/
String PARSE_SITE_MAP = "sitemap";
/**
* Analysis method resolution: response header
*/
String PARSE_HEADER = "header";
/**
* Parsing method prometheus exporter data
*/
Expand Down
4 changes: 2 additions & 2 deletions hertzbeat-manager/src/main/resources/define/app-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,5 @@ metrics:
digestAuthUsername: ^_^username^_^
digestAuthPassword: ^_^password^_^
# http response data parse type: default-hertzbeat rule, jsonpath-jsonpath script, website-for website monitoring, prometheus-prometheus exporter rule
parseType: website
keyword: ^_^keyword^_^
parseType: header
keyword: ^_^keyword^_^

0 comments on commit b9402cb

Please sign in to comment.