From 58354b648fb178fae486cb8ba2bfa04ae8a12867 Mon Sep 17 00:00:00 2001
From: cuipiheqiuqiu <76642201+cuipiheqiuqiu@users.noreply.github.com>
Date: Mon, 7 Nov 2022 23:48:19 +0800
Subject: [PATCH] WIP:feature support k8s monitor, http monitor nacos,
service&http_micro monitor msa (#421)
[collector] bugfix: Solve imprecise cyclicJob execution interval problem.
1.wheelTimer tickDuration from 10s changed to 1s,ticksPerWheel remain unchanged.
2.now support second level job interval.
[manager] elasticsearch support ssl as an option
[monitor]
1.K8sClient monitor k8s
2.http monitor nacos
3.service&http_micro monitor msa
[collector]fix pmd problem
---
collector/pom.xml | 11 +
.../collect/AbstractParseResponse.java | 80 ++++
.../collect/http/HttpCollectImpl.java | 6 +-
.../MicroServiceActuatorHttpCollectImpl.java | 353 ++++++++++++++
.../http/micro/AbstractMicroParse.java | 109 +++++
.../collect/http/micro/MicroCommonParse.java | 91 ++++
.../collect/http/micro/MicroLastParser.java | 78 +++
.../collect/http/micro/MicroParseCreater.java | 28 ++
.../http/micro/MicroRequestsParse.java | 57 +++
.../collector/collect/k8s/K8sCollectImpl.java | 240 ++++++++++
.../collect/k8s/K8sMetricsModel.java | 35 ++
.../collector/collect/k8s/K8sMetricsUtil.java | 305 ++++++++++++
.../microservice/JsonPathParseResponse.java | 134 ++++++
.../MicroServiceParentCollectImpl.java | 113 +++++
.../parse/DefaultParseResponse.java | 75 +++
.../parse/MicroParseResponse.java | 38 ++
.../parse/PrometheusParseResponse.java | 32 ++
.../parse/SiteMapParseResponse.java | 133 ++++++
.../parse/WebsiteParseResponse.java | 44 ++
.../parse/XmlPathParseResponse.java | 29 ++
.../strategy/CollectStrategyFactory.java | 44 ++
.../strategy/ParseStrategyFactory.java | 46 ++
.../collector/dispatch/DispatchConstants.java | 37 ++
.../collector/dispatch/MetricsCollect.java | 13 +
.../usthe/collector/util/JsonPathParser.java | 46 +-
.../com/usthe/collector/util/K8sClient.java | 119 +++++
.../com/usthe/common/entity/job/Metrics.java | 108 ++++-
.../entity/job/protocol/HttpProtocol.java | 3 +
.../entity/job/protocol/K8sProtocol.java | 36 ++
.../entity/job/protocol/ServiceProtocol.java | 45 ++
.../usthe/common/model/ServicePodModel.java | 52 ++
.../java/com/usthe/common/util/GsonUtil.java | 9 +
.../define/app/app-elasticsearch.yml | 8 +
.../main/resources/define/app/appmanager.yml | 448 ++++++++++++++++++
manager/src/main/resources/define/app/k8s.yml | 93 ++++
.../src/main/resources/define/app/nacos.yml | 57 +++
.../resources/define/param/appmanager.yml | 24 +
.../src/main/resources/define/param/k8s.yml | 20 +
.../src/main/resources/define/param/nacos.yml | 14 +
.../define/param/param-elasticsearch.yml | 7 +
40 files changed, 3208 insertions(+), 12 deletions(-)
create mode 100644 collector/src/main/java/com/usthe/collector/collect/AbstractParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/MicroServiceActuatorHttpCollectImpl.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/micro/AbstractMicroParse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/micro/MicroCommonParse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/micro/MicroLastParser.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/micro/MicroParseCreater.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/http/micro/MicroRequestsParse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/k8s/K8sCollectImpl.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/k8s/K8sMetricsModel.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/k8s/K8sMetricsUtil.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/JsonPathParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/MicroServiceParentCollectImpl.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/DefaultParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/MicroParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/PrometheusParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/SiteMapParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/WebsiteParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/microservice/parse/XmlPathParseResponse.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/strategy/CollectStrategyFactory.java
create mode 100644 collector/src/main/java/com/usthe/collector/collect/strategy/ParseStrategyFactory.java
create mode 100644 collector/src/main/java/com/usthe/collector/util/K8sClient.java
create mode 100644 common/src/main/java/com/usthe/common/entity/job/protocol/K8sProtocol.java
create mode 100644 common/src/main/java/com/usthe/common/entity/job/protocol/ServiceProtocol.java
create mode 100644 common/src/main/java/com/usthe/common/model/ServicePodModel.java
create mode 100644 manager/src/main/resources/define/app/appmanager.yml
create mode 100644 manager/src/main/resources/define/app/k8s.yml
create mode 100644 manager/src/main/resources/define/app/nacos.yml
create mode 100644 manager/src/main/resources/define/param/appmanager.yml
create mode 100644 manager/src/main/resources/define/param/k8s.yml
create mode 100644 manager/src/main/resources/define/param/nacos.yml
diff --git a/collector/pom.xml b/collector/pom.xml
index 665e549d46d..b90785b57a2 100644
--- a/collector/pom.xml
+++ b/collector/pom.xml
@@ -49,6 +49,11 @@
1.0
provided
+
+
+ org.springframework.boot
+ spring-boot-starter-validation
+
io.etcd
@@ -148,6 +153,12 @@
snmp4j
3.6.7
+
+
+ io.kubernetes
+ client-java
+ 10.0.1
+
\ No newline at end of file
diff --git a/collector/src/main/java/com/usthe/collector/collect/AbstractParseResponse.java b/collector/src/main/java/com/usthe/collector/collect/AbstractParseResponse.java
new file mode 100644
index 00000000000..c2e76adb10e
--- /dev/null
+++ b/collector/src/main/java/com/usthe/collector/collect/AbstractParseResponse.java
@@ -0,0 +1,80 @@
+package com.usthe.collector.collect;
+
+import com.usthe.common.entity.job.protocol.ServiceProtocol;
+import com.usthe.common.model.ServicePodModel;
+import com.usthe.common.entity.job.Metrics;
+import com.usthe.common.entity.job.protocol.HttpProtocol;
+import com.usthe.common.entity.message.CollectRep;
+import org.springframework.beans.factory.InitializingBean;
+
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * 不同数据格式解析抽象类
+ *
+ *
+ */
+
+public interface AbstractParseResponse extends InitializingBean {
+ /**
+ * 通用解析抽象方法
+ *
+ * @param resp
+ * @param aliasFields
+ * @param http
+ * @param builder
+ * @param responseTime
+ */
+ public default void parseResponse(String resp, List aliasFields, HttpProtocol http,
+ CollectRep.MetricsData.Builder builder, Long responseTime) {
+
+ }
+
+ /**
+ * k8s解析方式
+ *
+ * @param metrics
+ * @param resp
+ * @param podMap
+ * @param aliasFields
+ * @param service
+ * @param builder
+ * @param responseTime
+ */
+ public default void parseK8sApi(Metrics metrics, Object resp, Map podMap, List aliasFields, ServiceProtocol service,
+ CollectRep.MetricsData.Builder builder, Long responseTime) {
+
+ }
+
+ /**
+ * 微服务响应体解析方法
+ * @param resp
+ * @param fields
+ * @param aliasFields
+ * @param jsonScript
+ * @param http
+ * @param tempcloums
+ * @param kv
+ */
+ public default void parseResponse(String resp,List fields, List aliasFields ,List jsonScript, HttpProtocol http,
+ Map> tempcloums,String kv){
+
+ }
+
+ /**
+ * 微服务响应体解析方法
+ * @param resp
+ * @param field
+ * @param aliasField
+ * @param jsonScript
+ * @param http
+ * @param tempcloums
+ * @param kv
+ */
+ public default void parseResponse(String resp,String field, String aliasField ,String jsonScript, HttpProtocol http,
+ Map> tempcloums,String kv){
+
+ }
+}
diff --git a/collector/src/main/java/com/usthe/collector/collect/http/HttpCollectImpl.java b/collector/src/main/java/com/usthe/collector/collect/http/HttpCollectImpl.java
index f69ed02fc2e..97615b6916b 100644
--- a/collector/src/main/java/com/usthe/collector/collect/http/HttpCollectImpl.java
+++ b/collector/src/main/java/com/usthe/collector/collect/http/HttpCollectImpl.java
@@ -86,7 +86,7 @@
@Slf4j
public class HttpCollectImpl extends AbstractCollect {
- private HttpCollectImpl() {}
+ public HttpCollectImpl() {}
public static HttpCollectImpl getInstance() {
return Singleton.INSTANCE;
@@ -397,7 +397,7 @@ private void parseResponseByDefault(String resp, List aliasFields, HttpP
* @param httpProtocol http protocol
* @return context
*/
- private HttpContext createHttpContext(HttpProtocol httpProtocol) {
+ public HttpContext createHttpContext(HttpProtocol httpProtocol) {
HttpProtocol.Authorization auth = httpProtocol.getAuthorization();
if (auth != null && DispatchConstants.DIGEST_AUTH.equals(auth.getType())) {
HttpClientContext clientContext = new HttpClientContext();
@@ -422,7 +422,7 @@ private HttpContext createHttpContext(HttpProtocol httpProtocol) {
* @param httpProtocol http参数配置
* @return 请求体
*/
- private HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
+ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
RequestBuilder requestBuilder;
// method
String httpMethod = httpProtocol.getMethod().toUpperCase();
diff --git a/collector/src/main/java/com/usthe/collector/collect/http/MicroServiceActuatorHttpCollectImpl.java b/collector/src/main/java/com/usthe/collector/collect/http/MicroServiceActuatorHttpCollectImpl.java
new file mode 100644
index 00000000000..40a5bd07b37
--- /dev/null
+++ b/collector/src/main/java/com/usthe/collector/collect/http/MicroServiceActuatorHttpCollectImpl.java
@@ -0,0 +1,353 @@
+package com.usthe.collector.collect.http;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonPrimitive;
+import com.usthe.collector.collect.AbstractParseResponse;
+import com.usthe.collector.collect.common.http.CommonHttpClient;
+
+import com.usthe.collector.collect.strategy.CollectStrategyFactory;
+import com.usthe.collector.collect.strategy.ParseStrategyFactory;
+import com.usthe.collector.dispatch.DispatchConstants;
+import com.usthe.collector.util.JsonPathParser;
+import com.usthe.common.entity.job.Configmap;
+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.AesUtil;
+import com.usthe.common.util.CommonConstants;
+import com.usthe.common.util.GsonUtil;
+import com.jayway.jsonpath.TypeRef;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.SSLException;
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.ConnectException;
+import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+import java.util.stream.Collectors;
+
+
+
+/**
+ * msa http https collect
+ *
+ *
+ */
+@Slf4j
+public class MicroServiceActuatorHttpCollectImpl extends HttpCollectImpl {
+ public static final int SIZE = 50;
+
+
+ private MicroServiceActuatorHttpCollectImpl() {
+ super();
+ }
+
+ public static MicroServiceActuatorHttpCollectImpl getInstance() {
+ return Singleton.INSTANCE;
+ }
+
+
+ @Override
+ public void collect(CollectRep.MetricsData.Builder builder,long appId, String app, Metrics metrics) {
+ // 校验参数
+ try {
+ if (metrics == null || metrics.getHttp() == null) {
+ throw new Exception("Http/Https collect must has http params");
+ }
+ } catch (Exception e) {
+ builder.setCode(CollectRep.Code.FAIL);
+ builder.setMsg(e.getMessage());
+ return;
+ }
+ List row = new ArrayList<>();
+ try{
+ HttpProtocol tempHttp = metrics.getHttp();
+ List param = metrics.getChildParam();
+ Configmap configmap = param.stream().filter(c -> DispatchConstants.CHILD_REQUESTS.equals(c.getKey())).findFirst().orElse(null);
+ List