Files
core/utils/sentry.py
Untone b611ed541c
All checks were successful
Deploy on push / deploy (push) Successful in 5m36s
nogt-test
2025-10-06 19:40:57 +03:00

69 lines
3.2 KiB
Python
Raw Permalink 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.
import logging
import sentry_sdk
from sentry_sdk.integrations.ariadne import AriadneIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
from settings import GLITCHTIP_DSN
logger = logging.getLogger(__name__)
def start_sentry() -> None:
"""🔍 Инициализация Sentry/GlitchTip с дублированием логов"""
try:
logger.info("[utils.sentry] Sentry init started...")
# 🔍 Проверяем наличие DSN
if not GLITCHTIP_DSN:
logger.warning("[utils.sentry] GLITCHTIP_DSN не установлен, пропускаем инициализацию Sentry")
return
logger.info(f"[utils.sentry] Используем DSN: {GLITCHTIP_DSN[:50]}...")
# 🧾 Настройка LoggingIntegration для дублирования логов
# level=logging.WARNING - отправляем в GlitchTip только WARNING и выше
# event_level=logging.ERROR - создаем события только для ERROR и выше
sentry_logging = LoggingIntegration(
level=logging.WARNING, # Захватываем WARNING+ в breadcrumbs
event_level=logging.ERROR, # Создаем события только для ERROR+
)
# 🔍 Настраиваем фильтрацию через before_send callback
def before_send(event, hint):
"""Фильтрует события перед отправкой в Sentry"""
if "logentry" in event and "message" in event["logentry"]:
message = event["logentry"]["message"]
auth_spam_phrases = [
"Требуется авторизация",
"AuthorizationError",
"load_drafts",
"GetUserDocuments",
"GetDrafts",
"decorated_function",
]
if any(phrase in message for phrase in auth_spam_phrases):
return None # Блокируем отправку
return event
sentry_sdk.init(
dsn=GLITCHTIP_DSN,
traces_sample_rate=1.0, # Захват 100% транзакций
profiles_sample_rate=1.0, # Профилирование 100% транзакций
enable_tracing=True,
before_send=before_send, # 🔍 Фильтрация спама авторизации
integrations=[
sentry_logging, # 🔍 Дублирование логов
StarletteIntegration(),
AriadneIntegration(),
SqlalchemyIntegration(),
],
send_default_pii=True, # Отправка информации о пользователе (PII)
)
logger.info("[utils.sentry] Sentry initialized successfully.")
except (sentry_sdk.utils.BadDsn, ImportError, ValueError, TypeError) as _e:
logger.warning("[utils.sentry] Failed to initialize Sentry", exc_info=True)