kezhenxu94 commented on code in PR #9822:
URL: https://github.com/apache/skywalking/pull/9822#discussion_r1005228487


##########
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/prometheus/rule/Rules.java:
##########
@@ -40,36 +49,71 @@
 public class Rules {
     private static final Logger LOG = LoggerFactory.getLogger(Rule.class);
 
-    public static List<Rule> loadRules(final String path) throws 
ModuleStartException {
+    public static List<Rule> loadRules(final String path) throws IOException {
         return loadRules(path, Collections.emptyList());
     }
 
-    public static List<Rule> loadRules(final String path, List<String> 
enabledRules) throws ModuleStartException {
-        File[] rules;
-        try {
-            rules = ResourceUtils.getPathFiles(path);
-        } catch (FileNotFoundException e) {
-            throw new ModuleStartException("Load fetcher rules failed", e);
-        }
-        return Arrays.stream(rules)
-            .filter(File::isFile)
-            .map(f -> {
-                try (Reader r = new FileReader(f)) {
-                    String fileName = f.getName();
-                    int dotIndex = fileName.lastIndexOf('.');
-                    fileName = (dotIndex == -1) ? fileName : 
fileName.substring(0, dotIndex);
-                    if (!enabledRules.contains(fileName)) {
-                        return null;
+    public static List<Rule> loadRules(final String path, List<String> 
enabledRules) throws IOException {
+
+        final Path root = ResourceUtils.getPath(path);
+
+        Map<String, Boolean> formedEnabledRules = enabledRules
+                .stream()
+                .map(rule -> {
+                    rule = rule.trim();
+                    if (rule.startsWith("/")) {
+                        rule = rule.substring(1);
+                    }
+                    if (!rule.endsWith(".yaml") && !rule.endsWith(".yml") && 
!rule.endsWith("*")) {

Review Comment:
   We can use this to ensure that all listed files are `.yaml` or `.yml`
   
   ```suggestion
                       if (!rule.endsWith(".yaml") && !rule.endsWith(".yml")) {
   ```
   
   So (2)



##########
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/prometheus/rule/Rules.java:
##########
@@ -40,36 +49,71 @@
 public class Rules {
     private static final Logger LOG = LoggerFactory.getLogger(Rule.class);
 
-    public static List<Rule> loadRules(final String path) throws 
ModuleStartException {
+    public static List<Rule> loadRules(final String path) throws IOException {
         return loadRules(path, Collections.emptyList());
     }
 
-    public static List<Rule> loadRules(final String path, List<String> 
enabledRules) throws ModuleStartException {
-        File[] rules;
-        try {
-            rules = ResourceUtils.getPathFiles(path);
-        } catch (FileNotFoundException e) {
-            throw new ModuleStartException("Load fetcher rules failed", e);
-        }
-        return Arrays.stream(rules)
-            .filter(File::isFile)
-            .map(f -> {
-                try (Reader r = new FileReader(f)) {
-                    String fileName = f.getName();
-                    int dotIndex = fileName.lastIndexOf('.');
-                    fileName = (dotIndex == -1) ? fileName : 
fileName.substring(0, dotIndex);
-                    if (!enabledRules.contains(fileName)) {
-                        return null;
+    public static List<Rule> loadRules(final String path, List<String> 
enabledRules) throws IOException {
+
+        final Path root = ResourceUtils.getPath(path);
+
+        Map<String, Boolean> formedEnabledRules = enabledRules
+                .stream()
+                .map(rule -> {
+                    rule = rule.trim();
+                    if (rule.startsWith("/")) {
+                        rule = rule.substring(1);
+                    }
+                    if (!rule.endsWith(".yaml") && !rule.endsWith(".yml") && 
!rule.endsWith("*")) {
+                        return rule + "{.yaml,.yml}";
                     }
-                    Rule rule = new Yaml().loadAs(r, Rule.class);
-                    rule.setName(fileName);
                     return rule;
-                } catch (IOException e) {
-                    LOG.debug("Reading file {} failed", f, e);
-                }
+                })
+                .collect(Collectors.toMap(rule -> rule, $ -> false));
+        List<Rule> rules;
+        try (Stream<Path> stream = Files.walk(root)) {
+            rules = stream
+                    .filter(it -> formedEnabledRules.keySet().stream()
+                                    .anyMatch(rule -> {
+                                        boolean matches = 
FileSystems.getDefault().getPathMatcher("glob:" + rule)
+                                                .matches(root.relativize(it));
+                                        if (matches) {
+                                            formedEnabledRules.put(rule, true);
+                                        }
+                                        return matches;
+                                    }))
+                    .map(path1 -> getRulesFromFile(root.relativize(path1), 
path1))
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList()) ;
+        }
+
+        if (formedEnabledRules.containsValue(false)) {
+            throw new UnexpectedException("Some configuration files of enabled 
rules are not found, enabled rules: " + formedEnabledRules.keySet());
+        }
+        return rules;
+    }
+
+    private static Rule getRulesFromFile(Path relative, Path path) {
+        File file = path.toFile();
+        if (!file.isFile()) {
+            return null;
+        }
+        try (Reader r = new FileReader(file)) {
+            String fileName = file.getName();
+            int dotIndex = fileName.lastIndexOf('.');
+            if (fileName.startsWith(".") || dotIndex == -1 || 
(!fileName.endsWith(".yml") && !fileName.endsWith(".yaml"))) {
                 return null;
-            })
-            .filter(Objects::nonNull)
-            .collect(Collectors.toList());
+            }

Review Comment:
   (2) ... we don't need to check these



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to