mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	This fixes an issue where update-prod-static would crash with the following exception:
2021-07-19 03:59:24,601 upgrade-zulip-stage-2: Building static assets...
Traceback (most recent call last):
  File "./tools/update-prod-static", line 27, in <module>
    os.chdir(settings.DEPLOY_ROOT)
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./tools/../zproject/settings.py", line 20, in <module>
    from .computed_settings import *  # noqa: F401,F403 isort: skip
  File "./tools/../zproject/computed_settings.py", line 1186, in <module>
    from .sentry import setup_sentry
  File "./tools/../zproject/sentry.py", line 12, in <module>
    from zerver.lib.request import get_request_notes
  File "./tools/../zerver/lib/request.py", line 28, in <module>
    import zerver.lib.rate_limiter as rate_limiter
  File "./tools/../zerver/lib/rate_limiter.py", line 14, in <module>
    from zerver.models import UserProfile
  File "./tools/../zerver/models.py", line 26, in <module>
    from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/home/zulip/deployments/2021-07-19-03-58-39/zulip-py3-venv/lib/python3.6/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Traceback (most recent call last):
  File "/home/zulip/deployments/2021-07-19-03-58-39/scripts/lib/upgrade-zulip-stage-2", line 220, in <module>
    subprocess.check_call(["./tools/update-prod-static"], preexec_fn=su_to_zulip)
  File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['./tools/update-prod-static']' returned non-zero exit status 1.
		
	
		
			
				
	
	
		
			94 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import TYPE_CHECKING, Optional
 | 
						|
 | 
						|
import sentry_sdk
 | 
						|
from django.utils.translation import override as override_language
 | 
						|
from sentry_sdk.integrations.django import DjangoIntegration
 | 
						|
from sentry_sdk.integrations.logging import ignore_logger
 | 
						|
from sentry_sdk.integrations.redis import RedisIntegration
 | 
						|
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
 | 
						|
from sentry_sdk.utils import capture_internal_exceptions
 | 
						|
 | 
						|
from version import ZULIP_VERSION
 | 
						|
 | 
						|
if TYPE_CHECKING:
 | 
						|
    from sentry_sdk._types import Event, Hint
 | 
						|
 | 
						|
 | 
						|
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
 | 
						|
    from django.conf import settings
 | 
						|
 | 
						|
    from zerver.lib.request import get_current_request, get_request_notes
 | 
						|
    from zerver.models import get_user_profile_by_id
 | 
						|
 | 
						|
    with capture_internal_exceptions():
 | 
						|
        # event.user is the user context, from Sentry, which is
 | 
						|
        # pre-populated with some keys via its Django integration:
 | 
						|
        # https://docs.sentry.io/platforms/python/guides/django/enriching-error-data/additional-data/identify-user/
 | 
						|
        event.setdefault("tags", {})
 | 
						|
        user_info = event.get("user", {})
 | 
						|
        if user_info.get("id"):
 | 
						|
            user_profile = get_user_profile_by_id(user_info["id"])
 | 
						|
            event["tags"]["realm"] = user_info["realm"] = user_profile.realm.string_id or "root"
 | 
						|
            with override_language(settings.LANGUAGE_CODE):
 | 
						|
                # str() to force the lazy-translation to apply now,
 | 
						|
                # since it won't serialize into json for Sentry otherwise
 | 
						|
                user_info["role"] = str(user_profile.get_role_name())
 | 
						|
 | 
						|
        # These are PII, and should be scrubbed
 | 
						|
        if "username" in user_info:
 | 
						|
            del user_info["username"]
 | 
						|
        if "email" in user_info:
 | 
						|
            del user_info["email"]
 | 
						|
 | 
						|
        request = get_current_request()
 | 
						|
        if request:
 | 
						|
            request_notes = get_request_notes(request)
 | 
						|
            if request_notes.client is not None:
 | 
						|
                event["tags"]["client"] = request_notes.client.name
 | 
						|
            if request_notes.realm is not None:
 | 
						|
                event["tags"].setdefault("realm", request_notes.realm.string_id)
 | 
						|
    return event
 | 
						|
 | 
						|
 | 
						|
def setup_sentry(dsn: Optional[str], environment: str) -> None:
 | 
						|
    if not dsn:
 | 
						|
        return
 | 
						|
    sentry_sdk.init(
 | 
						|
        dsn=dsn,
 | 
						|
        environment=environment,
 | 
						|
        release=ZULIP_VERSION,
 | 
						|
        integrations=[
 | 
						|
            DjangoIntegration(),
 | 
						|
            RedisIntegration(),
 | 
						|
            SqlalchemyIntegration(),
 | 
						|
        ],
 | 
						|
        before_send=add_context,
 | 
						|
        # Because we strip the email/username from the Sentry data
 | 
						|
        # above, the effect of this flag is that the requests/users
 | 
						|
        # involved in exceptions will be identified in Sentry only by
 | 
						|
        # their IP address, user ID, realm, and role.  We consider
 | 
						|
        # this an appropriate balance between avoiding Sentry getting
 | 
						|
        # PII while having the identifiers needed to determine that an
 | 
						|
        # exception only affects a small subset of users or realms.
 | 
						|
        send_default_pii=True,
 | 
						|
    )
 | 
						|
 | 
						|
    # Ignore all of the loggers from django.security that are for user
 | 
						|
    # errors; see https://docs.djangoproject.com/en/3.0/ref/exceptions/#suspiciousoperation
 | 
						|
    ignore_logger("django.security.SuspiciousOperation")
 | 
						|
    ignore_logger("django.security.DisallowedHost")
 | 
						|
    ignore_logger("django.security.DisallowedModelAdminLookup")
 | 
						|
    ignore_logger("django.security.DisallowedModelAdminToField")
 | 
						|
    ignore_logger("django.security.DisallowedRedirect")
 | 
						|
    ignore_logger("django.security.InvalidSessionKey")
 | 
						|
    ignore_logger("django.security.RequestDataTooBig")
 | 
						|
    ignore_logger("django.security.SuspiciousFileOperation")
 | 
						|
    ignore_logger("django.security.SuspiciousMultipartForm")
 | 
						|
    ignore_logger("django.security.SuspiciousSession")
 | 
						|
    ignore_logger("django.security.TooManyFieldsSent")
 |