Skip to content

Commit

Permalink
update parser to parse from prometheus txt metrics data (#1421)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
tomsun28 authored Dec 15, 2023
1 parent 405b882 commit 3ff13aa
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ private void parseResponseByPrometheusExporter(String resp, List<String> aliasFi
for (String aliasField : aliasFields) {
if ("value".equals(aliasField)) {
if (metric.getCounter() != null) {
valueRowBuilder.addColumns(metric.getCounter().getValue() + "");
valueRowBuilder.addColumns(String.valueOf(metric.getCounter().getValue()));
} else if (metric.getGauge() != null) {
valueRowBuilder.addColumns(metric.getGauge().getValue() + "");
valueRowBuilder.addColumns(String.valueOf(metric.getGauge().getValue()));
} else if (metric.getUntyped() != null) {
valueRowBuilder.addColumns(metric.getUntyped().getValue() + "");
valueRowBuilder.addColumns(String.valueOf(metric.getUntyped().getValue()));
} else if (metric.getInfo() != null) {
valueRowBuilder.addColumns(metric.getInfo().getValue() + "");
valueRowBuilder.addColumns(String.valueOf(metric.getInfo().getValue()));
}
} else {
valueRowBuilder.addColumns(labelMap.get(aliasField));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.dromara.hertzbeat.collector.collect.common.http.CommonHttpClient;
import org.dromara.hertzbeat.collector.collect.http.promethus.exporter.ExporterParser;
import org.dromara.hertzbeat.collector.collect.http.promethus.exporter.MetricFamily;
import org.dromara.hertzbeat.collector.collect.http.promethus.exporter.MetricType;
import org.dromara.hertzbeat.collector.collect.prometheus.parser.MetricFamily;
import org.dromara.hertzbeat.collector.collect.prometheus.parser.TextParser;
import org.dromara.hertzbeat.collector.dispatch.DispatchConstants;
import org.dromara.hertzbeat.collector.util.CollectUtil;
import org.dromara.hertzbeat.common.constants.CollectorConstants;
Expand All @@ -60,7 +59,6 @@
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -170,15 +168,9 @@ private void validateParams(Metrics metrics) throws Exception {
}
}

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

private List<CollectRep.MetricsData> 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);
Map<String, MetricFamily> metricFamilyMap = TextParser.textToMetricFamilies(resp);
List<CollectRep.MetricsData> metricsDataList = new LinkedList<>();
for (Map.Entry<String, MetricFamily> entry : metricFamilyMap.entrySet()) {
builder.clearMetrics();
Expand All @@ -187,42 +179,28 @@ private List<CollectRep.MetricsData> parseResponseByPrometheusExporter(String re
String metricsName = entry.getKey();
builder.setMetrics(metricsName);
MetricFamily metricFamily = entry.getValue();
if (metricFamily.getMetricType() == MetricType.HISTOGRAM || metricFamily.getMetricType() == MetricType.SUMMARY) {
// todo HISTOGRAM SUMMARY
continue;
}
if (!metricFamily.getMetricList().isEmpty()) {
List<String> metricsFields = new LinkedList<>();
for (int index = 0; index < metricFamily.getMetricList().size(); index++) {
MetricFamily.Metric metric = metricFamily.getMetricList().get(index);
if (index == 0) {
metric.getLabelPair().forEach(label -> {
metric.getLabels().forEach(label -> {
metricsFields.add(label.getName());
builder.addFields(CollectRep.Field.newBuilder().setName(label.getName())
.setType(CommonConstants.TYPE_STRING).setLabel(true).build());
});
builder.addFields(CollectRep.Field.newBuilder().setName("value")
.setType(CommonConstants.TYPE_NUMBER).setLabel(false).build());
}
Map<String, String> labelMap = metric.getLabelPair()
Map<String, String> labelMap = metric.getLabels()
.stream()
.collect(Collectors.toMap(MetricFamily.Label::getName, MetricFamily.Label::getValue));
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String field : metricsFields) {
String fieldValue = labelMap.get(field);
valueRowBuilder.addColumns(fieldValue == null ? CommonConstants.NULL_VALUE : fieldValue);
}
if (metric.getCounter() != null) {
valueRowBuilder.addColumns(String.valueOf(metric.getCounter().getValue()));
} else if (metric.getGauge() != null) {
valueRowBuilder.addColumns(String.valueOf(metric.getGauge().getValue()));
} else if (metric.getUntyped() != null) {
valueRowBuilder.addColumns(String.valueOf(metric.getUntyped().getValue()));
} else if (metric.getInfo() != null) {
valueRowBuilder.addColumns(String.valueOf(metric.getInfo().getValue()));
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
valueRowBuilder.addColumns(String.valueOf(metric.getValue()));
builder.addValues(valueRowBuilder.build());
}
metricsDataList.add(builder.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.dromara.hertzbeat.collector.collect.prometheus.parser;

import lombok.Data;
import lombok.ToString;

import java.util.List;

/**
* metric family
*
*/
@Data
@ToString
public class MetricFamily {

/**
* metric name
*/
private String name;

/**
* metrics
*/
private List<Metric> metricList;

@Data
public static class Metric {

private List<Label> labels;

private double value;

private long timestamp;
}

@Data
public static class Label {

private String name;

private String value;
}
}
Loading

0 comments on commit 3ff13aa

Please sign in to comment.