Files
MessageGateway/app/api/v1/endpoints/health.py
Sergey Antropov b90def35ed Initial commit: Message Gateway project
- FastAPI приложение для отправки мониторинговых алертов в мессенджеры
- Поддержка Telegram и MAX/VK
- Интеграция с Grafana, Zabbix, AlertManager
- Автоматическое создание тикетов в Jira
- Управление группами мессенджеров через API
- Декораторы для авторизации и скрытия эндпоинтов
- Подробная документация в папке docs/

Автор: Сергей Антропов
Сайт: https://devops.org.ru
2025-11-12 20:25:11 +03:00

112 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Эндпоинты для проверки здоровья приложения.
Автор: Сергей Антропов
Сайт: https://devops.org.ru
"""
import logging
from fastapi import APIRouter, HTTPException
from typing import Dict, Any
from app.core.metrics import metrics
from app.core.groups import groups_config
from app.core.config import get_settings
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/health", tags=["health"])
@router.get(
"",
name="Проверка здоровья приложения",
response_model=Dict[str, Any],
responses={
200: {
"description": "Приложение работает и готово",
"content": {
"application/json": {
"examples": {
"healthy": {
"summary": "Приложение здорово",
"value": {
"status": "healthy",
"state": "online",
"telegram_bot_configured": True,
"groups_config_available": True
}
},
"not_ready": {
"summary": "Приложение не готово",
"value": {
"status": "not_ready",
"state": "online",
"checks": {
"telegram_bot_configured": False,
"groups_config_available": True
}
}
}
}
}
}
},
503: {
"description": "Приложение не готово к работе",
"content": {
"application/json": {
"example": {
"status": "not_ready",
"checks": {
"telegram_bot_configured": False,
"groups_config_available": True
}
}
}
}
}
}
)
async def health_check() -> Dict[str, Any]:
"""
Проверка здоровья и готовности приложения для Kubernetes probes.
Объединенный endpoint для liveness и readiness probes.
Не требует аутентификации.
Подробная документация: см. docs/api/health.md
"""
metrics.increment_api_endpoint("health")
settings = get_settings()
checks = {
"telegram_bot_configured": bool(settings.telegram_bot_token),
"groups_config_available": False,
}
# Проверяем доступность конфигурации групп
try:
await groups_config.refresh_cache()
checks["groups_config_available"] = True
except Exception as e:
logger.error(f"Ошибка при проверке конфигурации групп: {e}")
checks["groups_config_available"] = False
# Если не все проверки пройдены, возвращаем 503
if not all(checks.values()):
raise HTTPException(
status_code=503,
detail={
"status": "not_ready",
"state": "online",
"checks": checks
}
)
return {
"status": "healthy",
"state": "online",
**checks
}