fix dm label filtering

This commit is contained in:
HgO 2024-08-12 13:24:40 +02:00
parent 949ace768a
commit eeb39b8aea
2 changed files with 60 additions and 35 deletions

View file

@ -154,20 +154,30 @@ async def create_alert(
cache: Cache = request.app["cache"] cache: Cache = request.app["cache"]
config: Config = request.app["config"] config: Config = request.app["config"]
if config.dm_select_label and config.dm_select_label in alert.labels: if alert.match_all_labels(config.dm_filter_labels):
if alert.match_all_labels(config.dm_filter_labels): logger.info(
dm_select_value = alert.labels[config.dm_select_label] f"Found all DM filter labels in alert labels {config.dm_filter_labels}"
if dm_select_value not in config.dm_users: )
logger.warning( if config.dm_select_label and config.dm_select_label not in alert.labels:
f"Cannot find user with label {config.dm_select_label}={dm_select_value}" logger.warning(
) f"Dismissing alert: Cannot find select label {config.dm_select_label} in alert labels"
return )
return
user_id = config.dm_users[dm_select_value] dm_select_value = alert.labels[config.dm_select_label]
if user_id not in matrix_client_pool.dm_rooms: if dm_select_value not in config.dm_users:
logger.warning(f"Cannot find a matrix room for user {user_id}") logger.warning(
return f"Dismissing alert: Cannot find user with label {config.dm_select_label}={dm_select_value}"
room_id = matrix_client_pool.dm_rooms[user_id] )
return
user_id = config.dm_users[dm_select_value]
if user_id not in matrix_client_pool.dm_rooms:
logger.warning(
f"Dismissing alert: Cannot find a matrix room for user {user_id}"
)
return
room_id = matrix_client_pool.dm_rooms[user_id]
if alert.firing: if alert.firing:
try: try:

View file

@ -56,7 +56,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
self.fake_config.cache_expire_time = 0 self.fake_config.cache_expire_time = 0
self.fake_config.template_dir = None self.fake_config.template_dir = None
self.fake_config.dm_select_label = "uuid" self.fake_config.dm_select_label = "uuid"
self.fake_config.dm_filter_labels = {"matrix-alertbot": re.compile("dm")} self.fake_config.dm_filter_labels = {"matrix": re.compile("dm")}
self.fake_config.dm_users = BiDict( self.fake_config.dm_users = BiDict(
{"a7b37c33-574c-45ac-bb07-a3b314c2da54": "@fake_dm_user:example.com"} {"a7b37c33-574c-45ac-bb07-a3b314c2da54": "@fake_dm_user:example.com"}
) )
@ -107,7 +107,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
"severity": "warning", "severity": "warning",
"job": "job", "job": "job",
"uuid": "a7b37c33-574c-45ac-bb07-a3b314c2da54", "uuid": "a7b37c33-574c-45ac-bb07-a3b314c2da54",
"matrix-alertbot": "dm", "matrix": "dm",
}, },
"annotations": {"description": "some description"}, "annotations": {"description": "some description"},
} }
@ -517,10 +517,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
) )
async def test_create_alert_update_silence(self) -> None: async def test_create_alert_update_silence(self) -> None:
fake_alert = Mock(spec=Alert) fake_alert = Alert(
fake_alert.firing = True fingerprint="fingerprint",
fake_alert.fingerprint = "fingerprint" url="https://example.com",
fake_alert.labels = [] firing=True,
labels={"severity": "critical"},
annotations={"description": "dummy description"},
)
await create_alert(fake_alert, self.fake_room_id, self.fake_request) await create_alert(fake_alert, self.fake_room_id, self.fake_request)
@ -533,10 +536,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
async def test_create_alert_with_silence_not_found_error( async def test_create_alert_with_silence_not_found_error(
self, fake_send_text_to_room: Mock self, fake_send_text_to_room: Mock
) -> None: ) -> None:
fake_alert = Mock(spec=Alert) fake_alert = Alert(
fake_alert.firing = True fingerprint="fingerprint",
fake_alert.fingerprint = "fingerprint" url="https://example.com",
fake_alert.labels = [] firing=True,
labels={"severity": "critical"},
annotations={"description": "dummy description"},
)
self.fake_alertmanager_client.update_silence.side_effect = SilenceNotFoundError self.fake_alertmanager_client.update_silence.side_effect = SilenceNotFoundError
@ -562,10 +568,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
async def test_create_alert_with_silence_extend_error( async def test_create_alert_with_silence_extend_error(
self, fake_send_text_to_room: Mock self, fake_send_text_to_room: Mock
) -> None: ) -> None:
fake_alert = Mock(spec=Alert) fake_alert = Alert(
fake_alert.firing = True fingerprint="fingerprint",
fake_alert.fingerprint = "fingerprint" url="https://example.com",
fake_alert.labels = [] firing=True,
labels={"severity": "critical"},
annotations={"description": "dummy description"},
)
self.fake_alertmanager_client.update_silence.side_effect = SilenceExtendError self.fake_alertmanager_client.update_silence.side_effect = SilenceExtendError
@ -589,10 +598,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
@patch.object(matrix_alertbot.webhook, "send_text_to_room", autospec=True) @patch.object(matrix_alertbot.webhook, "send_text_to_room", autospec=True)
async def test_create_alert_not_firing(self, fake_send_text_to_room: Mock) -> None: async def test_create_alert_not_firing(self, fake_send_text_to_room: Mock) -> None:
fake_alert = Mock(spec=Alert) fake_alert = Alert(
fake_alert.firing = False fingerprint="fingerprint",
fake_alert.fingerprint = "fingerprint" url="https://example.com",
fake_alert.labels = [] firing=False,
labels={},
annotations={"description": "dummy description"},
)
await create_alert(fake_alert, self.fake_room_id, self.fake_request) await create_alert(fake_alert, self.fake_room_id, self.fake_request)
@ -610,10 +622,13 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
async def test_create_alert_not_firing_raise_matrix_client_error( async def test_create_alert_not_firing_raise_matrix_client_error(
self, fake_send_text_to_room: Mock self, fake_send_text_to_room: Mock
) -> None: ) -> None:
fake_alert = Mock(spec=Alert) fake_alert = Alert(
fake_alert.firing = False fingerprint="fingerprint",
fake_alert.fingerprint = "fingerprint" url="https://example.com",
fake_alert.labels = [] firing=False,
labels={},
annotations={"description": "dummy description"},
)
self.fake_matrix_client_pool.matrix_client = None self.fake_matrix_client_pool.matrix_client = None