11"""
2- Middleware for code_owner custom metric
2+ Middleware for code_owner custom attribute
33"""
44import logging
5+ import warnings
56
67from django .urls import resolve
78
8- from edx_django_utils .monitoring import get_current_transaction , set_custom_metric
9+ from edx_django_utils .monitoring import get_current_transaction , set_custom_attribute
910
1011from .utils import get_code_owner_from_module , is_code_owner_mappings_configured
1112
1213log = logging .getLogger (__name__ )
1314
1415
15- class CodeOwnerMetricMiddleware :
16+ class CodeOwnerMonitoringMiddleware :
1617 """
17- Django middleware object to set custom metrics for the owner of each view.
18+ Django middleware object to set custom attributes for the owner of each view.
1819
1920 For instructions on usage, see:
20- https://github.com/edx/edx-django-utils/blob/master/edx_django_utils/monitoring/docs/how_tos/add_code_owner_custom_metric_to_an_ida .rst
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
2122
22- Custom metrics set:
23+ Custom attributes set:
2324 - code_owner: The owning team mapped to the current view.
2425 - code_owner_mapping_error: If there are any errors when trying to perform the mapping.
2526 - code_owner_path_error: The error mapping by path, if code_owner isn't found in other ways.
@@ -35,58 +36,58 @@ def __init__(self, get_response):
3536
3637 def __call__ (self , request ):
3738 response = self .get_response (request )
38- self ._set_code_owner_metric (request )
39+ self ._set_code_owner_attribute (request )
3940 return response
4041
4142 def process_exception (self , request , exception ):
42- self ._set_code_owner_metric (request )
43+ self ._set_code_owner_attribute (request )
4344
44- def _set_code_owner_metric (self , request ):
45+ def _set_code_owner_attribute (self , request ):
4546 """
46- Sets the code_owner custom metric , as well as several supporting custom metrics .
47+ Sets the code_owner custom attribute , as well as several supporting custom attributes .
4748
48- See CodeOwnerMetricMiddleware docstring for a complete list of metrics .
49+ See CodeOwnerMonitoringMiddleware docstring for a complete list of attributes .
4950
5051 """
51- code_owner , path_error = self ._set_code_owner_metric_from_path (request )
52+ code_owner , path_error = self ._set_code_owner_attribute_from_path (request )
5253 if code_owner :
53- set_custom_metric ('code_owner' , code_owner )
54+ set_custom_attribute ('code_owner' , code_owner )
5455 return
5556 if not path_error :
5657 # module found, but mapping wasn't configured
57- code_owner = self ._set_code_owner_metric_catch_all ()
58+ code_owner = self ._set_code_owner_attribute_catch_all ()
5859 if code_owner :
59- set_custom_metric ('code_owner' , code_owner )
60+ set_custom_attribute ('code_owner' , code_owner )
6061 return
6162
62- code_owner , transaction_error = self ._set_code_owner_metric_from_current_transaction (request )
63+ code_owner , transaction_error = self ._set_code_owner_attribute_from_current_transaction (request )
6364 if code_owner :
64- set_custom_metric ('code_owner' , code_owner )
65+ set_custom_attribute ('code_owner' , code_owner )
6566 return
6667 if not transaction_error :
6768 # transaction name found, but mapping wasn't configured
68- code_owner = self ._set_code_owner_metric_catch_all ()
69+ code_owner = self ._set_code_owner_attribute_catch_all ()
6970 if code_owner :
70- set_custom_metric ('code_owner' , code_owner )
71+ set_custom_attribute ('code_owner' , code_owner )
7172 return
7273
73- code_owner = self ._set_code_owner_metric_catch_all ()
74+ code_owner = self ._set_code_owner_attribute_catch_all ()
7475 if code_owner :
75- set_custom_metric ('code_owner' , code_owner )
76+ set_custom_attribute ('code_owner' , code_owner )
7677 return
7778
7879 # only report errors if code_owner couldn't be found, including catch-all
7980 if path_error :
80- set_custom_metric ('code_owner_path_error' , path_error )
81+ set_custom_attribute ('code_owner_path_error' , path_error )
8182 if transaction_error :
82- set_custom_metric ('code_owner_transaction_error' , transaction_error )
83+ set_custom_attribute ('code_owner_transaction_error' , transaction_error )
8384
84- def _set_code_owner_metric_from_path (self , request ):
85+ def _set_code_owner_attribute_from_path (self , request ):
8586 """
86- Uses the request path to find the view_func and then sets code owner metrics based on the view.
87+ Uses the request path to find the view_func and then sets code owner attributes based on the view.
8788
8889 Side-effects:
89- Sets code_owner_path_module custom metric , used to determine code_owner
90+ Sets code_owner_path_module custom attribute , used to determine code_owner
9091
9192 Returns:
9293 (str, str): (code_owner, error_message), where at least one of these should be None
@@ -98,18 +99,18 @@ def _set_code_owner_metric_from_path(self, request):
9899 try :
99100 view_func , _ , _ = resolve (request .path )
100101 path_module = view_func .__module__
101- set_custom_metric ('code_owner_path_module' , path_module )
102+ set_custom_attribute ('code_owner_path_module' , path_module )
102103 code_owner = get_code_owner_from_module (path_module )
103104 return code_owner , None
104105 except Exception as e : # pylint: disable=broad-except
105106 return None , str (e )
106107
107- def _set_code_owner_metric_from_current_transaction (self , request ):
108+ def _set_code_owner_attribute_from_current_transaction (self , request ):
108109 """
109- Uses the current transaction name to set the code owner metric .
110+ Uses the current transaction name to set the code owner attribute .
110111
111112 Side-effects:
112- Sets code_owner_transaction_name custom metric , used to determine code_owner
113+ Sets code_owner_transaction_name custom attribute , used to determine code_owner
113114
114115 Returns:
115116 (str, str): (code_owner, error_message), where at least one of these should be None
@@ -123,14 +124,14 @@ def _set_code_owner_metric_from_current_transaction(self, request):
123124 transaction_name = get_current_transaction ().name
124125 if not transaction_name :
125126 return None , 'No current transaction name found.'
126- set_custom_metric ('code_owner_transaction_name' , transaction_name )
127+ set_custom_attribute ('code_owner_transaction_name' , transaction_name )
127128 module_name = transaction_name .split (':' )[0 ]
128129 code_owner = get_code_owner_from_module (module_name )
129130 return code_owner , None
130131 except Exception as e : # pylint: disable=broad-except
131132 return None , str (e )
132133
133- def _set_code_owner_metric_catch_all (self ):
134+ def _set_code_owner_attribute_catch_all (self ):
134135 """
135136 If the catch-all module "*" is configured, return the code_owner.
136137
@@ -146,3 +147,13 @@ def _set_code_owner_metric_catch_all(self):
146147 return code_owner
147148 except Exception : # pylint: disable=broad-except; #pragma: no cover
148149 return None
150+
151+
152+ class CodeOwnerMetricMiddleware (CodeOwnerMonitoringMiddleware ):
153+ """
154+ Deprecated class for handling middleware. Class has been renamed to CodeOwnerMonitoringMiddleware.
155+ """
156+ def __init__ (self , * args , ** kwargs ): # pragma: no cover
157+ super (CodeOwnerMetricMiddleware , self ).__init__ (* args , ** kwargs )
158+ msg = "Use 'CodeOwnerMonitoringMiddleware' in place of 'CodeOwnerMetricMiddleware'."
159+ warnings .warn (msg , DeprecationWarning )
0 commit comments