Skip to content

Commit

Permalink
ARCHBOM-1490: rename "custom metric" to "custom attribute" (#59)
Browse files Browse the repository at this point in the history
Rename "custom metric" to "custom attribute" throughout
the monitoring library. This decision can be read about
in the ADR 0002-custom-monitoring-language.rst. The
following have been deprecated:

* set_custom_metric (use set_custom_attribute)
* set_custom_metric_for_course_key (use
  set_custom_attribute_for_course_key)
* MonitoringCustomMetricsMiddleware (use
  CachedCustomMonitoringMiddleware)
* CachedCustomMonitoringMiddleware.accumulate_metric (use
  CachedCustomMonitoringMiddleware.accumulate_attribute)
* CodeOwnerMetricMiddleware (use
  CodeOwnerMonitoringMiddleware)

ARCHBOM-1490
  • Loading branch information
robrap authored Sep 2, 2020
1 parent 3110a05 commit 2ebb3a6
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 226 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ Change Log
Unreleased
~~~~~~~~~~

[3.8.0] - 2020-08-31
~~~~~~~~~~~~~~~~~~~~

Updated
_______

* Renamed "custom metric" to "custom attribute" throughout the monitoring library. This decision can be read about in the ADR 0002-custom-monitoring-language.rst. The following have been deprecated:

* set_custom_metric (use set_custom_attribute)
* set_custom_metrics_for_course_key (use set_custom_attributes_for_course_key)
* MonitoringCustomMetricsMiddleware (use CachedCustomMonitoringMiddleware)
* CachedCustomMonitoringMiddleware.accumulate_metric (use CachedCustomMonitoringMiddleware.accumulate_attribute)

* This wasn't meant to be used publicly, but was deprecated just in case.

* CodeOwnerMetricMiddleware (use CodeOwnerMonitoringMiddleware)

[3.7.4] - 2020-08-29
~~~~~~~~~~~~~~~~~~~~

Expand All @@ -23,10 +40,14 @@ Unreleased
[3.7.3] - 2020-08-12
~~~~~~~~~~~~~~~~~~~~

Updated
_______

* Upgrade psutil to latest version

[3.7.2] - 2020-08-10
~~~~~~~~~~~~~~~~~~~~

Updated
_______

Expand Down
2 changes: 1 addition & 1 deletion edx_django_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
EdX utilities for Django Application development..
"""

__version__ = "3.7.4"
__version__ = "3.8.0"

default_app_config = (
"edx_django_utils.apps.EdxDjangoUtilsConfig"
Expand Down
6 changes: 3 additions & 3 deletions edx_django_utils/monitoring/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Here is how you add the middleware:
# Generate code ownership metrics. Keep this immediately after RequestCacheMiddleware.
...
# Monitoring middleware must come after RequestCacheMiddleware
'edx_django_utils.monitoring.code_owner.middleware.CodeOwnerMetricMiddleware',
'edx_django_utils.monitoring.middleware.MonitoringCustomMetricsMiddleware',
'edx_django_utils.monitoring.code_owner.middleware.CodeOwnerMonitoringMiddleware',
'edx_django_utils.monitoring.middleware.CachedCustomMonitoringMiddleware',
'edx_django_utils.monitoring.middleware.MonitoringMemoryMiddleware',
)
Expand All @@ -35,4 +35,4 @@ In addition to adding the MonitoringMemoryMiddleware, you will need to enable a
Code Owner Custom Metric
------------------------

See docstrings for ``CodeOwnerMetricMiddleware`` for configuring the ``code_owner`` custom metric for your IDA.
See docstrings for ``CodeOwnerMonitoringMiddleware`` for configuring the ``code_owner`` custom attribute for your IDA.
10 changes: 9 additions & 1 deletion edx_django_utils/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
See README.rst for details.
"""
from .transactions import function_trace, get_current_transaction, ignore_transaction, set_monitoring_transaction_name
from .utils import accumulate, increment, set_custom_metric, set_custom_metrics_for_course_key
# "set_custom_metric*" methods are deprecated
from .utils import (
accumulate,
increment,
set_custom_attribute,
set_custom_attributes_for_course_key,
set_custom_metric,
set_custom_metrics_for_course_key
)
75 changes: 43 additions & 32 deletions edx_django_utils/monitoring/code_owner/middleware.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
"""
Middleware for code_owner custom metric
Middleware for code_owner custom attribute
"""
import logging
import warnings

from django.urls import resolve

from edx_django_utils.monitoring import get_current_transaction, set_custom_metric
from edx_django_utils.monitoring import get_current_transaction, set_custom_attribute

from .utils import get_code_owner_from_module, is_code_owner_mappings_configured

log = logging.getLogger(__name__)


class CodeOwnerMetricMiddleware:
class CodeOwnerMonitoringMiddleware:
"""
Django middleware object to set custom metrics for the owner of each view.
Django middleware object to set custom attributes for the owner of each view.
For instructions on usage, see:
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
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
Custom metrics set:
Custom attributes set:
- code_owner: The owning team mapped to the current view.
- code_owner_mapping_error: If there are any errors when trying to perform the mapping.
- code_owner_path_error: The error mapping by path, if code_owner isn't found in other ways.
Expand All @@ -35,58 +36,58 @@ def __init__(self, get_response):

def __call__(self, request):
response = self.get_response(request)
self._set_code_owner_metric(request)
self._set_code_owner_attribute(request)
return response

def process_exception(self, request, exception):
self._set_code_owner_metric(request)
self._set_code_owner_attribute(request)

def _set_code_owner_metric(self, request):
def _set_code_owner_attribute(self, request):
"""
Sets the code_owner custom metric, as well as several supporting custom metrics.
Sets the code_owner custom attribute, as well as several supporting custom attributes.
See CodeOwnerMetricMiddleware docstring for a complete list of metrics.
See CodeOwnerMonitoringMiddleware docstring for a complete list of attributes.
"""
code_owner, path_error = self._set_code_owner_metric_from_path(request)
code_owner, path_error = self._set_code_owner_attribute_from_path(request)
if code_owner:
set_custom_metric('code_owner', code_owner)
set_custom_attribute('code_owner', code_owner)
return
if not path_error:
# module found, but mapping wasn't configured
code_owner = self._set_code_owner_metric_catch_all()
code_owner = self._set_code_owner_attribute_catch_all()
if code_owner:
set_custom_metric('code_owner', code_owner)
set_custom_attribute('code_owner', code_owner)
return

code_owner, transaction_error = self._set_code_owner_metric_from_current_transaction(request)
code_owner, transaction_error = self._set_code_owner_attribute_from_current_transaction(request)
if code_owner:
set_custom_metric('code_owner', code_owner)
set_custom_attribute('code_owner', code_owner)
return
if not transaction_error:
# transaction name found, but mapping wasn't configured
code_owner = self._set_code_owner_metric_catch_all()
code_owner = self._set_code_owner_attribute_catch_all()
if code_owner:
set_custom_metric('code_owner', code_owner)
set_custom_attribute('code_owner', code_owner)
return

code_owner = self._set_code_owner_metric_catch_all()
code_owner = self._set_code_owner_attribute_catch_all()
if code_owner:
set_custom_metric('code_owner', code_owner)
set_custom_attribute('code_owner', code_owner)
return

# only report errors if code_owner couldn't be found, including catch-all
if path_error:
set_custom_metric('code_owner_path_error', path_error)
set_custom_attribute('code_owner_path_error', path_error)
if transaction_error:
set_custom_metric('code_owner_transaction_error', transaction_error)
set_custom_attribute('code_owner_transaction_error', transaction_error)

def _set_code_owner_metric_from_path(self, request):
def _set_code_owner_attribute_from_path(self, request):
"""
Uses the request path to find the view_func and then sets code owner metrics based on the view.
Uses the request path to find the view_func and then sets code owner attributes based on the view.
Side-effects:
Sets code_owner_path_module custom metric, used to determine code_owner
Sets code_owner_path_module custom attribute, used to determine code_owner
Returns:
(str, str): (code_owner, error_message), where at least one of these should be None
Expand All @@ -98,18 +99,18 @@ def _set_code_owner_metric_from_path(self, request):
try:
view_func, _, _ = resolve(request.path)
path_module = view_func.__module__
set_custom_metric('code_owner_path_module', path_module)
set_custom_attribute('code_owner_path_module', path_module)
code_owner = get_code_owner_from_module(path_module)
return code_owner, None
except Exception as e: # pylint: disable=broad-except
return None, str(e)

def _set_code_owner_metric_from_current_transaction(self, request):
def _set_code_owner_attribute_from_current_transaction(self, request):
"""
Uses the current transaction name to set the code owner metric.
Uses the current transaction name to set the code owner attribute.
Side-effects:
Sets code_owner_transaction_name custom metric, used to determine code_owner
Sets code_owner_transaction_name custom attribute, used to determine code_owner
Returns:
(str, str): (code_owner, error_message), where at least one of these should be None
Expand All @@ -123,14 +124,14 @@ def _set_code_owner_metric_from_current_transaction(self, request):
transaction_name = get_current_transaction().name
if not transaction_name:
return None, 'No current transaction name found.'
set_custom_metric('code_owner_transaction_name', transaction_name)
set_custom_attribute('code_owner_transaction_name', transaction_name)
module_name = transaction_name.split(':')[0]
code_owner = get_code_owner_from_module(module_name)
return code_owner, None
except Exception as e: # pylint: disable=broad-except
return None, str(e)

def _set_code_owner_metric_catch_all(self):
def _set_code_owner_attribute_catch_all(self):
"""
If the catch-all module "*" is configured, return the code_owner.
Expand All @@ -146,3 +147,13 @@ def _set_code_owner_metric_catch_all(self):
return code_owner
except Exception: # pylint: disable=broad-except; #pragma: no cover
return None


class CodeOwnerMetricMiddleware(CodeOwnerMonitoringMiddleware):
"""
Deprecated class for handling middleware. Class has been renamed to CodeOwnerMonitoringMiddleware.
"""
def __init__(self, *args, **kwargs): # pragma: no cover
super(CodeOwnerMetricMiddleware, self).__init__(*args, **kwargs)
msg = "Use 'CodeOwnerMonitoringMiddleware' in place of 'CodeOwnerMetricMiddleware'."
warnings.warn(msg, DeprecationWarning)
Loading

0 comments on commit 2ebb3a6

Please sign in to comment.