-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
import logging | ||
|
||
import celpy | ||
from sqlmodel import Session | ||
|
||
from keep.api.core.db import get_session_sync | ||
from keep.api.models.alert import AlertDto | ||
from keep.api.models.db.blackout import BlackoutRule | ||
from keep.api.utils.cel_utils import preprocess_cel_expression | ||
|
||
|
||
class BlackoutsBl: | ||
def __init__(self, tenant_id: str, session: Session | None) -> None: | ||
self.logger = logging.getLogger(__name__) | ||
self.tenant_id = tenant_id | ||
session = session if session else get_session_sync() | ||
self.blackouts: list[BlackoutRule] = ( | ||
session.query(BlackoutRule) | ||
.filter(BlackoutRule.tenant_id == tenant_id) | ||
.filter(BlackoutRule.enabled == True) | ||
.all() | ||
) | ||
|
||
def check_if_alert_in_blackout(self, alert: AlertDto) -> bool: | ||
extra = {"tenant_id": self.tenant_id, "fingerprint": alert.fingerprint} | ||
self.logger.info("Checking blackout for alert", extra=extra) | ||
env = celpy.Environment() | ||
|
||
for blackout in self.blackouts: | ||
cel = preprocess_cel_expression(blackout.cel_query) | ||
ast = env.compile(cel) | ||
prgm = env.program(ast) | ||
|
||
payload = alert.dict() | ||
# todo: fix this in the future | ||
payload["source"] = payload["source"][0] | ||
|
||
activation = celpy.json_to_cel(json.loads(json.dumps(payload, default=str))) | ||
|
||
try: | ||
cel_result = prgm.evaluate(activation) | ||
except celpy.evaluation.CELEvalError as e: | ||
if "no such member" in str(e): | ||
continue | ||
# wtf | ||
raise | ||
if cel_result: | ||
self.logger.info( | ||
"Alert is blacked out", extra={**extra, "blackout_id": blackout.id} | ||
) | ||
return True | ||
self.logger.info("Alert is not blacked out", extra=extra) | ||
return False |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from datetime import datetime, timedelta | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from keep.api.bl.blackouts_bl import BlackoutsBl | ||
from keep.api.models.alert import AlertDto | ||
from keep.api.models.db.blackout import BlackoutRule | ||
|
||
|
||
@pytest.fixture | ||
def mock_session(): | ||
return MagicMock() | ||
|
||
|
||
@pytest.fixture | ||
def mock_blackout_rule(): | ||
return BlackoutRule( | ||
id=1, | ||
name="Test Blackout", | ||
tenant_id="test-tenant", | ||
cel_query='source == "test-source"', | ||
start_time=datetime.utcnow() - timedelta(hours=1), | ||
end_time=datetime.utcnow() + timedelta(hours=1), | ||
enabled=True, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def alert_dto(): | ||
return AlertDto( | ||
id="test-alert", | ||
source=["test-source"], | ||
name="Test Alert", | ||
status="firing", | ||
severity="critical", | ||
lastReceived="2021-08-01T00:00:00Z", | ||
) | ||
|
||
|
||
def test_alert_in_blackout(mock_session, mock_blackout_rule, alert_dto): | ||
mock_session.query.return_value.filter.return_value.filter.return_value.all.return_value = [ | ||
mock_blackout_rule | ||
] | ||
|
||
blackout_bl = BlackoutsBl(tenant_id="test-tenant", session=mock_session) | ||
result = blackout_bl.check_if_alert_in_blackout(alert_dto) | ||
|
||
assert result is True | ||
|
||
|
||
def test_alert_not_in_blackout(mock_session, mock_blackout_rule, alert_dto): | ||
# Modify the cel_query so that the alert won't match | ||
mock_blackout_rule.cel_query = 'source == "other-source"' | ||
mock_session.query.return_value.filter.return_value.filter.return_value.all.return_value = [ | ||
mock_blackout_rule | ||
] | ||
|
||
blackout_bl = BlackoutsBl(tenant_id="test-tenant", session=mock_session) | ||
result = blackout_bl.check_if_alert_in_blackout(alert_dto) | ||
|
||
assert result is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters