From a8c953477c77bf294ea084c08469d83404145d1d Mon Sep 17 00:00:00 2001 From: HgO Date: Sun, 3 Nov 2024 14:28:15 +0100 Subject: [PATCH 1/2] add more debug messages --- matrix_alertbot/callback.py | 43 +++++++++++++++++++++++++++++++++++++ matrix_alertbot/matrix.py | 26 +++++++++++++++++++++- matrix_alertbot/webhook.py | 18 ++++++++++------ tests/test_matrix.py | 8 +++---- tests/test_webhook.py | 21 +++++++++++------- 5 files changed, 97 insertions(+), 19 deletions(-) diff --git a/matrix_alertbot/callback.py b/matrix_alertbot/callback.py index 82e2ec6..b5f4dd9 100644 --- a/matrix_alertbot/callback.py +++ b/matrix_alertbot/callback.py @@ -4,6 +4,14 @@ import logging import re from diskcache import Cache +from nio import ( + AccountDataEvent, + EphemeralEvent, + Event, + PresenceEvent, + Response, + ToDeviceEvent, +) from nio.client import AsyncClient from nio.events import ( InviteMemberEvent, @@ -525,3 +533,38 @@ class Callbacks: raise SendRetryError( f"{response_event.status_code} - {response_event.message}" ) + + async def debug_room_event(self, room: MatrixRoom, event: Event): + logger.debug( + f"Bot {self.matrix_client.user_id} | Room ID {room.room_id} | {type(event).__name__} | Event ID {event.event_id} | Received room event: {event.source}" + ) + + async def debug_presence(self, event: PresenceEvent): + logger.debug( + f"Bot {self.matrix_client.user_id} | User ID {event.user_id} | Received presence event: {event.presence}" + ) + + async def debug_ephemeral(self, room: MatrixRoom, event: EphemeralEvent): + logger.debug( + f"Bot {self.matrix_client.user_id} | Room ID {room.room_id} | {type(event).__name__} | Received ephemeral event: {event}" + ) + + async def debug_account_data(self, event: AccountDataEvent): + logger.debug( + f"Bot {self.matrix_client.user_id} | {type(event).__name__} | Received account data event: {event}" + ) + + async def debug_room_account_data(self, room: MatrixRoom, event: AccountDataEvent): + logger.debug( + f"Bot {self.matrix_client.user_id} | Room ID {room.room_id} | {type(event).__name__} | Received room account data event: {event}" + ) + + async def debug_to_device(self, event: ToDeviceEvent): + logger.debug( + f"Bot {self.matrix_client.user_id} | {type(event).__name__} | Sender {event.sender} | Received to device event: {event.source}" + ) + + async def debug_response(self, response: Response): + logger.debug( + f"Bot {self.matrix_client.user_id} | {type(response).__name__} | Received response: {response}" + ) diff --git a/matrix_alertbot/matrix.py b/matrix_alertbot/matrix.py index 4b6da27..4293e8a 100644 --- a/matrix_alertbot/matrix.py +++ b/matrix_alertbot/matrix.py @@ -10,7 +10,16 @@ from typing import Dict, List, Optional, Tuple from aiohttp import ClientConnectionError, ServerDisconnectedError from diskcache import Cache -from nio import RoomPreset, RoomVisibility +from nio import ( + AccountDataEvent, + EphemeralEvent, + Event, + PresenceEvent, + Response, + RoomPreset, + RoomVisibility, + ToDeviceEvent, +) from nio.client import AsyncClient, AsyncClientConfig from nio.events import ( InviteMemberEvent, @@ -179,6 +188,21 @@ class MatrixClientPool: matrix_client.add_to_device_callback( callbacks.key_verification_end, (KeyVerificationMac,) ) + matrix_client.add_event_callback(callbacks.debug_room_event, (Event,)) + matrix_client.add_presence_callback(callbacks.debug_presence, (PresenceEvent,)) + matrix_client.add_ephemeral_callback( + callbacks.debug_ephemeral, (EphemeralEvent,) + ) + matrix_client.add_global_account_data_callback( + callbacks.debug_account_data, (AccountDataEvent,) + ) + matrix_client.add_room_account_data_callback( + callbacks.debug_room_account_data, (AccountDataEvent,) + ) + matrix_client.add_to_device_callback( + callbacks.debug_to_device, (ToDeviceEvent,) + ) + matrix_client.add_response_callback(callbacks.debug_response, (Response,)) return matrix_client diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index a2a824b..01fb1ca 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -118,24 +118,27 @@ async def create_alerts(request: web_request.Request) -> web.Response: body=f"An error occured with Alertmanager when handling alert with fingerprint {alert.fingerprint}.", ) except (SendRetryError, LocalProtocolError, ClientError) as e: - logger.error( - f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" + logger.exception( + f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}", + exc_info=e, ) return web.Response( status=500, body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.", ) except MatrixClientError as e: - logger.error( - f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" + logger.exception( + f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}", + exc_info=e, ) return web.Response( status=500, body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.", ) except Exception as e: - logger.error( - f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" + logger.exception( + f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}", + exc_info=e, ) return web.Response( status=500, @@ -201,6 +204,9 @@ async def create_alert( event = await send_text_to_room( matrix_client_pool.matrix_client, room_id, plaintext, html, notice=False ) + logger.info( + f"Sent alert {alert.fingerprint} to room {room_id} with event ID {event.event_id}" + ) else: raise MatrixClientError("No matrix client available") diff --git a/tests/test_matrix.py b/tests/test_matrix.py index e23c8fa..850695c 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -304,8 +304,8 @@ class MatrixClientPoolTestCase(unittest.IsolatedAsyncioTestCase): self.fake_account_config_1.homeserver_url, matrix_client_1.homeserver ) self.assertEqual(self.fake_config.store_dir, matrix_client_1.store_path) - self.assertEqual(6, len(matrix_client_1.event_callbacks)) - self.assertEqual(4, len(matrix_client_1.to_device_callbacks)) + self.assertEqual(7, len(matrix_client_1.event_callbacks)) + self.assertEqual(5, len(matrix_client_1.to_device_callbacks)) fake_async_client_config.assert_has_calls( [ @@ -348,8 +348,8 @@ class MatrixClientPoolTestCase(unittest.IsolatedAsyncioTestCase): self.fake_account_config_1.homeserver_url, matrix_client_1.homeserver ) self.assertEqual(self.fake_config.store_dir, matrix_client_1.store_path) - self.assertEqual(6, len(matrix_client_1.event_callbacks)) - self.assertEqual(4, len(matrix_client_1.to_device_callbacks)) + self.assertEqual(7, len(matrix_client_1.event_callbacks)) + self.assertEqual(5, len(matrix_client_1.to_device_callbacks)) self.assertEqual(5, matrix_client_1.config.max_limit_exceeded) self.assertEqual(3, matrix_client_1.config.max_timeouts) self.assertTrue(matrix_client_1.config.store_sync_tokens) diff --git a/tests/test_webhook.py b/tests/test_webhook.py index 1f3f197..0335075 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -159,7 +159,8 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): "some description2", notice=False, ), - ] + ], + any_order=True ) self.fake_cache.set.assert_called_once_with( fake_send_text_to_room.return_value.event_id, @@ -209,7 +210,8 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): "some description2", notice=False, ), - ] + ], + any_order=True ) self.fake_cache.set.assert_called_once_with( fake_send_text_to_room.return_value.event_id, @@ -450,8 +452,9 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): self.fake_cache.set.assert_not_called() self.fake_cache.delete.assert_called_once_with("fingerprint1") - fake_logger.error.assert_called_once_with( - "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Local protocol error" + fake_logger.exception.assert_called_once_with( + "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", + exc_info=fake_send_text_to_room.side_effect ) @patch.object(matrix_alertbot.webhook, "logger", autospec=True) @@ -480,8 +483,9 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): ) fake_create_alert.assert_called_once() - fake_logger.error.assert_called_once_with( - "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Matrix client error" + fake_logger.exception.assert_called_once_with( + "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", + exc_info=fake_create_alert.side_effect ) @patch.object(matrix_alertbot.webhook, "logger", autospec=True) @@ -512,8 +516,9 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): self.fake_cache.set.assert_not_called() self.fake_cache.delete.assert_called_once_with("fingerprint1") - fake_logger.error.assert_called_once_with( - "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Exception" + fake_logger.exception.assert_called_once_with( + "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", + exc_info=fake_send_text_to_room.side_effect ) async def test_create_alert_update_silence(self) -> None: From ecf6e8b01750fb1b668abd242e448bd49135259f Mon Sep 17 00:00:00 2001 From: HgO Date: Sun, 3 Nov 2024 14:34:20 +0100 Subject: [PATCH 2/2] linting --- tests/test_webhook.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_webhook.py b/tests/test_webhook.py index 0335075..525b0ea 100644 --- a/tests/test_webhook.py +++ b/tests/test_webhook.py @@ -160,7 +160,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): notice=False, ), ], - any_order=True + any_order=True, ) self.fake_cache.set.assert_called_once_with( fake_send_text_to_room.return_value.event_id, @@ -211,7 +211,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): notice=False, ), ], - any_order=True + any_order=True, ) self.fake_cache.set.assert_called_once_with( fake_send_text_to_room.return_value.event_id, @@ -454,7 +454,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): fake_logger.exception.assert_called_once_with( "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", - exc_info=fake_send_text_to_room.side_effect + exc_info=fake_send_text_to_room.side_effect, ) @patch.object(matrix_alertbot.webhook, "logger", autospec=True) @@ -485,7 +485,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): fake_logger.exception.assert_called_once_with( "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", - exc_info=fake_create_alert.side_effect + exc_info=fake_create_alert.side_effect, ) @patch.object(matrix_alertbot.webhook, "logger", autospec=True) @@ -518,7 +518,7 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase): fake_logger.exception.assert_called_once_with( "Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com", - exc_info=fake_send_text_to_room.side_effect + exc_info=fake_send_text_to_room.side_effect, ) async def test_create_alert_update_silence(self) -> None: