Skip to content

[bugfix] support no-whitespace alert expressions in instance filtering #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 21, 2025

What's changed?

apache#3220

This PR fixes a bug in RealTimeAlertCalculator where instance filtering failed when alert expressions contained no whitespace (e.g., equals(__app__,"redis") && equals(__instance__,"501045327364864")).

The backend regex has been updated to support such compact expressions. A unit test is also added to ensure proper filtering across different formatting styles.

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

  • I have added the necessary e2e tests and all cases have passed.

Summary by CodeRabbit

  • New Features
    • Added the ability to instantiate alert calculation without automatically starting calculation threads.
  • Bug Fixes
    • Improved instance pattern matching to handle optional whitespace in alert definitions.
  • Tests
    • Introduced new tests to verify correct filtering of alert definitions based on instance ID expressions, covering cases with and without spaces.

Copy link

coderabbitai bot commented Apr 21, 2025

Walkthrough

A new constructor overload has been added to the RealTimeAlertCalculator class, allowing the caller to control whether alert calculation threads start automatically. The visibility of the startCalculate and filterThresholdsByAppAndMetrics methods has been changed from private to public. A minor regex update was made to the instance matching pattern. Additionally, a new test class, RealTimeAlertCalculatorTest, has been introduced to verify the correct filtering of alert definitions based on instance expressions, covering scenarios with and without spaces in the expressions.

Changes

File(s) Change Summary
hertzbeat-alerter/src/main/java/.../RealTimeAlertCalculator.java Added constructor overload with a start flag; original constructor now delegates to new one and is annotated with @Autowired. Made startCalculate() and filterThresholdsByAppAndMetrics() public. Updated regex pattern.
hertzbeat-alerter/src/test/java/.../RealTimeAlertCalculatorTest.java Added new test class with setup and two test methods to verify correct filtering of alert definitions by instance expressions, handling cases with and without spaces.

Sequence Diagram(s)

sequenceDiagram
    participant Test as RealTimeAlertCalculatorTest
    participant Calculator as RealTimeAlertCalculator
    participant MockDeps as Mocked Dependencies

    Test->>MockDeps: Set up mocks for dependencies
    Test->>Calculator: Instantiate with start=false
    Test->>Calculator: Call filterThresholdsByAppAndMetrics()
    Calculator->>MockDeps: (Uses dependencies as needed)
    Calculator-->>Test: Returns filtered alert definitions
    Test->>Test: Assert filtering correctness
Loading

Poem

In the warren of code, a new path appears,
With threads that now start—or not, as one steers.
Regexes trimmed, spaces now tamed,
Tests hop in to see how alerts are named.
Public methods leap, no longer confined,
A rabbit’s delight in the logic refined! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@visz11
Copy link
Collaborator

visz11 commented Apr 21, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 21, 2025

✅ Actions performed

Full review triggered.

@visz11
Copy link
Collaborator

visz11 commented Apr 22, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 22, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculator.java (1)

116-116: Consider adding thread safety to the now-public startCalculate method.

Since this method has been made public for testing purposes, it could potentially be called multiple times, which might lead to redundant worker threads being created.

 public void startCalculate() {
+    // Add a synchronization mechanism or flag to prevent multiple calls
+    // For example: use AtomicBoolean to track if threads are already started
     Runnable runnable = () -> {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b02837b and d4f628a.

📒 Files selected for processing (2)
  • hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculator.java (4 hunks)
  • hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculatorTest.java (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculatorTest.java (1)
web-app/src/app/pojo/AlertDefine.ts (1)
  • AlertDefine (20-41)
🔇 Additional comments (7)
hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculatorTest.java (3)

25-38: Setup looks good with appropriate mocks.

The test setup correctly mocks all required dependencies and passes false to the constructor to prevent threads from starting automatically during tests, which is a good practice for unit testing.


40-60: Well-structured test for expressions with spaces.

This test case correctly verifies that the filter method works properly when there are spaces in the instance expression, which is the established behavior.


62-82: Good test case for the bugfix - handling expressions without spaces.

This test case directly addresses the bug described in the PR summary, verifying that alert expressions without spaces between the comma and quotes (e.g., equals(__instance__,"501045327364864")) are properly filtered. This ensures the fix works as expected.

hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/calculate/RealTimeAlertCalculator.java (4)

76-76: Fixed regex pattern to handle expressions without spaces.

The regular expression pattern has been updated to include \\s* (zero or more whitespace characters), which correctly handles both cases with and without spaces after commas in instance expressions.


85-90: Good constructor refactoring to support testing.

The original constructor now delegates to the new overloaded constructor, passing true to start calculation threads by default. This maintains backward compatibility while enabling better testability.


92-114: Well-documented constructor overload for testability.

The new constructor with the start parameter is well-documented and provides better testability by allowing tests to create instances without starting alert calculation threads.


276-276: Method made public for testing purposes.

This method visibility change allows direct testing of the filtering logic, which is essential for this bugfix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants