add more debug messages

This commit is contained in:
HgO 2024-11-03 14:28:15 +01:00
parent 6feeadda69
commit a8c953477c
5 changed files with 97 additions and 19 deletions

View file

@ -4,6 +4,14 @@ import logging
import re import re
from diskcache import Cache from diskcache import Cache
from nio import (
AccountDataEvent,
EphemeralEvent,
Event,
PresenceEvent,
Response,
ToDeviceEvent,
)
from nio.client import AsyncClient from nio.client import AsyncClient
from nio.events import ( from nio.events import (
InviteMemberEvent, InviteMemberEvent,
@ -525,3 +533,38 @@ class Callbacks:
raise SendRetryError( raise SendRetryError(
f"{response_event.status_code} - {response_event.message}" 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}"
)

View file

@ -10,7 +10,16 @@ from typing import Dict, List, Optional, Tuple
from aiohttp import ClientConnectionError, ServerDisconnectedError from aiohttp import ClientConnectionError, ServerDisconnectedError
from diskcache import Cache 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.client import AsyncClient, AsyncClientConfig
from nio.events import ( from nio.events import (
InviteMemberEvent, InviteMemberEvent,
@ -179,6 +188,21 @@ class MatrixClientPool:
matrix_client.add_to_device_callback( matrix_client.add_to_device_callback(
callbacks.key_verification_end, (KeyVerificationMac,) 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 return matrix_client

View file

@ -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}.", body=f"An error occured with Alertmanager when handling alert with fingerprint {alert.fingerprint}.",
) )
except (SendRetryError, LocalProtocolError, ClientError) as e: except (SendRetryError, LocalProtocolError, ClientError) as e:
logger.error( logger.exception(
f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}",
exc_info=e,
) )
return web.Response( return web.Response(
status=500, status=500,
body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.", body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.",
) )
except MatrixClientError as e: except MatrixClientError as e:
logger.error( logger.exception(
f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}",
exc_info=e,
) )
return web.Response( return web.Response(
status=500, status=500,
body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.", body=f"An error occured when sending alert with fingerprint '{alert.fingerprint}' to Matrix room.",
) )
except Exception as e: except Exception as e:
logger.error( logger.exception(
f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}: {e}" f"Unable to send alert {alert.fingerprint} to Matrix room {room_id}",
exc_info=e,
) )
return web.Response( return web.Response(
status=500, status=500,
@ -201,6 +204,9 @@ async def create_alert(
event = await send_text_to_room( event = await send_text_to_room(
matrix_client_pool.matrix_client, room_id, plaintext, html, notice=False 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: else:
raise MatrixClientError("No matrix client available") raise MatrixClientError("No matrix client available")

View file

@ -304,8 +304,8 @@ class MatrixClientPoolTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_account_config_1.homeserver_url, matrix_client_1.homeserver 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(self.fake_config.store_dir, matrix_client_1.store_path)
self.assertEqual(6, len(matrix_client_1.event_callbacks)) self.assertEqual(7, len(matrix_client_1.event_callbacks))
self.assertEqual(4, len(matrix_client_1.to_device_callbacks)) self.assertEqual(5, len(matrix_client_1.to_device_callbacks))
fake_async_client_config.assert_has_calls( 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.fake_account_config_1.homeserver_url, matrix_client_1.homeserver
) )
self.assertEqual(self.fake_config.store_dir, matrix_client_1.store_path) self.assertEqual(self.fake_config.store_dir, matrix_client_1.store_path)
self.assertEqual(6, len(matrix_client_1.event_callbacks)) self.assertEqual(7, len(matrix_client_1.event_callbacks))
self.assertEqual(4, len(matrix_client_1.to_device_callbacks)) self.assertEqual(5, len(matrix_client_1.to_device_callbacks))
self.assertEqual(5, matrix_client_1.config.max_limit_exceeded) self.assertEqual(5, matrix_client_1.config.max_limit_exceeded)
self.assertEqual(3, matrix_client_1.config.max_timeouts) self.assertEqual(3, matrix_client_1.config.max_timeouts)
self.assertTrue(matrix_client_1.config.store_sync_tokens) self.assertTrue(matrix_client_1.config.store_sync_tokens)

View file

@ -159,7 +159,8 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
"some description2", "some description2",
notice=False, notice=False,
), ),
] ],
any_order=True
) )
self.fake_cache.set.assert_called_once_with( self.fake_cache.set.assert_called_once_with(
fake_send_text_to_room.return_value.event_id, fake_send_text_to_room.return_value.event_id,
@ -209,7 +210,8 @@ class WebhookApplicationTestCase(aiohttp.test_utils.AioHTTPTestCase):
"some description2", "some description2",
notice=False, notice=False,
), ),
] ],
any_order=True
) )
self.fake_cache.set.assert_called_once_with( self.fake_cache.set.assert_called_once_with(
fake_send_text_to_room.return_value.event_id, 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.set.assert_not_called()
self.fake_cache.delete.assert_called_once_with("fingerprint1") self.fake_cache.delete.assert_called_once_with("fingerprint1")
fake_logger.error.assert_called_once_with( fake_logger.exception.assert_called_once_with(
"Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Local protocol error" "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) @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_create_alert.assert_called_once()
fake_logger.error.assert_called_once_with( fake_logger.exception.assert_called_once_with(
"Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Matrix client error" "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) @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.set.assert_not_called()
self.fake_cache.delete.assert_called_once_with("fingerprint1") self.fake_cache.delete.assert_called_once_with("fingerprint1")
fake_logger.error.assert_called_once_with( fake_logger.exception.assert_called_once_with(
"Unable to send alert fingerprint1 to Matrix room !abcdefg:example.com: Exception" "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: async def test_create_alert_update_silence(self) -> None: