2020-07-02 03:40:54 +02:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
|
2020-07-02 02:20:55 +02:00
|
|
|
import sentry_sdk
|
|
|
|
from sentry_sdk.integrations import Integration
|
|
|
|
from sentry_sdk.integrations.django import DjangoIntegration
|
|
|
|
from sentry_sdk.integrations.redis import RedisIntegration
|
|
|
|
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
|
2020-07-02 03:40:54 +02:00
|
|
|
from sentry_sdk.utils import capture_internal_exceptions
|
2020-07-02 02:20:55 +02:00
|
|
|
|
2020-07-28 02:09:57 +02:00
|
|
|
from version import ZULIP_VERSION
|
|
|
|
|
2020-07-02 02:20:55 +02:00
|
|
|
from .config import PRODUCTION
|
|
|
|
|
2020-07-02 03:40:54 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from sentry_sdk._types import Event, Hint
|
|
|
|
|
2020-08-08 04:36:12 +02:00
|
|
|
def add_context(event: 'Event', hint: 'Hint') -> Optional['Event']:
|
|
|
|
if "exc_info" in hint:
|
|
|
|
_, exc_value, _ = hint["exc_info"]
|
|
|
|
# Ignore GeneratorExit, KeyboardInterrupt, and SystemExit exceptions
|
|
|
|
if not isinstance(exc_value, Exception):
|
|
|
|
return None
|
2020-07-02 03:40:54 +02:00
|
|
|
from zerver.models import get_user_profile_by_id
|
|
|
|
with capture_internal_exceptions():
|
|
|
|
user_info = event.get("user", {})
|
|
|
|
if user_info.get("id"):
|
|
|
|
user_profile = get_user_profile_by_id(user_info["id"])
|
|
|
|
user_info["realm"] = user_profile.realm.string_id or 'root'
|
2020-07-28 03:13:14 +02:00
|
|
|
user_info["role"] = user_profile.get_role_name()
|
2020-07-02 03:40:54 +02:00
|
|
|
return event
|
2020-07-02 02:20:55 +02:00
|
|
|
|
|
|
|
def setup_sentry(dsn: Optional[str], *integrations: Integration) -> None:
|
|
|
|
if not dsn:
|
|
|
|
return
|
|
|
|
sentry_sdk.init(
|
|
|
|
dsn=dsn,
|
|
|
|
environment="production" if PRODUCTION else "development",
|
2020-07-28 02:09:57 +02:00
|
|
|
release=ZULIP_VERSION,
|
2020-07-02 02:20:55 +02:00
|
|
|
integrations=[
|
|
|
|
DjangoIntegration(),
|
|
|
|
RedisIntegration(),
|
|
|
|
SqlalchemyIntegration(),
|
|
|
|
*integrations,
|
|
|
|
],
|
2020-07-02 03:40:54 +02:00
|
|
|
before_send=add_context,
|
2020-07-02 02:20:55 +02:00
|
|
|
)
|