diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/HttpCollectImpl.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/HttpCollectImpl.java index d3864a94a3e..03d0c48f988 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/HttpCollectImpl.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/HttpCollectImpl.java @@ -389,13 +389,13 @@ private void parseResponseByPrometheusExporter(String resp, List 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)); diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java index b5d71cd0d12..a933ee9f435 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java @@ -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; @@ -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; @@ -170,15 +168,9 @@ private void validateParams(Metrics metrics) throws Exception { } } - private static final Map EXPORTER_PARSER_TABLE = new ConcurrentHashMap<>(); - private List parseResponseByPrometheusExporter(String resp, List 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 metricFamilyMap = parser.textToMetric(resp); + Map metricFamilyMap = TextParser.textToMetricFamilies(resp); List metricsDataList = new LinkedList<>(); for (Map.Entry entry : metricFamilyMap.entrySet()) { builder.clearMetrics(); @@ -187,16 +179,12 @@ private List 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 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()); @@ -204,7 +192,7 @@ private List parseResponseByPrometheusExporter(String re builder.addFields(CollectRep.Field.newBuilder().setName("value") .setType(CommonConstants.TYPE_NUMBER).setLabel(false).build()); } - Map labelMap = metric.getLabelPair() + Map labelMap = metric.getLabels() .stream() .collect(Collectors.toMap(MetricFamily.Label::getName, MetricFamily.Label::getValue)); CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); @@ -212,17 +200,7 @@ private List parseResponseByPrometheusExporter(String re 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()); diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/parser/MetricFamily.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/parser/MetricFamily.java new file mode 100644 index 00000000000..82382a70b2c --- /dev/null +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/prometheus/parser/MetricFamily.java @@ -0,0 +1,43 @@ +package org.dromara.hertzbeat.collector.collect.prometheus.parser; + +import lombok.Data; +import lombok.ToString; + +import java.util.List; + +/** + * metric family + * @author ceilzcx + */ +@Data +@ToString +public class MetricFamily { + + /** + * metric name + */ + private String name; + + /** + * metrics + */ + private List metricList; + + @Data + public static class Metric { + + private List