|
|
@@ -1,11 +1,21 @@
|
|
|
package com.usthe.collector.collect.http;
|
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.jayway.jsonpath.Configuration;
|
|
|
+import com.jayway.jsonpath.Option;
|
|
|
+import com.jayway.jsonpath.spi.cache.CacheProvider;
|
|
|
+import com.jayway.jsonpath.spi.cache.LRUCache;
|
|
|
import com.usthe.collector.collect.AbstractCollect;
|
|
|
import com.usthe.collector.common.http.HttpClientPool;
|
|
|
import com.usthe.collector.dispatch.DispatchConstants;
|
|
|
+import com.usthe.collector.util.JsonPathParser;
|
|
|
import com.usthe.common.entity.job.Metrics;
|
|
|
import com.usthe.common.entity.job.protocol.HttpProtocol;
|
|
|
import com.usthe.common.entity.message.CollectRep;
|
|
|
+import com.usthe.common.util.CommonConstants;
|
|
|
import com.usthe.common.util.IpDomainUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.HttpHeaders;
|
|
|
@@ -82,20 +92,20 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
String parseType = metrics.getHttp().getParseType();
|
|
|
try {
|
|
|
if (DispatchConstants.PARSE_DEFAULT.equals(parseType)) {
|
|
|
- parseResponseByDefault(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
- } else if (DispatchConstants.PARSE_PROMETHEUS.equals(parseType)) {
|
|
|
- parseResponseByPrometheus(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
+ parseResponseByDefault(resp, metrics.getAliasFields(), builder);
|
|
|
} else if (DispatchConstants.PARSE_JSON_PATH.equals(parseType)) {
|
|
|
parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
+ } else if (DispatchConstants.PARSE_PROMETHEUS.equals(parseType)) {
|
|
|
+ parseResponseByPrometheus(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
} else if (DispatchConstants.PARSE_XML_PATH.equals(parseType)) {
|
|
|
parseResponseByXmlPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
} else {
|
|
|
- parseResponseByDefault(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
|
|
+ parseResponseByDefault(resp, metrics.getAliasFields(), builder);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.info("parse error: {}.", e.getMessage(), e);
|
|
|
builder.setCode(CollectRep.Code.FAIL);
|
|
|
- builder.setMsg("parse response data error.");
|
|
|
+ builder.setMsg("parse response data error:" + e.getMessage());
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
@@ -135,21 +145,71 @@ public class HttpCollectImpl extends AbstractCollect {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void parseResponseByXmlPath(String resp, List<String> preFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
|
|
|
+ private void parseResponseByXmlPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
|
+ CollectRep.MetricsData.Builder builder) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void parseResponseByJsonPath(String resp, List<String> preFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
|
|
|
-
|
|
|
+ private void parseResponseByJsonPath(String resp, List<String> aliasFields, HttpProtocol http,
|
|
|
+ CollectRep.MetricsData.Builder builder) {
|
|
|
+ List<Map<String, Object>> results = JsonPathParser.parseContentWithJsonPath(resp,http. getParseScript());
|
|
|
+ for (Map<String, Object> stringMap : results) {
|
|
|
+ CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
|
+ for (String alias : aliasFields) {
|
|
|
+ Object value = stringMap.get(alias);
|
|
|
+ if (value != null) {
|
|
|
+ valueRowBuilder.addColumns(String.valueOf(value));
|
|
|
+ } else {
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.addValues(valueRowBuilder.build());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void parseResponseByPrometheus(String resp, List<String> preFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
|
|
|
+ private void parseResponseByPrometheus(String resp, List<String> aliasFields, HttpProtocol http,
|
|
|
+ CollectRep.MetricsData.Builder builder) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void parseResponseByDefault(String resp, List<String> preFields, HttpProtocol http,
|
|
|
- CollectRep.MetricsData.Builder builder) {
|
|
|
-
|
|
|
+ private void parseResponseByDefault(String resp, List<String> aliasFields, CollectRep.MetricsData.Builder builder) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ JsonElement element = gson.toJsonTree(resp);
|
|
|
+ if (element.isJsonArray()) {
|
|
|
+ JsonArray array = element.getAsJsonArray();
|
|
|
+ for (JsonElement jsonElement : array) {
|
|
|
+ if (jsonElement.isJsonObject()) {
|
|
|
+ JsonObject object = jsonElement.getAsJsonObject();
|
|
|
+ CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
|
+ for (String alias : aliasFields) {
|
|
|
+ JsonElement valueElement = object.get(alias);
|
|
|
+ if (valueElement != null) {
|
|
|
+ String value = valueElement.getAsString();
|
|
|
+ valueRowBuilder.addColumns(value);
|
|
|
+ } else {
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.addValues(valueRowBuilder.build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (element.isJsonObject()) {
|
|
|
+ JsonObject object = element.getAsJsonObject();
|
|
|
+ CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
|
|
+ StringBuilder instance = new StringBuilder();
|
|
|
+ for (String alias : aliasFields) {
|
|
|
+ JsonElement valueElement = object.get(alias);
|
|
|
+ if (valueElement != null) {
|
|
|
+ String value = valueElement.getAsString();
|
|
|
+ valueRowBuilder.addColumns(value);
|
|
|
+ instance.append(value);
|
|
|
+ } else {
|
|
|
+ valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ valueRowBuilder.setInstance(instance.toString());
|
|
|
+ builder.addValues(valueRowBuilder.build());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|