You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2022/10/21 08:18:58 UTC

[GitHub] [skywalking] kezhenxu94 commented on a diff in pull request #9822: Change the way of loading MAL rules

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


##########
oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/prometheus/rule/Rules.java:
##########
@@ -47,29 +49,70 @@ 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.getAllPathFiles(path);
         } catch (FileNotFoundException e) {
             throw new ModuleStartException("Load fetcher rules failed", e);
         }
+        final List<String> formedEnabledRules =
+                enabledRules
+                        .stream()
+                        .map(rule -> rule.endsWith(".yaml") ? rule : rule + ".yaml")
+                        .map(rule -> rule.startsWith("/") ? rule.substring(1) : rule)
+                        .collect(Collectors.toList());
+
         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)) {
+                .flatMap(f -> {
+                    if (f.isDirectory()) {
+                        return Arrays.stream(Objects.requireNonNull(f.listFiles()))
+                                .filter(File::isFile)
+                                .map(file -> {
+                                    try (Reader r = new FileReader(file)) {
+                                        String fileName = file.getName();
+                                        int dotIndex = fileName.lastIndexOf('.');
+
+                                        if (dotIndex == -1 || !"yaml".equals(fileName.substring(dotIndex + 1))) {
+                                            return null;
+                                        }
+                                        String ruleName = fileName.substring(0, dotIndex);
+                                        fileName = f.getName() + '/' + fileName;
+                                        if (!formedEnabledRules.contains(fileName) && !formedEnabledRules.contains(f.getName() + "/*.yaml")) {
+                                            return null;
+                                        }
+                                        Rule rule = new Yaml().loadAs(r, Rule.class);
+                                        if (rule == null) {
+                                            return null;
+                                        }
+                                        rule.setName(ruleName);
+                                        return rule;
+                                    } catch (IOException e) {
+                                        LOG.debug("Reading file {} failed", f, e);
+                                    }
+                                    return null;
+                                });
+                    } else {
+                        try (Reader r = new FileReader(f)) {
+                            String fileName = f.getName();
+                            int dotIndex = fileName.lastIndexOf('.');
+                            if (dotIndex == -1 || !"yaml".equals(fileName.substring(dotIndex + 1))) {
+                                return null;
+                            }
+                            String ruleName = fileName.substring(0, dotIndex);
+                            if (!formedEnabledRules.contains(fileName)) {
+                                return null;
+                            }
+                            Rule rule = new Yaml().loadAs(r, Rule.class);
+                            if (rule == null) {
+                                return null;
+                            }
+                            rule.setName(ruleName);
+                            return Stream.of(rule);
+                        } catch (IOException e) {
+                            LOG.debug("Reading file {} failed", f, e);
+                        }

Review Comment:
   Can you extract this to a method and share it with the lines above in the `if` statement?



-- 
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: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org