Refactor metrics handling and improve logging

- Removed the MetricsManager initialization from `run_helper.py` to avoid duplication, as metrics are now handled in `main.py`.
- Updated logging levels in `server_prometheus.py` and `metrics_middleware.py` to use debug instead of info for less critical messages.
- Added metrics configuration to `BaseDependencyFactory` for better management of metrics settings.
- Deleted the obsolete `metrics_exporter.py` file to streamline the codebase.
- Updated various tests to reflect changes in the metrics handling and ensure proper functionality.
This commit is contained in:
2025-09-03 00:33:20 +03:00
parent 6fcecff97c
commit c8c7d50cbb
19 changed files with 402 additions and 605 deletions

View File

@@ -75,9 +75,10 @@ class TestGroupHandlers:
assert handlers.admin_reply_service is not None
assert handlers.router is not None
@pytest.mark.asyncio
async def test_handle_message_success(self, mock_db, mock_keyboard_markup, mock_reply_message, mock_state):
"""Test successful message handling"""
mock_db.get_user_by_message_id.return_value = 99999
mock_db.get_user_by_message_id = AsyncMock(return_value=99999)
handlers = create_group_handlers(mock_db, mock_keyboard_markup)
@@ -97,6 +98,7 @@ class TestGroupHandlers:
# Verify state was set
mock_state.set_state.assert_called_once_with(FSM_STATES["CHAT"])
@pytest.mark.asyncio
async def test_handle_message_no_reply(self, mock_db, mock_keyboard_markup, mock_message, mock_state):
"""Test message handling without reply"""
handlers = create_group_handlers(mock_db, mock_keyboard_markup)
@@ -121,9 +123,10 @@ class TestGroupHandlers:
# Verify state was not set
mock_state.set_state.assert_not_called()
@pytest.mark.asyncio
async def test_handle_message_user_not_found(self, mock_db, mock_keyboard_markup, mock_reply_message, mock_state):
"""Test message handling when user is not found"""
mock_db.get_user_by_message_id.return_value = None
mock_db.get_user_by_message_id = AsyncMock(return_value=None)
handlers = create_group_handlers(mock_db, mock_keyboard_markup)
@@ -154,24 +157,27 @@ class TestAdminReplyService:
"""Create service instance"""
return AdminReplyService(mock_db)
def test_get_user_id_for_reply_success(self, service, mock_db):
@pytest.mark.asyncio
async def test_get_user_id_for_reply_success(self, service, mock_db):
"""Test successful user ID retrieval"""
mock_db.get_user_by_message_id.return_value = 12345
mock_db.get_user_by_message_id = AsyncMock(return_value=12345)
result = service.get_user_id_for_reply(111)
result = await service.get_user_id_for_reply(111)
assert result == 12345
mock_db.get_user_by_message_id.assert_called_once_with(111)
def test_get_user_id_for_reply_not_found(self, service, mock_db):
@pytest.mark.asyncio
async def test_get_user_id_for_reply_not_found(self, service, mock_db):
"""Test user ID retrieval when user not found"""
mock_db.get_user_by_message_id.return_value = None
mock_db.get_user_by_message_id = AsyncMock(return_value=None)
with pytest.raises(UserNotFoundError, match="User not found for message_id: 111"):
service.get_user_id_for_reply(111)
await service.get_user_id_for_reply(111)
mock_db.get_user_by_message_id.assert_called_once_with(111)
@pytest.mark.asyncio
async def test_send_reply_to_user(self, service, mock_db):
"""Test sending reply to user"""
message = Mock()