|
12 | 12 |
|
13 | 13 | from sentry_sdk import get_current_hub, configure_scope, capture_exception, init |
14 | 14 | from .._wsgi import RequestExtractor |
15 | | -from .. import Integration |
16 | 15 |
|
17 | 16 |
|
18 | 17 | try: |
@@ -93,74 +92,54 @@ def _got_request_exception(request=None, **kwargs): |
93 | 92 | capture_exception() |
94 | 93 |
|
95 | 94 |
|
96 | | -MIDDLEWARE_NAME = "sentry_sdk.integrations.django.SentryMiddleware" |
97 | | - |
98 | | -CONFLICTING_MIDDLEWARE = ( |
99 | | - "raven.contrib.django.middleware.SentryMiddleware", |
100 | | - "raven.contrib.django.middleware.SentryLogMiddleware", |
101 | | -) + (MIDDLEWARE_NAME,) |
| 95 | +MIDDLEWARE_NAME = __name__ + ".SentryMiddleware" |
102 | 96 |
|
103 | 97 | _installer_lock = Lock() |
104 | 98 | _installed = False |
105 | 99 |
|
106 | 100 |
|
107 | | -def _install(): |
| 101 | +def install(client): |
108 | 102 | global _installed |
109 | 103 | with _installer_lock: |
110 | 104 | if _installed: |
111 | 105 | return |
112 | 106 |
|
113 | | - client_options, integration_options = DjangoIntegration.parse_environment( |
114 | | - dict((key, getattr(settings, key)) for key in dir(settings)) |
115 | | - ) |
116 | | - |
117 | | - client_options.setdefault("integrations", []).append( |
118 | | - DjangoIntegration(**integration_options) |
119 | | - ) |
120 | | - |
121 | | - init(**client_options) |
| 107 | + _install_impl() |
122 | 108 | _installed = True |
123 | 109 |
|
124 | 110 |
|
125 | 111 | def _install_impl(): |
126 | | - # default settings.MIDDLEWARE is None |
127 | | - if getattr(settings, "MIDDLEWARE", None): |
128 | | - middleware_attr = "MIDDLEWARE" |
129 | | - else: |
130 | | - middleware_attr = "MIDDLEWARE_CLASSES" |
| 112 | + old_getattr = settings.__getattr__ |
131 | 113 |
|
132 | | - # make sure to get an empty tuple when attr is None |
133 | | - middleware = list(getattr(settings, middleware_attr, ()) or ()) |
134 | | - conflicts = set(CONFLICTING_MIDDLEWARE).intersection(set(middleware)) |
135 | | - if conflicts: |
136 | | - raise RuntimeError("Other sentry-middleware already registered: %s" % conflicts) |
137 | | - |
138 | | - setattr(settings, middleware_attr, [MIDDLEWARE_NAME] + middleware) |
139 | | - |
140 | | - signals.request_finished.connect(_request_finished) |
141 | | - signals.got_request_exception.connect(_got_request_exception) |
| 114 | + # When `sentry_sdk.init` is called, the settings object may or may not be |
| 115 | + # initialized. So we need to lazily merge the array values. |
142 | 116 |
|
| 117 | + def sentry_patched_getattr(self, key): |
| 118 | + if key in ("MIDDLEWARE", "MIDDLEWARE_CLASSES"): |
| 119 | + try: |
| 120 | + rv = old_getattr(key) |
| 121 | + except AttributeError: |
| 122 | + rv = [] |
143 | 123 |
|
144 | | -class DjangoIntegration(Integration): |
145 | | - identifier = "django" |
146 | 124 |
|
147 | | - def install(self, client): |
148 | | - _install_impl() |
| 125 | + rv = type(rv)([MIDDLEWARE_NAME]) + rv |
| 126 | + elif key == "INSTALLED_APPS": |
| 127 | + try: |
| 128 | + rv = old_getattr(key) |
| 129 | + except AttributeError: |
| 130 | + rv = [] |
149 | 131 |
|
| 132 | + rv = type(rv)([__name__]) + rv |
| 133 | + else: |
| 134 | + rv = old_getattr(key) |
150 | 135 |
|
151 | | -try: |
152 | | - # Django >= 1.7 |
153 | | - from django.apps import AppConfig |
154 | | -except ImportError: |
155 | | - _install() |
156 | | -else: |
| 136 | + # fix cache set by old_getattr |
| 137 | + if key in self.__dict__: |
| 138 | + self.__dict__[key] = rv |
| 139 | + return rv |
157 | 140 |
|
158 | | - class SentryConfig(AppConfig): |
159 | | - name = "sentry_sdk.integrations.django" |
160 | | - label = "sentry_sdk_integrations_django" |
161 | | - verbose_name = "Sentry" |
162 | 141 |
|
163 | | - def ready(self): |
164 | | - _install() |
| 142 | + type(settings).__getattr__ = sentry_patched_getattr |
165 | 143 |
|
166 | | - default_app_config = "sentry_sdk.integrations.django.SentryConfig" |
| 144 | + signals.request_finished.connect(_request_finished) |
| 145 | + signals.got_request_exception.connect(_got_request_exception) |
0 commit comments