|
@@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
|
|
|
import com.usthe.collector.collect.AbstractCollect;
|
|
import com.usthe.collector.collect.AbstractCollect;
|
|
|
import com.usthe.collector.common.http.HttpClientPool;
|
|
import com.usthe.collector.common.http.HttpClientPool;
|
|
|
import com.usthe.collector.dispatch.DispatchConstants;
|
|
import com.usthe.collector.dispatch.DispatchConstants;
|
|
|
|
|
+import com.usthe.collector.util.CollectorConstants;
|
|
|
import com.usthe.collector.util.JsonPathParser;
|
|
import com.usthe.collector.util.JsonPathParser;
|
|
|
import com.usthe.common.entity.job.Metrics;
|
|
import com.usthe.common.entity.job.Metrics;
|
|
|
import com.usthe.common.entity.job.protocol.HttpProtocol;
|
|
import com.usthe.common.entity.job.protocol.HttpProtocol;
|
|
@@ -56,6 +57,7 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
@Override
|
|
@Override
|
|
|
public void collect(CollectRep.MetricsData.Builder builder,
|
|
public void collect(CollectRep.MetricsData.Builder builder,
|
|
|
long appId, String app, Metrics metrics) {
|
|
long appId, String app, Metrics metrics) {
|
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
// 简单校验必有参数
|
|
// 简单校验必有参数
|
|
|
if (metrics == null || metrics.getHttp() == null) {
|
|
if (metrics == null || metrics.getHttp() == null) {
|
|
|
builder.setCode(CollectRep.Code.FAIL);
|
|
builder.setCode(CollectRep.Code.FAIL);
|
|
@@ -84,18 +86,21 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
builder.setMsg("statusCode: " + statusCode + ",entity empty.");
|
|
builder.setMsg("statusCode: " + statusCode + ",entity empty.");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ Long responseTime = System.currentTimeMillis() - startTime;
|
|
|
String parseType = metrics.getHttp().getParseType();
|
|
String parseType = metrics.getHttp().getParseType();
|
|
|
try {
|
|
try {
|
|
|
if (DispatchConstants.PARSE_DEFAULT.equals(parseType)) {
|
|
if (DispatchConstants.PARSE_DEFAULT.equals(parseType)) {
|
|
|
- parseResponseByDefault(resp, metrics.getAliasFields(), builder);
|
|
|
|
|
|
|
+ parseResponseByDefault(resp, metrics.getAliasFields(), builder, responseTime);
|
|
|
} else if (DispatchConstants.PARSE_JSON_PATH.equals(parseType)) {
|
|
} else if (DispatchConstants.PARSE_JSON_PATH.equals(parseType)) {
|
|
|
- parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
|
|
|
|
+ parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder, responseTime);
|
|
|
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(parseType)) {
|
|
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(parseType)) {
|
|
|
parseResponseByPrometheus(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
parseResponseByPrometheus(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
} else if (DispatchConstants.PARSE_XML_PATH.equals(parseType)) {
|
|
} else if (DispatchConstants.PARSE_XML_PATH.equals(parseType)) {
|
|
|
parseResponseByXmlPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
parseResponseByXmlPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
|
|
+ } else if (DispatchConstants.PARSE_WEBSITE.equals(parseType)){
|
|
|
|
|
+ parseResponseByWebsite(resp, metrics.getAliasFields(), builder, responseTime);
|
|
|
} else {
|
|
} else {
|
|
|
- parseResponseByDefault(resp, metrics.getAliasFields(), builder);
|
|
|
|
|
|
|
+ parseResponseByDefault(resp, metrics.getAliasFields(), builder, responseTime);
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.info("parse error: {}.", e.getMessage(), e);
|
|
log.info("parse error: {}.", e.getMessage(), e);
|
|
@@ -140,13 +145,26 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void parseResponseByWebsite(String resp, List<String> aliasFields,
|
|
|
|
|
+ CollectRep.MetricsData.Builder builder, Long responseTime) {
|
|
|
|
|
+ CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
|
|
|
+ // todo resp 网站关键字监测
|
|
|
|
|
+ for (String alias : aliasFields) {
|
|
|
|
|
+ if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
|
|
|
|
|
+ valueRowBuilder.addColumns(responseTime.toString());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ builder.addValues(valueRowBuilder.build());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private void parseResponseByXmlPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
private void parseResponseByXmlPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
|
CollectRep.MetricsData.Builder builder) {
|
|
CollectRep.MetricsData.Builder builder) {
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void parseResponseByJsonPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
private void parseResponseByJsonPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
|
- CollectRep.MetricsData.Builder builder) {
|
|
|
|
|
|
|
+ CollectRep.MetricsData.Builder builder, Long responseTime) {
|
|
|
List<Map<String, Object>> results = JsonPathParser.parseContentWithJsonPath(resp,http. getParseScript());
|
|
List<Map<String, Object>> results = JsonPathParser.parseContentWithJsonPath(resp,http. getParseScript());
|
|
|
for (Map<String, Object> stringMap : results) {
|
|
for (Map<String, Object> stringMap : results) {
|
|
|
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
@@ -155,7 +173,11 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
if (value != null) {
|
|
if (value != null) {
|
|
|
valueRowBuilder.addColumns(String.valueOf(value));
|
|
valueRowBuilder.addColumns(String.valueOf(value));
|
|
|
} else {
|
|
} else {
|
|
|
- valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
|
|
|
|
+ if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
|
|
|
|
|
+ valueRowBuilder.addColumns(responseTime.toString());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
builder.addValues(valueRowBuilder.build());
|
|
builder.addValues(valueRowBuilder.build());
|
|
@@ -167,7 +189,8 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void parseResponseByDefault(String resp, List<String> aliasFields, CollectRep.MetricsData.Builder builder) {
|
|
|
|
|
|
|
+ private void parseResponseByDefault(String resp, List<String> aliasFields,
|
|
|
|
|
+ CollectRep.MetricsData.Builder builder, Long responseTime) {
|
|
|
JsonElement element = JsonParser.parseString(resp);
|
|
JsonElement element = JsonParser.parseString(resp);
|
|
|
if (element.isJsonArray()) {
|
|
if (element.isJsonArray()) {
|
|
|
JsonArray array = element.getAsJsonArray();
|
|
JsonArray array = element.getAsJsonArray();
|
|
@@ -181,7 +204,11 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
String value = valueElement.getAsString();
|
|
String value = valueElement.getAsString();
|
|
|
valueRowBuilder.addColumns(value);
|
|
valueRowBuilder.addColumns(value);
|
|
|
} else {
|
|
} else {
|
|
|
- valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
|
|
|
|
+ if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
|
|
|
|
|
+ valueRowBuilder.addColumns(responseTime.toString());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
builder.addValues(valueRowBuilder.build());
|
|
builder.addValues(valueRowBuilder.build());
|
|
@@ -283,6 +310,8 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(httpProtocol.getParseType())) {
|
|
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(httpProtocol.getParseType())) {
|
|
|
requestBuilder.addHeader(HttpHeaders.ACCEPT, DispatchConstants.PARSE_PROMETHEUS_ACCEPT);
|
|
requestBuilder.addHeader(HttpHeaders.ACCEPT, DispatchConstants.PARSE_PROMETHEUS_ACCEPT);
|
|
|
requestBuilder.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip");
|
|
requestBuilder.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ requestBuilder.addHeader(HttpHeaders.ACCEPT, "*/*");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 判断是否使用Bearer Token认证
|
|
// 判断是否使用Bearer Token认证
|