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

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

commit f034c693957387d01193f5c931bd6f9421be7f98
Author: OneSizeFitQuorum <[email protected]>
AuthorDate: Wed Dec 6 11:47:56 2023 +0800

    add format check
    
    Signed-off-by: OneSizeFitQuorum <[email protected]>
---
 .../java/org/apache/iotdb/itbase/env/BaseEnv.java  |  1 +
 .../apache/iotdb/db/it/metric/IoTDBMetricIT.java   | 64 ++++++++++++++++++++++
 2 files changed, 65 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..a8a1d6c1fc5 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
@@ -19,11 +19,13 @@
 
 package org.apache.iotdb.db.it.metric;
 
+import java.util.regex.Pattern;
 import org.apache.iotdb.it.env.EnvFactory;
 import org.apache.iotdb.it.framework.IoTDBTestRunner;
 import org.apache.iotdb.itbase.category.ClusterIT;
 import org.apache.iotdb.itbase.category.LocalStandaloneIT;
 
+import org.apache.iotdb.metrics.reporter.prometheus.PrometheusReporter;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
@@ -33,10 +35,71 @@ import org.junit.runner.RunWith;
 
 import java.util.Collections;
 import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @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 +126,7 @@ public class IoTDBMetricIT {
     for (String metricContent : metricContents) {
       Assert.assertNotNull(metricContent);
       Assert.assertNotEquals(0, metricContent.length());
+      Assert.assertTrue(isValidPrometheusTextFormat(metricContent));
     }
   }
 }

Reply via email to