Remove .env_example file and implement MetricsUpdater service for enhanced metrics tracking. Update bot.py to start and stop metrics updater, and improve database connection handling in CRUD operations with metrics tracking. Update README with details on metrics issues and fixes.

This commit is contained in:
2025-09-08 23:18:55 +03:00
parent 596a2fa813
commit 23c30a78e2
11 changed files with 744 additions and 49 deletions

View File

@@ -127,6 +127,27 @@ class MetricsService:
['status']
)
# Метрики пула соединений
self.db_pool_size = Gauge(
'anon_bot_db_pool_size',
'Database connection pool size'
)
self.db_pool_created_connections = Gauge(
'anon_bot_db_pool_created_connections',
'Number of created connections in pool'
)
self.db_pool_available_connections = Gauge(
'anon_bot_db_pool_available_connections',
'Number of available connections in pool'
)
self.db_pool_utilization_percent = Gauge(
'anon_bot_db_pool_utilization_percent',
'Database connection pool utilization percentage'
)
# Метрики пагинации
self.pagination_requests_total = Counter(
'anon_bot_pagination_requests_total',
@@ -237,13 +258,25 @@ class MetricsService:
self.db_query_duration.labels(operation=operation, table=table).observe(duration)
def record_db_connection(self, status: str):
"""Записать метрики подключения к БД"""
"""Записать метрики подключения к БД (только для реальных соединений пула)"""
self.db_connections_total.labels(status=status).inc()
if status == "opened":
self.db_connections_active.inc()
elif status == "closed":
self.db_connections_active.dec()
def update_db_connections_from_pool(self, active_count: int):
"""Обновить количество активных соединений на основе реального пула"""
# Сбрасываем счетчик и устанавливаем реальное значение
self.db_connections_active.set(active_count)
def update_db_pool_metrics(self, pool_stats: dict):
"""Обновить метрики пула соединений"""
self.db_pool_size.set(pool_stats.get("pool_size", 0))
self.db_pool_created_connections.set(pool_stats.get("created_connections", 0))
self.db_pool_available_connections.set(pool_stats.get("available_connections", 0))
self.db_pool_utilization_percent.set(pool_stats.get("utilization_percent", 0))
def record_pagination_time(self, entity_type: str, duration: float, method: str = "cursor"):
"""Записать время пагинации"""
self.pagination_requests_total.labels(entity_type=entity_type, method=method).inc()