Enhance Makefile and update metrics handling in bot

- Added new commands in the Makefile for restarting individual services: `restart-bot`, `restart-prometheus`, and `restart-grafana`.
- Updated Prometheus and Grafana dashboard expressions for better metrics aggregation.
- Removed the `main_with_metrics.py` file and integrated metrics handling directly into the main bot file.
- Refactored middleware to improve metrics tracking and error handling across message and callback processing.
- Optimized metrics recording with enhanced bucket configurations for better performance monitoring.
This commit is contained in:
2025-08-29 18:23:17 +03:00
parent c68db87901
commit f097d69dd4
13 changed files with 166 additions and 408 deletions

View File

@@ -138,6 +138,8 @@ class PostService:
self.db = db
self.settings = settings
@track_time("handle_text_post", "post_service")
@track_errors("post_service", "handle_text_post")
async def handle_text_post(self, message: types.Message, first_name: str) -> None:
"""Handle text post submission"""
post_text = get_text_message(message.text.lower(), first_name, message.from_user.username)
@@ -146,6 +148,8 @@ class PostService:
sent_message_id = await send_text_message(self.settings.group_for_posts, message, post_text, markup)
self.db.add_post_in_db(sent_message_id, message.text, message.from_user.id)
@track_time("handle_photo_post", "post_service")
@track_errors("post_service", "handle_photo_post")
async def handle_photo_post(self, message: types.Message, first_name: str) -> None:
"""Handle photo post submission"""
post_caption = ""
@@ -160,6 +164,8 @@ class PostService:
self.db.add_post_in_db(sent_message.message_id, sent_message.caption, message.from_user.id)
await add_in_db_media(sent_message, self.db)
@track_time("handle_video_post", "post_service")
@track_errors("post_service", "handle_video_post")
async def handle_video_post(self, message: types.Message, first_name: str) -> None:
"""Handle video post submission"""
post_caption = ""
@@ -174,6 +180,8 @@ class PostService:
self.db.add_post_in_db(sent_message.message_id, sent_message.caption, message.from_user.id)
await add_in_db_media(sent_message, self.db)
@track_time("handle_video_note_post", "post_service")
@track_errors("post_service", "handle_video_note_post")
async def handle_video_note_post(self, message: types.Message) -> None:
"""Handle video note post submission"""
markup = get_reply_keyboard_for_post()
@@ -184,6 +192,8 @@ class PostService:
self.db.add_post_in_db(sent_message.message_id, sent_message.caption, message.from_user.id)
await add_in_db_media(sent_message, self.db)
@track_time("handle_audio_post", "post_service")
@track_errors("post_service", "handle_audio_post")
async def handle_audio_post(self, message: types.Message, first_name: str) -> None:
"""Handle audio post submission"""
post_caption = ""
@@ -198,6 +208,8 @@ class PostService:
self.db.add_post_in_db(sent_message.message_id, sent_message.caption, message.from_user.id)
await add_in_db_media(sent_message, self.db)
@track_time("handle_voice_post", "post_service")
@track_errors("post_service", "handle_voice_post")
async def handle_voice_post(self, message: types.Message) -> None:
"""Handle voice post submission"""
markup = get_reply_keyboard_for_post()
@@ -208,6 +220,8 @@ class PostService:
self.db.add_post_in_db(sent_message.message_id, sent_message.caption, message.from_user.id)
await add_in_db_media(sent_message, self.db)
@track_time("handle_media_group_post", "post_service")
@track_errors("post_service", "handle_media_group_post")
async def handle_media_group_post(self, message: types.Message, album: list, first_name: str) -> None:
"""Handle media group post submission"""
post_caption = " "
@@ -229,6 +243,8 @@ class PostService:
message_id=media_group_message_id, helper_message_id=help_message_id
)
@track_time("process_post", "post_service")
@track_errors("post_service", "process_post")
async def process_post(self, message: types.Message, album: Union[list, None] = None) -> None:
"""Process post based on content type"""
first_name = get_first_name(message)