This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 2edc91d287f Add format check for Prometheus Reporter Check (#11666)
2edc91d287f is described below

commit 2edc91d287f72f71596b1b5f2964553527a4054c
Author: Potato <[email protected]>
AuthorDate: Wed Dec 6 14:15:42 2023 +0800

    Add format check for Prometheus Reporter Check (#11666)
---
 .../java/org/apache/iotdb/itbase/env/BaseEnv.java  |  1 +
 .../apache/iotdb/db/it/metric/IoTDBMetricIT.java   | 71 ++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git 
a/integration-test/src/main/java/org/apache/iotdb/itbase/env/BaseEnv.java 
b/integration-test/src/main/java/org/apache/iotdb/itbase/env/BaseEnv.java
index 7e3d70e1328..ebdeb6a1875 100644
--- a/integration-test/src/main/java/org/apache/iotdb/itbase/env/BaseEnv.java
+++ b/integration-test/src/main/java/org/apache/iotdb/itbase/env/BaseEnv.java
@@ -83,6 +83,7 @@ public interface BaseEnv {
         String str;
         while ((str = bufr.readLine()) != null) {
           sb.append(str);
+          sb.append('\n');
         }
         bufr.close();
       } else {
diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/metric/IoTDBMetricIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/metric/IoTDBMetricIT.java
index 1f50344a593..76e6ddce817 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/metric/IoTDBMetricIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/metric/IoTDBMetricIT.java
@@ -30,13 +30,83 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.regex.Pattern;
 
 @RunWith(IoTDBTestRunner.class)
 @Category({LocalStandaloneIT.class, ClusterIT.class})
 public class IoTDBMetricIT {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(IoTDBMetricIT.class);
+
+  private static final String METRIC_NAME_REGEX = "[a-zA-Z_:][a-zA-Z0-9_:]*";
+  private static final String LABEL_NAME_REGEX = "[a-zA-Z_][a-zA-Z0-9_]*";
+  private static final String LABEL_VALUE_REGEX = "[^\",]+";
+  private static final String METRIC_LINE_REGEX =
+      METRIC_NAME_REGEX
+          + "(\\{"
+          + LABEL_NAME_REGEX
+          + "=\""
+          + LABEL_VALUE_REGEX
+          + "\",("
+          + LABEL_NAME_REGEX
+          + "=\""
+          + LABEL_VALUE_REGEX
+          + "\",)*})? [+-]?[0-9]*\\.?[0-9]+([eE][+-]?[0-9]+)?";
+  private static final String HELP_PREFIX = "# HELP ";
+  private static final String HELP_REGEX = HELP_PREFIX + METRIC_NAME_REGEX;
+  private static final String TYPE_PREFIX = "# TYPE ";
+  private static final String TYPE_REGEX = TYPE_PREFIX + METRIC_NAME_REGEX + " 
.+";
+
+  private static final String VALID_LOG_STRING =
+      "This line {} is invalid in prometheus line protocol";
+
+  public static boolean isValidPrometheusTextFormat(String metrics) {
+    String[] lines = metrics.split("\\n");
+    boolean valid = true;
+
+    for (String line : lines) {
+      if (!line.isEmpty()) {
+        if (line.startsWith(HELP_PREFIX)) {
+          if (!isValidHelpLine(line)) {
+            LOGGER.error(VALID_LOG_STRING, line);
+            valid = false;
+            break;
+          }
+        } else if (line.startsWith(TYPE_PREFIX)) {
+          if (!isValidTypeLine(line)) {
+            LOGGER.error(VALID_LOG_STRING, line);
+            valid = false;
+            break;
+          }
+        } else {
+          if (!isValidMetricLine(line)) {
+            LOGGER.error(VALID_LOG_STRING, line);
+            valid = false;
+            break;
+          }
+        }
+      }
+    }
+    return valid;
+  }
+
+  private static boolean isValidMetricLine(String line) {
+    return Pattern.matches(METRIC_LINE_REGEX, line.trim());
+  }
+
+  private static boolean isValidHelpLine(String line) {
+    return Pattern.matches(HELP_REGEX, line.trim());
+  }
+
+  private static boolean isValidTypeLine(String line) {
+    return Pattern.matches(TYPE_REGEX, line.trim());
+  }
+
   @BeforeClass
   public static void setUp() throws Exception {
     // Start ConfigNode with Prometheus reporter up
@@ -63,6 +133,7 @@ public class IoTDBMetricIT {
     for (String metricContent : metricContents) {
       Assert.assertNotNull(metricContent);
       Assert.assertNotEquals(0, metricContent.length());
+      Assert.assertTrue(isValidPrometheusTextFormat(metricContent));
     }
   }
 }

Reply via email to