wu-sheng commented on code in PR #9822:
URL: https://github.com/apache/skywalking/pull/9822#discussion_r1003434032


##########
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/prometheus/rule/Rules.java:
##########
@@ -47,29 +53,79 @@ public static List<Rule> loadRules(final String path) 
throws ModuleStartExceptio
     public static List<Rule> loadRules(final String path, List<String> 
enabledRules) throws ModuleStartException {
         File[] rules;
         try {
-            rules = ResourceUtils.getPathFiles(path);
+            rules = ResourceUtils.list(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;
+        Map<String, Boolean> formedEnabledRules = enabledRules
+                .stream()
+                .map(rule -> {
+                    rule = rule.trim();
+                    if (rule.startsWith("/")) {
+                        rule = rule.substring(1);
+                    }
+                    if (rule.endsWith(".yaml")) {
+                        return rule;
+                    } else if (rule.endsWith(".yml")) {
+                        return rule.substring(0, rule.length() - 2) + "aml";
+                    } else {
+                        return rule + ".yaml";
                     }
-                    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> result = Arrays.stream(rules)
+                .flatMap(f -> {
+                    if (f.isDirectory()) {
+                        return 
Arrays.stream(Objects.requireNonNull(f.listFiles()))
+                                .filter(File::isFile)
+                                .map(file -> 
getRulesFromFile(formedEnabledRules, f, file));
+                    } else {
+                        return Stream.of(getRulesFromFile(formedEnabledRules, 
null, f));
+                    }
+                })
+                .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 result;
+    }
+
+    private static Rule getRulesFromFile(Map<String, Boolean> 
formedEnabledRules, File directory, File file) {
+        try (Reader r = new FileReader(file)) {
+            String fileName = file.getName();
+            int dotIndex = fileName.lastIndexOf('.');
+            if (fileName.endsWith(".yml")) {
+                fileName = fileName.substring(0, fileName.length() - 2) + 
"aml";
+            }
+            if (dotIndex == -1 || !"yaml".equals(fileName.substring(dotIndex + 
1))) {
+                return null;
+            }
+            String ruleName = fileName.substring(0, dotIndex);

Review Comment:
   What would happens if illegal files and legal files are mixed together? And 
I think there are two cases
   1. illegal.yaml is mixed with other normal files, then we should fail in 
booting. Right?
   2. Another case is, we have a hidden file, such as `.abc` is a hidden file 
in the folder, but it is not a YAML file.
   
   Such as in a macOS, you could have several hidden files in every folder,  
**.DS_Store**.
   <img width="675" alt="image" 
src="https://user-images.githubusercontent.com/5441976/197560595-8d3ccbef-6377-4dc4-a268-baed08c6304c.png";>
   



-- 
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