[monitor] 支持网站监控类型,降低雪花算法UUID最大值解决ts json解析大数问题
This commit is contained in:
@@ -5,10 +5,6 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* collector start
|
||||
* @author tomsun28
|
||||
@@ -19,20 +15,5 @@ import java.net.InetAddress;
|
||||
public class Collector {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Collector.class, args);
|
||||
// DatagramSocket s = new DatagramSocket();
|
||||
//
|
||||
// /** 2、提供数据,封装打包 ---DatagramPacket(byte[] buf, int length, InetAddress address, int port) */
|
||||
//
|
||||
// byte[] bs = "正在使用UDP发送--我是数据! ".getBytes();
|
||||
// DatagramPacket dp = new DatagramPacket(bs, bs.length, InetAddress.getByName("192.168.1.189"), 8070);
|
||||
//
|
||||
// /** 3、使用send发送 */
|
||||
// try {
|
||||
// s.send(dp);
|
||||
// s.receive(dp)
|
||||
// } catch (IOException e) {
|
||||
// System.out.println("发送失败: ");
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
|
||||
import com.usthe.collector.collect.AbstractCollect;
|
||||
import com.usthe.collector.common.http.HttpClientPool;
|
||||
import com.usthe.collector.dispatch.DispatchConstants;
|
||||
import com.usthe.collector.util.CollectorConstants;
|
||||
import com.usthe.collector.util.JsonPathParser;
|
||||
import com.usthe.common.entity.job.Metrics;
|
||||
import com.usthe.common.entity.job.protocol.HttpProtocol;
|
||||
@@ -56,6 +57,7 @@ public class HttpCollectImpl extends AbstractCollect {
|
||||
@Override
|
||||
public void collect(CollectRep.MetricsData.Builder builder,
|
||||
long appId, String app, Metrics metrics) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 简单校验必有参数
|
||||
if (metrics == null || metrics.getHttp() == null) {
|
||||
builder.setCode(CollectRep.Code.FAIL);
|
||||
@@ -84,18 +86,21 @@ public class HttpCollectImpl extends AbstractCollect {
|
||||
builder.setMsg("statusCode: " + statusCode + ",entity empty.");
|
||||
return;
|
||||
}
|
||||
Long responseTime = System.currentTimeMillis() - startTime;
|
||||
String parseType = metrics.getHttp().getParseType();
|
||||
try {
|
||||
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)) {
|
||||
parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
|
||||
parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder, responseTime);
|
||||
} 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 if (DispatchConstants.PARSE_WEBSITE.equals(parseType)){
|
||||
parseResponseByWebsite(resp, metrics.getAliasFields(), builder, responseTime);
|
||||
} else {
|
||||
parseResponseByDefault(resp, metrics.getAliasFields(), builder);
|
||||
parseResponseByDefault(resp, metrics.getAliasFields(), builder, responseTime);
|
||||
}
|
||||
} catch (Exception 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,
|
||||
CollectRep.MetricsData.Builder builder) {
|
||||
|
||||
}
|
||||
|
||||
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());
|
||||
for (Map<String, Object> stringMap : results) {
|
||||
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
||||
@@ -155,7 +173,11 @@ public class HttpCollectImpl extends AbstractCollect {
|
||||
if (value != null) {
|
||||
valueRowBuilder.addColumns(String.valueOf(value));
|
||||
} 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());
|
||||
@@ -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);
|
||||
if (element.isJsonArray()) {
|
||||
JsonArray array = element.getAsJsonArray();
|
||||
@@ -181,7 +204,11 @@ public class HttpCollectImpl extends AbstractCollect {
|
||||
String value = valueElement.getAsString();
|
||||
valueRowBuilder.addColumns(value);
|
||||
} 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());
|
||||
@@ -283,6 +310,8 @@ public class HttpCollectImpl extends AbstractCollect {
|
||||
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(httpProtocol.getParseType())) {
|
||||
requestBuilder.addHeader(HttpHeaders.ACCEPT, DispatchConstants.PARSE_PROMETHEUS_ACCEPT);
|
||||
requestBuilder.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip");
|
||||
} else {
|
||||
requestBuilder.addHeader(HttpHeaders.ACCEPT, "*/*");
|
||||
}
|
||||
|
||||
// 判断是否使用Bearer Token认证
|
||||
|
||||
@@ -56,6 +56,10 @@ public interface DispatchConstants {
|
||||
* 解析方式 自定义xml path
|
||||
*/
|
||||
String PARSE_XML_PATH = "xmlPath";
|
||||
/**
|
||||
* 解析方式 网站可用性监控规则 提供responseTime指标
|
||||
*/
|
||||
String PARSE_WEBSITE = "website";
|
||||
/**
|
||||
* 解析方式 prometheus规则
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.usthe.collector.util;
|
||||
|
||||
/**
|
||||
* collector 常量
|
||||
* @author tom
|
||||
* @date 2021/12/3 12:15
|
||||
*/
|
||||
public interface CollectorConstants {
|
||||
|
||||
String RESPONSE_TIME = "responseTime";
|
||||
}
|
||||
Reference in New Issue
Block a user