Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Prometheus exporter metrics parser and IoTDB monitor #505

Merged
merged 23 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
665b5d6
prometheus exporter metrics parser
Ceilzcx Nov 8, 2022
c60f164
prometheus exporter metrics parser
Ceilzcx Nov 9, 2022
2be453d
prometheus exporter metrics parser
Ceilzcx Nov 9, 2022
c86faec
prometheus exporter metrics parser
Ceilzcx Nov 9, 2022
78a7682
optimize ExporterParser.java
Ceilzcx Nov 9, 2022
e07fc6b
optimize ExporterParser.java
Ceilzcx Nov 10, 2022
bdcd31d
optimize ExporterParser.java
Ceilzcx Nov 11, 2022
8d5b814
handle INF parser
Ceilzcx Nov 11, 2022
65f7b8b
Merge branch 'master' into feature#exporter
Ceilzcx Dec 3, 2022
dd0e56a
添加iotdb的exporter方式监听
Ceilzcx Dec 3, 2022
72c0821
[manager] iotdb monitor add cluster_node_status metric
tomsun28 Dec 4, 2022
8d2abc9
[manager] fix actuator mail health check error when startup
tomsun28 Dec 4, 2022
e1f23af
[manager] use prometheus instead of prometheus_exporter
tomsun28 Dec 4, 2022
760c562
[manager] add iotdb monitoring metrics
tomsun28 Dec 4, 2022
49b463c
[collector] add metric type - info, update parser
tomsun28 Dec 4, 2022
b133c4d
[collector] update
tomsun28 Dec 4, 2022
323c585
ExporterParser change to singleton mode
Ceilzcx Dec 5, 2022
b6483e6
add ExporterParser currentMetricFamily
Ceilzcx Dec 5, 2022
4cf9c60
格式化优化代码, 使用map管理ExporterParser防止无限制创建对象, 使用锁机制反正多线程读写冲突
Ceilzcx Dec 19, 2022
d3dd668
修改注解格式
Ceilzcx Dec 19, 2022
3e18775
格式化注解
Ceilzcx Dec 19, 2022
bcbc075
修改注解格式
Ceilzcx Dec 20, 2022
128224f
解决魔法数问题
Ceilzcx Dec 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<groupId>com.usthe.tancloud</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
<scope>provided</scope>
<!-- <scope>provided</scope>-->
</dependency>
<!-- validation -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.gson.JsonParser;
import com.usthe.collector.collect.AbstractCollect;
import com.usthe.collector.collect.common.http.CommonHttpClient;
import com.usthe.collector.collect.http.promethus.exporter.ExporterParser;
import com.usthe.collector.collect.http.promethus.exporter.MetricFamily;
import com.usthe.collector.dispatch.DispatchConstants;
import com.usthe.collector.util.CollectUtil;
import com.usthe.collector.util.CollectorConstants;
Expand Down Expand Up @@ -76,6 +78,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


/**
Expand All @@ -86,7 +90,8 @@
@Slf4j
public class HttpCollectImpl extends AbstractCollect {

public HttpCollectImpl() {}
public HttpCollectImpl() {
}

@Override
public void collect(CollectRep.MetricsData.Builder builder,
Expand Down Expand Up @@ -114,12 +119,14 @@ public void collect(CollectRep.MetricsData.Builder builder,
return;
} else {
// 2xx 3xx 状态码 成功
// todo 这里直接将InputStream转为了String, 对于prometheus exporter大数据来说, 会生成大对象, 可能会严重影响JVM内存空间
// todo 方法一、使用InputStream进行解析, 代码改动大; 方法二、手动触发gc, 可以参考dubbo for long i
String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
// 根据不同的解析方式解析
if (resp == null || "".equals(resp)) {
log.info("http response entity is empty, status: {}.", statusCode);
}
Long responseTime = System.currentTimeMillis() - startTime;
Long responseTime = System.currentTimeMillis() - startTime;
String parseType = metrics.getHttp().getParseType();
try {
if (DispatchConstants.PARSE_DEFAULT.equals(parseType)) {
Expand All @@ -128,9 +135,11 @@ public void collect(CollectRep.MetricsData.Builder builder,
parseResponseByJsonPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder, responseTime);
} else if (DispatchConstants.PARSE_PROM_QL.equalsIgnoreCase(parseType)) {
parseResponseByPromQL(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
} else if (DispatchConstants.PARSE_PROMETHEUS.equals(parseType)) {
parseResponseByPrometheusExporter(resp, metrics.getAliasFields(), builder);
} else if (DispatchConstants.PARSE_XML_PATH.equals(parseType)) {
parseResponseByXmlPath(resp, metrics.getAliasFields(), metrics.getHttp(), builder);
} else if (DispatchConstants.PARSE_WEBSITE.equals(parseType)){
} else if (DispatchConstants.PARSE_WEBSITE.equals(parseType)) {
parseResponseByWebsite(resp, metrics.getAliasFields(), metrics.getHttp(), builder, responseTime);
} else if (DispatchConstants.PARSE_SITE_MAP.equals(parseType)) {
parseResponseBySiteMap(resp, metrics.getAliasFields(), builder);
Expand Down Expand Up @@ -316,7 +325,7 @@ private void parseResponseByJsonPath(String resp, List<String> aliasFields, Http
continue;
}
if (objectValue instanceof Map) {
Map<String, Object> stringMap = (Map<String, Object>)objectValue;
Map<String, Object> stringMap = (Map<String, Object>) objectValue;
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String alias : aliasFields) {
Object value = stringMap.get(alias);
Expand Down Expand Up @@ -361,7 +370,44 @@ private void parseResponseByJsonPath(String resp, List<String> aliasFields, Http
private void parseResponseByPromQL(String resp, List<String> aliasFields, HttpProtocol http,
CollectRep.MetricsData.Builder builder) {
AbstractPrometheusParse prometheusParser = PrometheusParseCreater.getPrometheusParse();
prometheusParser.handle(resp,aliasFields,http,builder);
prometheusParser.handle(resp, aliasFields, http, builder);
}

private static final Map<Long, ExporterParser> EXPORTER_PARSER_TABLE = new ConcurrentHashMap<>();

private void parseResponseByPrometheusExporter(String resp, List<String> aliasFields,
CollectRep.MetricsData.Builder builder) {
if (!EXPORTER_PARSER_TABLE.containsKey(builder.getId())) {
EXPORTER_PARSER_TABLE.put(builder.getId(), new ExporterParser());
}
ExporterParser parser = EXPORTER_PARSER_TABLE.get(builder.getId());
Map<String, MetricFamily> metricFamilyMap = parser.textToMetric(resp);
String metrics = builder.getMetrics();
if (metricFamilyMap.containsKey(metrics)) {
MetricFamily metricFamily = metricFamilyMap.get(metrics);
for (MetricFamily.Metric metric : metricFamily.getMetricList()) {
Map<String, String> labelMap = metric.getLabelPair()
.stream()
.collect(Collectors.toMap(MetricFamily.Label::getName, MetricFamily.Label::getValue));
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String aliasField : aliasFields) {
if ("value".equals(aliasField)) {
if (metric.getCounter() != null) {
valueRowBuilder.addColumns(metric.getCounter().getValue() + "");
} else if (metric.getGauge() != null) {
valueRowBuilder.addColumns(metric.getGauge().getValue() + "");
} else if (metric.getUntyped() != null) {
valueRowBuilder.addColumns(metric.getUntyped().getValue() + "");
} else if (metric.getInfo() != null) {
valueRowBuilder.addColumns(metric.getInfo().getValue() + "");
}
} else {
valueRowBuilder.addColumns(labelMap.get(aliasField));
}
}
builder.addValues(valueRowBuilder.build());
}
}
}

private void parseResponseByDefault(String resp, List<String> aliasFields, HttpProtocol http,
Expand Down Expand Up @@ -436,6 +482,7 @@ public HttpContext createHttpContext(HttpProtocol httpProtocol) {

/**
* 根据http配置参数构造请求头
*
* @param httpProtocol http参数配置
* @return 请求体
*/
Expand All @@ -444,9 +491,9 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
// method
String httpMethod = httpProtocol.getMethod().toUpperCase();
if (HttpMethod.GET.matches(httpMethod)) {
requestBuilder = RequestBuilder.get();
requestBuilder = RequestBuilder.get();
} else if (HttpMethod.POST.matches(httpMethod)) {
requestBuilder = RequestBuilder.post();
requestBuilder = RequestBuilder.post();
} else if (HttpMethod.PUT.matches(httpMethod)) {
requestBuilder = RequestBuilder.put();
} else if (HttpMethod.DELETE.matches(httpMethod)) {
Expand All @@ -471,7 +518,7 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
// The default request header can be overridden if customized
// keep-alive
requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive");
requestBuilder.addHeader(HttpHeaders.USER_AGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36");
requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36");
// headers The custom request header is overwritten here
Map<String, String> headers = httpProtocol.getHeaders();
if (headers != null && !headers.isEmpty()) {
Expand All @@ -488,9 +535,6 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
requestBuilder.addHeader(HttpHeaders.ACCEPT, "application/json");
} else if (DispatchConstants.PARSE_XML_PATH.equals(httpProtocol.getParseType())) {
requestBuilder.addHeader(HttpHeaders.ACCEPT, "text/xml,application/xml");
} 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, "*/*");
}
Expand All @@ -507,13 +551,13 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
&& StringUtils.hasText(authorization.getBasicAuthPassword())) {
String authStr = authorization.getBasicAuthUsername() + ":" + authorization.getBasicAuthPassword();
String encodedAuth = new String(Base64.encodeBase64(authStr.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + " " + encodedAuth);
requestBuilder.addHeader(HttpHeaders.AUTHORIZATION, DispatchConstants.BASIC + " " + encodedAuth);
}
}
}

// 请求内容,会覆盖post协议的params
if(StringUtils.hasLength(httpProtocol.getPayload())){
if (StringUtils.hasLength(httpProtocol.getPayload())) {
requestBuilder.setEntity(new StringEntity(httpProtocol.getPayload(), StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.usthe.collector.collect.http.promethus;

/**
* @author ceilzcx
* @since 7/11/2022
*/
public class ParseException extends RuntimeException {

public ParseException(String msg) {
super(msg);
}
}
Loading