tju-yxq opened a new issue, #1404:
URL: https://github.com/apache/rocketmq-dashboard/issues/1404

   ## Bug Report
   
   ### Before Creating the Bug Report
   
   - [x] I found a bug, not just asking a question, which should be created in 
[GitHub Discussions](https://github.com/apache/rocketmq/discussions).
   - [x] I have searched the [GitHub 
Issues](https://github.com/apache/rocketmq/issues) and [GitHub 
Discussions](https://github.com/apache/rocketmq/discussions) of this repository 
and believe that this is not a duplicate.
   - [x] I have confirmed that this bug belongs to the current repository, not 
other repositories of RocketMQ.
   
   ### Runtime platform environment
   
   OS: Ubuntu 20.04 / Any OS running RocketMQ Studio
   
   ### RocketMQ version
   
   branch: rocketmq-studio
   version: 5.3.2+
   Git commit id: f727341
   
   ### JDK Version
   
   OpenJDK 21
   
   ### Describe the Bug
   
   `AlertService.exportPrometheusRulesYaml()` converts each alert rule name 
into a Prometheus alert name by stripping all non-alphanumeric characters 
(`[^A-Za-z0-9_]`). When two rules have names that normalize to the same string 
(e.g., `"Broker Down!"` and `"Broker_Down"` both become `"BrokerDown"`), the 
exported YAML contains duplicate alert names within the same rule group. 
Prometheus **rejects** rule files with duplicate alert names, causing the 
entire rules file to fail to load.
   
   ```java
   private String alertName(AlertRuleVO rule) {
       String alertName = hasText(rule.getName()) ? 
rule.getName().replaceAll("[^A-Za-z0-9_]", "") : "";
       return alertName.isEmpty() ? "RocketMQAlert" : alertName;
   }
   ```
   
   Additionally, when a rule name contains **only** special characters (e.g., 
`"!!!"`), the sanitized name is empty and falls back to `"RocketMQAlert"`. If 
multiple such rules exist, they all become `"RocketMQAlert"`, again producing 
duplicates.
   
   ### Steps to Reproduce
   
   1. In RocketMQ Studio, create two alert rules:
      - Rule A: name = `"Consumer Lag!"`, metric = 
`rocketmq_consumer_lag_messages`, threshold = 1000
      - Rule B: name = `"Consumer Lag?"`, metric = 
`rocketmq_consumer_lag_messages`, threshold = 5000
   2. Export the Prometheus rules YAML (`GET 
/api/alert-rules/export/prometheus`).
   3. Load the exported YAML into Prometheus.
   4. Prometheus fails to load the rules with an error like: `duplicate alert 
name: ConsumerLag`
   
   ### What Did You Expect to See?
   
   The exported YAML should have unique alert names. When a collision is 
detected, the exporter should append a numeric suffix (e.g., `ConsumerLag` and 
`ConsumerLag_2`) to disambiguate.
   
   ### What Did You See Instead?
   
   Two rules with the same `alert: ConsumerLag` in the same group, which 
Prometheus rejects.
   
   ### Additional Context
   
   **Affected file**: 
`server/src/main/java/org/apache/rocketmq/studio/ops/alert/AlertService.java`, 
method `alertName()` and `exportPrometheusRulesYaml()`.
   
   **Fix approach**:
   
   In `exportPrometheusRulesYaml()`, track alert names that have already been 
emitted within each group. When a collision is detected, append `_2`, `_3`, 
etc. until the name is unique:
   
   ```java
   Set<String> usedAlertNames = new HashSet<>();
   for (PrometheusAlertRule rule : group.getValue()) {
       String baseName = alertName(rule);
       String uniqueName = baseName;
       int suffix = 2;
       while (!usedAlertNames.add(uniqueName)) {
           uniqueName = baseName + "_" + suffix++;
       }
       yaml.append("      - alert: ").append(uniqueName).append('\n');
       // ... rest of the rule emission
   }
   ```
   
   This adds approximately 10 lines to the export loop. No existing logic is 
deleted.
   


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