2727import lombok .extern .slf4j .Slf4j ;
2828import org .apache .hertzbeat .collector .collect .http .promethus .ParseException ;
2929import org .apache .hertzbeat .common .util .StrBuffer ;
30- import org .springframework .util .StringUtils ;
3130
3231/**
3332 * Resolves the data passed by prometheus's exporter interface http:xxx/metrics
@@ -38,7 +37,7 @@ public class ExporterParser {
3837 private static final String HELP = "HELP" ;
3938 private static final String TYPE = "TYPE" ;
4039 private static final String EOF = "EOF" ;
41-
40+ private static final String METRIC_NAME_LABEL = ".name" ;
4241 private static final String QUANTILE_LABEL = "quantile" ;
4342 private static final String BUCKET_LABEL = "le" ;
4443 private static final String NAME_LABEL = "__name__" ;
@@ -52,13 +51,11 @@ public class ExporterParser {
5251 private static final char ENTER = '\n' ;
5352 private static final char SPACE = ' ' ;
5453 private static final char COMMA = ',' ;
55-
54+ private final Lock lock = new ReentrantLock ();
5655 private MetricFamily currentMetricFamily ;
5756 private String currentQuantile ;
5857 private String currentBucket ;
5958
60- private final Lock lock = new ReentrantLock ();
61-
6259 public Map <String , MetricFamily > textToMetric (String resp ) {
6360 // key: metric name, value: metric family
6461 Map <String , MetricFamily > metricMap = new ConcurrentHashMap <>(10 );
@@ -86,7 +83,8 @@ private void parseLine(Map<String, MetricFamily> metricMap, StrBuffer buffer) {
8683 this .currentMetricFamily = null ;
8784 this .parseComment (metricMap , buffer );
8885 }
89- case ENTER -> {}
86+ case ENTER -> {
87+ }
9088 default -> {
9189 this .currentBucket = null ;
9290 this .currentQuantile = null ;
@@ -114,7 +112,8 @@ private void parseComment(Map<String, MetricFamily> metricMap, StrBuffer buffer)
114112 switch (token ) {
115113 case HELP -> this .parseHelp (buffer );
116114 case TYPE -> this .parseType (buffer );
117- default -> {}
115+ default -> {
116+ }
118117 }
119118 }
120119
@@ -135,28 +134,27 @@ private void parseType(StrBuffer line) {
135134
136135 private void parseMetric (StrBuffer buffer ) {
137136 String metricName = this .readTokenAsMetricName (buffer );
137+ MetricFamily .Label label = new MetricFamily .Label ();
138+ label .setName (METRIC_NAME_LABEL );
139+ label .setValue (metricName );
140+
138141 if (metricName .isEmpty ()) {
139142 log .error ("error parse metric, metric name is null, line: {}" , buffer .toStr ());
140143 return ;
141144 }
145+
142146 List <MetricFamily .Metric > metricList = this .currentMetricFamily .getMetricList ();
143147 if (metricList == null ) {
144148 metricList = new ArrayList <>();
145149 this .currentMetricFamily .setMetricList (metricList );
146150 }
147- // TODO: This part may have issues. The current logic creates only one metric for both HISTOGRAM and SUMMARY
148- // compared to the source code, there is a slight modification: the source code stores parsing results in a property
149- // here, the results are passed through parameters.
150- MetricFamily .Metric metric ;
151- if (!metricList .isEmpty ()
152- && (this .currentMetricFamily .getMetricType ().equals (MetricType .HISTOGRAM )
153- || this .currentMetricFamily .getMetricType ().equals (MetricType .SUMMARY ))) {
154- metric = metricList .get (0 );
155- } else {
156- metric = new MetricFamily .Metric ();
157- metricList .add (metric );
158- }
159151
152+ // TODO For the time being, the data is displayed in the form of labels. If there is a better chart display method in the future, we will optimize it.
153+ MetricFamily .Metric metric = new MetricFamily .Metric ();
154+ metricList .add (metric );
155+
156+ metric .setLabelPair (new ArrayList <>());
157+ metric .getLabelPair ().add (label );
160158 this .readLabels (metric , buffer );
161159 }
162160
@@ -165,7 +163,6 @@ private void readLabels(MetricFamily.Metric metric, StrBuffer buffer) {
165163 if (buffer .isEmpty ()) {
166164 return ;
167165 }
168- metric .setLabelPair (new ArrayList <>());
169166 if (buffer .charAt (0 ) == LEFT_CURLY_BRACKET ) {
170167 buffer .read ();
171168 this .startReadLabelName (metric , buffer );
@@ -218,9 +215,9 @@ private void startReadLabelValue(MetricFamily.Metric metric, MetricFamily.Label
218215 this .currentQuantile = labelValue ;
219216 } else if (this .currentMetricFamily .getMetricType ().equals (MetricType .HISTOGRAM ) && label .getName ().equals (BUCKET_LABEL )) {
220217 this .currentBucket = labelValue ;
221- } else {
222- metric .getLabelPair ().add (label );
223218 }
219+ metric .getLabelPair ().add (label );
220+
224221 if (buffer .isEmpty ()) {
225222 return ;
226223 }
@@ -257,47 +254,16 @@ private void readLabelValue(MetricFamily.Metric metric, MetricFamily.Label label
257254 metric .setUntyped (untyped );
258255 }
259256 case SUMMARY -> {
260- MetricFamily .Summary summary = metric .getSummary ();
261- if (summary == null ) {
262- summary = new MetricFamily .Summary ();
263- metric .setSummary (summary );
264- }
265- // Process data for xxx_sum
266- if (label != null && this .isSum (label .getName ())) {
267- summary .setSum (buffer .toDouble ());
268- }
269- // Process data for xxx_count
270- else if (label != null && this .isCount (label .getName ())) {
271- summary .setCount (buffer .toLong ());
272- }
273- // Handle format for "xxx{quantile=\"0\"} 0"
274- else if (StringUtils .hasText (this .currentQuantile )) {
275- List <MetricFamily .Quantile > quantileList = summary .getQuantileList ();
276- MetricFamily .Quantile quantile = new MetricFamily .Quantile ();
277- quantile .setXLabel (StrBuffer .parseDouble (this .currentQuantile ));
278- quantile .setValue (buffer .toDouble ());
279- quantileList .add (quantile );
280- }
257+ // For the time being, the data is displayed in the form of labels. If there is a better chart display method in the future, we will optimize it.
258+ MetricFamily .Summary summary = new MetricFamily .Summary ();
259+ summary .setValue (buffer .toDouble ());
260+ metric .setSummary (summary );
281261 }
282262 case HISTOGRAM -> {
283- MetricFamily .Histogram histogram = metric .getHistogram ();
284- if (histogram == null ) {
285- histogram = new MetricFamily .Histogram ();
286- metric .setHistogram (histogram );
287- }
288- if (label != null && this .isSum (label .getName ())) {
289- histogram .setSum (buffer .toDouble ());
290- } else if (label != null && this .isCount (label .getName ())) {
291- histogram .setCount (buffer .toLong ());
292- }
293- // Process the format "xxx{quantile=\"0\"} 0"
294- else if (StringUtils .hasText (this .currentBucket )) {
295- List <MetricFamily .Bucket > bucketList = histogram .getBucketList ();
296- MetricFamily .Bucket bucket = new MetricFamily .Bucket ();
297- bucket .setUpperBound (StrBuffer .parseDouble (this .currentBucket ));
298- bucket .setCumulativeCount (buffer .toLong ());
299- bucketList .add (bucket );
300- }
263+ // For the time being, the data is displayed in the form of labels. If there is a better chart display method in the future, we will optimize it.
264+ MetricFamily .Histogram histogram = new MetricFamily .Histogram ();
265+ histogram .setValue (buffer .toDouble ());
266+ metric .setHistogram (histogram );
301267 }
302268 default -> throw new ParseException ("no such type in metricFamily" );
303269 }
@@ -390,7 +356,9 @@ private String readTokenAsLabelValue(StrBuffer buffer) {
390356 escaped = false ;
391357 } else {
392358 switch (c ) {
393- case QUOTES -> { return builder .toString (); }
359+ case QUOTES -> {
360+ return builder .toString ();
361+ }
394362 case ENTER -> throw new ParseException ("parse label value error, next line" );
395363 case '\\' -> escaped = true ;
396364 default -> builder .append (c );
0 commit comments