public List filterThresholdsByAppAndMetrics()

in hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculator.java [276:332]


    public List<AlertDefine> filterThresholdsByAppAndMetrics(List<AlertDefine> thresholds, String app, String metrics, Map<String, String> labels, String instance, int priority) {
        return thresholds.stream()
                .filter(define -> {
                    if (StringUtils.isBlank(define.getExpr())) {
                        return false;
                    }
                    String expr = define.getExpr();

                    // Extract and check app - required
                    Matcher appMatcher = APP_PATTERN.matcher(expr);
                    if (!appMatcher.find() || !app.equals(appMatcher.group(1))) {
                        return false;
                    }
                    
                    // Extract and check available - required
                    if (priority != 0) {
                        Matcher availableMatcher = AVAILABLE_PATTERN.matcher(expr);
                        if (availableMatcher.find()) {
                            return false;
                        }
                    }

                    // Extract and check metrics - optional
                    Matcher metricsMatcher = METRICS_PATTERN.matcher(expr);
                    if (metricsMatcher.find() && !metrics.equals(metricsMatcher.group(1))) {
                        return false;
                    }

                    // Extract and check instance - optional with multiple values
                    Matcher instanceMatcher = INSTANCE_PATTERN.matcher(expr);
                    Matcher labelMatcher = LABEL_PATTERN.matcher(expr);
                    // If no instance and instance labels specified in expr, accept all instances
                    if (!instanceMatcher.find() && !labelMatcher.find()) {
                        return true;
                    }
                    
                    // Reset matcher to check all instances
                    instanceMatcher.reset();
                    labelMatcher.reset();
                    // If instances specified, current instance must match one of them
                    while (instanceMatcher.find()) {
                        if (Objects.equals(instance, instanceMatcher.group(1))) {
                            return true;
                        }
                    }
                    // If instance labels specified, current instance must match one of them
                    Set<String> labelKvStringSet = kvLabelsToKvStringSet(labels);
                    while (labelMatcher.find()) {
                        String label = labelMatcher.group(1);
                        if (labelKvStringSet.contains(label)) {
                            return true;
                        }
                    }
                    return false;
                })
                .collect(Collectors.toList());
    }