|
38 | 38 | public class OnlineParser {
|
39 | 39 |
|
40 | 40 | private static final Map<Integer, Integer> escapeMap = new HashMap<>(8);
|
41 |
| - |
| 41 | + |
| 42 | + private static final char RIGHT_BRACKET = '}'; |
| 43 | + private static final char LEFT_BRACKET = '{'; |
| 44 | + |
42 | 45 | static {
|
43 | 46 | escapeMap.put((int) 'n', (int) '\n');
|
44 | 47 | escapeMap.put((int) 'b', (int) '\b');
|
@@ -94,14 +97,14 @@ private static class CharChecker {
|
94 | 97 | }
|
95 | 98 |
|
96 | 99 | private CharChecker maybeLeftBracket() {
|
97 |
| - if (i == '{') { |
| 100 | + if (i == LEFT_BRACKET) { |
98 | 101 | satisfied = true;
|
99 | 102 | }
|
100 | 103 | return this;
|
101 | 104 | }
|
102 | 105 |
|
103 | 106 | private CharChecker maybeRightBracket() {
|
104 |
| - if (i == '}') { |
| 107 | + if (i == RIGHT_BRACKET) { |
105 | 108 | satisfied = true;
|
106 | 109 | }
|
107 | 110 | return this;
|
@@ -264,35 +267,52 @@ private static Double toDouble(String string) throws FormatException {
|
264 | 267 | }
|
265 | 268 | }
|
266 | 269 |
|
267 |
| - private static CharChecker parseLabels(InputStream inputStream, StringBuilder stringBuilder, List<MetricFamily.Label> labelList) throws IOException, FormatException { |
| 270 | + |
| 271 | + /** |
| 272 | + * parse single label like label_name="label_value" |
| 273 | + */ |
| 274 | + private static CharChecker parseLabel(InputStream inputStream, StringBuilder stringBuilder, List<MetricFamily.Label> labelList) throws IOException, FormatException { |
268 | 275 | int i;
|
269 |
| - while (true) { |
270 |
| - MetricFamily.Label label = new MetricFamily.Label(); |
271 |
| - i = skipSpaces(inputStream).getInt(); |
272 |
| - stringBuilder.append((char) i); |
273 |
| - i = parseLabelName(inputStream, stringBuilder).maybeSpace().maybeEqualsSign().noElse(); |
274 |
| - label.setName(stringBuilder.toString()); |
275 |
| - stringBuilder.delete(0, stringBuilder.length()); |
276 |
| - if (i == ' ') { |
277 |
| - skipSpaces(inputStream).maybeEqualsSign().noElse(); |
278 |
| - } |
| 276 | + MetricFamily.Label label = new MetricFamily.Label(); |
| 277 | + i = skipSpaces(inputStream).getInt(); |
| 278 | + if (i == RIGHT_BRACKET) { |
| 279 | + return new CharChecker(i); |
| 280 | + } |
| 281 | + stringBuilder.append((char) i); |
| 282 | + i = parseLabelName(inputStream, stringBuilder).maybeSpace().maybeEqualsSign().noElse(); |
| 283 | + label.setName(stringBuilder.toString()); |
| 284 | + stringBuilder.delete(0, stringBuilder.length()); |
| 285 | + if (i == ' ') { |
| 286 | + skipSpaces(inputStream).maybeEqualsSign().noElse(); |
| 287 | + } |
| 288 | + |
| 289 | + skipSpaces(inputStream).maybeQuotationMark().noElse(); |
| 290 | + parseLabelValue(inputStream, stringBuilder).maybeQuotationMark().noElse(); |
| 291 | + String labelValue = stringBuilder.toString(); |
| 292 | + if (!labelValue.equals(new String(labelValue.getBytes(StandardCharsets.UTF_8)))) { |
| 293 | + throw new FormatException(); |
| 294 | + } |
| 295 | + label.setValue(labelValue); |
| 296 | + stringBuilder.delete(0, stringBuilder.length()); |
| 297 | + labelList.add(label); |
| 298 | + return new CharChecker(i); |
| 299 | + } |
279 | 300 |
|
280 |
| - skipSpaces(inputStream).maybeQuotationMark().noElse(); |
281 |
| - parseLabelValue(inputStream, stringBuilder).maybeQuotationMark().noElse(); |
282 |
| - String labelValue = stringBuilder.toString(); |
283 |
| - if (!labelValue.equals(new String(labelValue.getBytes(StandardCharsets.UTF_8)))) { |
284 |
| - throw new FormatException(); |
285 |
| - } |
286 |
| - label.setValue(labelValue); |
287 |
| - stringBuilder.delete(0, stringBuilder.length()); |
288 | 301 |
|
| 302 | + private static void parseLabels(InputStream inputStream, StringBuilder stringBuilder, List<MetricFamily.Label> labelList) throws IOException, FormatException { |
| 303 | + int i; |
| 304 | + while (true) { |
| 305 | + // deal with labels like {label1="aaa",label2=bbb",} |
| 306 | + CharChecker charChecker = parseLabel(inputStream, stringBuilder, labelList); |
| 307 | + if (charChecker.i == RIGHT_BRACKET) { |
| 308 | + return; |
| 309 | + } |
| 310 | + //deal with labels like {label1="aaa",label2=bbb"} |
289 | 311 | i = skipSpaces(inputStream).maybeSpace().maybeComma().maybeRightBracket().noElse();
|
290 |
| - labelList.add(label); |
291 |
| - if (i == '}') { |
292 |
| - break; |
| 312 | + if (i == RIGHT_BRACKET) { |
| 313 | + return; |
293 | 314 | }
|
294 | 315 | }
|
295 |
| - return new CharChecker(i); |
296 | 316 | }
|
297 | 317 |
|
298 | 318 | private static CharChecker parseMetric(InputStream inputStream, Map<String, MetricFamily> metricFamilyMap, StringBuilder stringBuilder) throws IOException, FormatException {
|
|
0 commit comments