|
1 | 1 | from __future__ import absolute_import |
2 | 2 |
|
3 | | -from threading import Lock |
4 | | - |
5 | 3 | from django.core import signals |
6 | 4 |
|
7 | 5 | try: |
|
12 | 10 | from sentry_sdk import get_current_hub, configure_scope, capture_exception |
13 | 11 | from sentry_sdk.hub import _internal_exceptions |
14 | 12 | from ._wsgi import RequestExtractor |
| 13 | +from . import Integration |
| 14 | + |
| 15 | + |
| 16 | +class DjangoIntegration(Integration): |
| 17 | + identifier = 'django' |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + pass |
| 21 | + |
| 22 | + def install(self, client): |
| 23 | + from django.core.handlers.base import BaseHandler |
| 24 | + |
| 25 | + make_event_processor = self._make_event_processor |
| 26 | + |
| 27 | + old_get_response = BaseHandler.get_response |
| 28 | + |
| 29 | + def sentry_patched_get_response(self, request): |
| 30 | + with get_current_hub().push_scope(): |
| 31 | + get_current_hub().add_event_processor( |
| 32 | + lambda: make_event_processor(request) |
| 33 | + ) |
| 34 | + |
| 35 | + with configure_scope() as scope: |
| 36 | + scope.transaction = resolve(request.path).func.__name__ |
| 37 | + |
| 38 | + return old_get_response(self, request) |
| 39 | + |
| 40 | + BaseHandler.get_response = sentry_patched_get_response |
| 41 | + |
| 42 | + signals.got_request_exception.connect(_got_request_exception) |
| 43 | + |
| 44 | + def _make_event_processor(self, request): |
| 45 | + def processor(event): |
| 46 | + with _internal_exceptions(): |
| 47 | + DjangoRequestExtractor(request).extract_into_event(event) |
| 48 | + |
| 49 | + # TODO: user info |
| 50 | + |
| 51 | + return processor |
| 52 | + |
| 53 | + |
| 54 | +def _got_request_exception(request=None, **kwargs): |
| 55 | + capture_exception() |
| 56 | + |
15 | 57 |
|
16 | 58 |
|
17 | 59 | class DjangoRequestExtractor(RequestExtractor): |
@@ -41,52 +83,3 @@ def files(self): |
41 | 83 |
|
42 | 84 | def size_of_file(self, file): |
43 | 85 | return file.size |
44 | | - |
45 | | - |
46 | | -_installer_lock = Lock() |
47 | | -_installed = False |
48 | | - |
49 | | - |
50 | | -def install(client): |
51 | | - global _installed |
52 | | - with _installer_lock: |
53 | | - if _installed: |
54 | | - return |
55 | | - |
56 | | - _install_impl() |
57 | | - _installed = True |
58 | | - |
59 | | - |
60 | | -def _install_impl(): |
61 | | - from django.core.handlers.base import BaseHandler |
62 | | - |
63 | | - old_get_response = BaseHandler.get_response |
64 | | - |
65 | | - def sentry_patched_get_response(self, request): |
66 | | - with get_current_hub().push_scope(): |
67 | | - get_current_hub().add_event_processor( |
68 | | - lambda: _make_event_processor(request) |
69 | | - ) |
70 | | - |
71 | | - with configure_scope() as scope: |
72 | | - scope.transaction = resolve(request.path).func.__name__ |
73 | | - |
74 | | - return old_get_response(self, request) |
75 | | - |
76 | | - BaseHandler.get_response = sentry_patched_get_response |
77 | | - |
78 | | - signals.got_request_exception.connect(_got_request_exception) |
79 | | - |
80 | | - |
81 | | -def _make_event_processor(request): |
82 | | - def processor(event): |
83 | | - with _internal_exceptions(): |
84 | | - DjangoRequestExtractor(request).extract_into_event(event) |
85 | | - |
86 | | - # TODO: user info |
87 | | - |
88 | | - return processor |
89 | | - |
90 | | - |
91 | | -def _got_request_exception(request=None, **kwargs): |
92 | | - capture_exception() |
0 commit comments