|
1 | 1 | """ |
2 | | -Middleware for code_owner custom attribute |
3 | | -""" |
4 | | -import logging |
5 | | -import warnings |
| 2 | +Deprecated Middleware for backward-compatibility. |
6 | 3 |
|
7 | | -from django.urls import resolve |
| 4 | +IMPORTANT: No new classes should be added to this file. |
| 5 | +TODO: Remove this file once these classes are no longer used. |
8 | 6 |
|
9 | | -from edx_django_utils.monitoring import get_current_transaction, set_custom_attribute |
10 | | - |
11 | | -from .utils import get_code_owner_from_module, is_code_owner_mappings_configured |
| 7 | +""" |
| 8 | +import warnings |
12 | 9 |
|
13 | | -log = logging.getLogger(__name__) |
| 10 | +from edx_django_utils.monitoring.internal.code_owner.middleware import \ |
| 11 | + CodeOwnerMonitoringMiddleware as InternalCodeOwnerMonitoringMiddleware |
| 12 | +from edx_django_utils.monitoring.internal.utils import set_custom_attribute |
14 | 13 |
|
15 | 14 |
|
16 | | -class CodeOwnerMonitoringMiddleware: |
| 15 | +class CodeOwnerMonitoringMiddleware(InternalCodeOwnerMonitoringMiddleware): |
17 | 16 | """ |
18 | | - Django middleware object to set custom attributes for the owner of each view. |
19 | | -
|
20 | | - For instructions on usage, see: |
21 | | - https://github.com/edx/edx-django-utils/blob/master/edx_django_utils/monitoring/docs/how_tos/add_code_owner_custom_attribute_to_an_ida.rst |
22 | | -
|
23 | | - Custom attributes set: |
24 | | - - code_owner: The owning team mapped to the current view. |
25 | | - - code_owner_mapping_error: If there are any errors when trying to perform the mapping. |
26 | | - - code_owner_path_error: The error mapping by path, if code_owner isn't found in other ways. |
27 | | - - code_owner_path_module: The __module__ of the view_func which was used to try to map to code_owner. |
28 | | - This can be used to find missing mappings. |
29 | | - - code_owner_transaction_error: The error mapping by transaction, if code_owner isn't found in other ways. |
30 | | - - code_owner_transaction_name: The current transaction name used to try to map to code_owner. |
31 | | - This can be used to find missing mappings. |
32 | | -
|
| 17 | + Deprecated class for handling middleware. Class has been moved to public API. |
33 | 18 | """ |
34 | | - def __init__(self, get_response): |
35 | | - self.get_response = get_response |
36 | | - |
37 | | - def __call__(self, request): |
38 | | - response = self.get_response(request) |
39 | | - self._set_code_owner_attribute(request) |
40 | | - return response |
41 | | - |
42 | | - def process_exception(self, request, exception): |
43 | | - self._set_code_owner_attribute(request) |
44 | | - |
45 | | - def _set_code_owner_attribute(self, request): |
46 | | - """ |
47 | | - Sets the code_owner custom attribute, as well as several supporting custom attributes. |
48 | | -
|
49 | | - See CodeOwnerMonitoringMiddleware docstring for a complete list of attributes. |
50 | | -
|
51 | | - """ |
52 | | - code_owner, path_error = self._set_code_owner_attribute_from_path(request) |
53 | | - if code_owner: |
54 | | - set_custom_attribute('code_owner', code_owner) |
55 | | - return |
56 | | - if not path_error: |
57 | | - # module found, but mapping wasn't configured |
58 | | - code_owner = self._set_code_owner_attribute_catch_all() |
59 | | - if code_owner: |
60 | | - set_custom_attribute('code_owner', code_owner) |
61 | | - return |
62 | | - |
63 | | - code_owner, transaction_error = self._set_code_owner_attribute_from_current_transaction(request) |
64 | | - if code_owner: |
65 | | - set_custom_attribute('code_owner', code_owner) |
66 | | - return |
67 | | - if not transaction_error: |
68 | | - # transaction name found, but mapping wasn't configured |
69 | | - code_owner = self._set_code_owner_attribute_catch_all() |
70 | | - if code_owner: |
71 | | - set_custom_attribute('code_owner', code_owner) |
72 | | - return |
73 | | - |
74 | | - code_owner = self._set_code_owner_attribute_catch_all() |
75 | | - if code_owner: |
76 | | - set_custom_attribute('code_owner', code_owner) |
77 | | - return |
78 | | - |
79 | | - # only report errors if code_owner couldn't be found, including catch-all |
80 | | - if path_error: |
81 | | - set_custom_attribute('code_owner_path_error', path_error) |
82 | | - if transaction_error: |
83 | | - set_custom_attribute('code_owner_transaction_error', transaction_error) |
84 | | - |
85 | | - def _set_code_owner_attribute_from_path(self, request): |
86 | | - """ |
87 | | - Uses the request path to find the view_func and then sets code owner attributes based on the view. |
88 | | -
|
89 | | - Side-effects: |
90 | | - Sets code_owner_path_module custom attribute, used to determine code_owner |
91 | | -
|
92 | | - Returns: |
93 | | - (str, str): (code_owner, error_message), where at least one of these should be None |
94 | | -
|
95 | | - """ |
96 | | - if not is_code_owner_mappings_configured(): |
97 | | - return None, None |
98 | | - |
99 | | - try: |
100 | | - view_func, _, _ = resolve(request.path) |
101 | | - path_module = view_func.__module__ |
102 | | - set_custom_attribute('code_owner_path_module', path_module) |
103 | | - code_owner = get_code_owner_from_module(path_module) |
104 | | - return code_owner, None |
105 | | - except Exception as e: # pylint: disable=broad-except |
106 | | - return None, str(e) |
107 | | - |
108 | | - def _set_code_owner_attribute_from_current_transaction(self, request): |
109 | | - """ |
110 | | - Uses the current transaction name to set the code owner attribute. |
111 | | -
|
112 | | - Side-effects: |
113 | | - Sets code_owner_transaction_name custom attribute, used to determine code_owner |
114 | | -
|
115 | | - Returns: |
116 | | - (str, str): (code_owner, error_message), where at least one of these should be None |
117 | | -
|
118 | | - """ |
119 | | - if not is_code_owner_mappings_configured(): |
120 | | - # ensure we don't set code ownership custom attributes if not configured to do so |
121 | | - return None, None # pragma: no cover |
122 | | - |
123 | | - try: |
124 | | - # Example: openedx.core.djangoapps.contentserver.middleware:StaticContentServer |
125 | | - transaction_name = get_current_transaction().name |
126 | | - if not transaction_name: |
127 | | - return None, 'No current transaction name found.' |
128 | | - module_name = transaction_name.split(':')[0] |
129 | | - set_custom_attribute('code_owner_transaction_name', transaction_name) |
130 | | - set_custom_attribute('code_owner_path_module', module_name) |
131 | | - code_owner = get_code_owner_from_module(module_name) |
132 | | - return code_owner, None |
133 | | - except Exception as e: # pylint: disable=broad-except |
134 | | - return None, str(e) |
135 | | - |
136 | | - def _set_code_owner_attribute_catch_all(self): |
137 | | - """ |
138 | | - If the catch-all module "*" is configured, return the code_owner. |
139 | | -
|
140 | | - Returns: |
141 | | - (str): code_owner or None if no catch-all configured. |
142 | | -
|
143 | | - """ |
144 | | - try: |
145 | | - code_owner = get_code_owner_from_module('*') |
146 | | - return code_owner |
147 | | - except Exception: # pylint: disable=broad-except; #pragma: no cover |
148 | | - return None |
| 19 | + def __init__(self, *args, **kwargs): |
| 20 | + super().__init__(*args, **kwargs) |
| 21 | + msg = "Use 'edx_django_utils.monitoring.CodeOwnerMonitoringMiddleware' in place of " \ |
| 22 | + "'edx_django_utils.monitoring.code_owner.middleware.CodeOwnerMonitoringMiddleware'." |
| 23 | + warnings.warn(msg, DeprecationWarning) |
| 24 | + set_custom_attribute('deprecated_code_owner_middleware', 'CodeOwnerMonitoringMiddleware') |
149 | 25 |
|
150 | 26 |
|
151 | | -class CodeOwnerMetricMiddleware(CodeOwnerMonitoringMiddleware): |
| 27 | +class CodeOwnerMetricMiddleware(InternalCodeOwnerMonitoringMiddleware): |
152 | 28 | """ |
153 | 29 | Deprecated class for handling middleware. Class has been renamed to CodeOwnerMonitoringMiddleware. |
154 | 30 | """ |
155 | 31 | def __init__(self, *args, **kwargs): # pragma: no cover |
156 | | - super(CodeOwnerMetricMiddleware, self).__init__(*args, **kwargs) |
157 | | - msg = "Use 'CodeOwnerMonitoringMiddleware' in place of 'CodeOwnerMetricMiddleware'." |
| 32 | + super().__init__(*args, **kwargs) |
| 33 | + msg = "Use 'edx_django_utils.monitoring.CodeOwnerMonitoringMiddleware' in place of " \ |
| 34 | + "'edx_django_utils.monitoring.code_owner.middleware.CodeOwnerMetricMiddleware'." |
158 | 35 | warnings.warn(msg, DeprecationWarning) |
| 36 | + set_custom_attribute('deprecated_code_owner_middleware', 'CodeOwnerMetricMiddleware') |
0 commit comments