Skip to content

Commit a4af6a3

Browse files
authored
Merge pull request #76 from edx/robrap/update-deprecation-attributes
add record_exception and update deprecated custom attribute values
2 parents 45e0531 + 1633baa commit a4af6a3

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

CHANGELOG.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ Change Log
1515
Unreleased
1616
~~~~~~~~~~
1717

18+
[3.13.0] - 2020-11-18
19+
~~~~~~~~~~~~~~~~~~~~~
20+
21+
Added
22+
_____
23+
24+
* Added record_exception to monitor caught exceptions.
25+
26+
Updated
27+
_______
28+
29+
* Added additional details to the `deprecated_monitoring_utils` custom attribute values to make it simpler to track down usage.
30+
1831
[3.12.0] - 2020-11-17
1932
~~~~~~~~~~~~~~~~~~~~~
2033

edx_django_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
EdX utilities for Django Application development..
33
"""
44

5-
__version__ = "3.12.0"
5+
__version__ = "3.13.0"
66

77
default_app_config = (
88
"edx_django_utils.apps.EdxDjangoUtilsConfig"

edx_django_utils/monitoring/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Monitoring Utils
33

44
This is our primary abstraction from 3rd party monitoring libraries such as newrelic.agent. It includes middleware and utility methods for adding custom attributes and for better monitoring memory consumption.
55

6+
See ``__init__.py`` for a list of everything included in the public API.
7+
68
If, for some reason, you need low level access to the newrelic agent, please extend this library to implement the feature that you want. Applications should never include ``import newrelic.agent`` directly.
79

810
Using Custom Attributes

edx_django_utils/monitoring/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
ignore_transaction,
1717
set_monitoring_transaction_name
1818
)
19-
from .internal.utils import accumulate, increment, set_custom_attribute, set_custom_attributes_for_course_key
19+
from .internal.utils import (
20+
accumulate,
21+
increment,
22+
record_exception,
23+
set_custom_attribute,
24+
set_custom_attributes_for_course_key
25+
)
2026
# "set_custom_metric*" methods are deprecated
2127
from .utils import set_custom_metric, set_custom_metrics_for_course_key

edx_django_utils/monitoring/internal/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
try:
2323
import newrelic.agent
24-
except ImportError:
24+
except ImportError: # pragma: no cover
2525
newrelic = None # pylint: disable=invalid-name
2626

2727

@@ -80,3 +80,19 @@ def set_custom_attribute(key, value):
8080
if newrelic: # pragma: no cover
8181
# note: parameter is new relic's older name for attributes
8282
newrelic.agent.add_custom_parameter(key, value)
83+
84+
85+
def record_exception():
86+
"""
87+
Records a caught exception to the monitoring system.
88+
89+
Note: By default, only unhandled exceptions are monitored. This function
90+
can be called to record exceptions as monitored errors, even if you handle
91+
the exception gracefully from a user perspective.
92+
93+
For more details, see:
94+
https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/recordexception-python-agent-api
95+
96+
"""
97+
if newrelic: # pragma: no cover
98+
newrelic.agent.record_exception()

edx_django_utils/monitoring/tests/test_monitoring.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
from mock import call, patch
77

88
from edx_django_utils.cache import RequestCache
9-
from edx_django_utils.monitoring import CachedCustomMonitoringMiddleware, accumulate, get_current_transaction, increment
9+
from edx_django_utils.monitoring import (
10+
CachedCustomMonitoringMiddleware,
11+
accumulate,
12+
get_current_transaction,
13+
increment,
14+
record_exception
15+
)
1016

1117
from ..middleware import CachedCustomMonitoringMiddleware as DeprecatedCachedCustomMonitoringMiddleware
1218
from ..middleware import MonitoringCustomMetricsMiddleware as DeprecatedMonitoringCustomMetricsMiddleware
@@ -137,3 +143,8 @@ def test_deprecated_set_custom_attribute(self, mock_set_custom_attribute):
137143
def test_deprecated_set_custom_attributes_for_course_key(self, mock_set_custom_attributes_for_course_key):
138144
deprecated_set_custom_attributes_for_course_key('key')
139145
mock_set_custom_attributes_for_course_key.assert_called_with('key')
146+
147+
@patch('newrelic.agent.record_exception')
148+
def test_record_exception(self, mock_record_exception):
149+
record_exception()
150+
mock_record_exception.assert_called_once()

edx_django_utils/monitoring/utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def accumulate(name, value):
1919
"""
2020
msg = "Use 'monitoring.accumulate' in place of 'monitoring.utils.accumulate'."
2121
warnings.warn(msg, DeprecationWarning)
22-
internal_set_custom_attribute('deprecated_monitoring_utils', 'accumulate')
22+
internal_set_custom_attribute('deprecated_monitoring_utils', 'accumulate[{}]'.format(name))
2323
internal_accumulate(name, value)
2424

2525

@@ -29,7 +29,7 @@ def increment(name):
2929
"""
3030
msg = "Use 'monitoring.increment' in place of 'monitoring.utils.increment'."
3131
warnings.warn(msg, DeprecationWarning)
32-
internal_set_custom_attribute('deprecated_monitoring_utils', 'increment')
32+
internal_set_custom_attribute('deprecated_monitoring_utils', 'increment[{}]'.format(name))
3333
internal_increment(name)
3434

3535

@@ -39,7 +39,7 @@ def set_custom_attribute(key, value):
3939
"""
4040
msg = "Use 'monitoring.set_custom_attribute' in place of 'monitoring.utils.set_custom_attribute'."
4141
warnings.warn(msg, DeprecationWarning)
42-
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_attribute')
42+
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_attribute[{}]'.format(key))
4343
internal_set_custom_attribute(key, value)
4444

4545

@@ -50,7 +50,10 @@ def set_custom_attributes_for_course_key(course_key):
5050
msg = "Use 'monitoring.set_custom_attributes_for_course_key' in place of " \
5151
"'monitoring.utils.set_custom_attributes_for_course_key'."
5252
warnings.warn(msg, DeprecationWarning)
53-
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_attributes_for_course_key')
53+
internal_set_custom_attribute(
54+
'deprecated_monitoring_utils',
55+
'set_custom_attributes_for_course_key[{}]'.format(str(course_key))
56+
)
5457
internal_set_custom_attributes_for_course_key(course_key)
5558

5659

@@ -60,7 +63,7 @@ def set_custom_metric(key, value): # pragma: no cover
6063
"""
6164
msg = "Use 'set_custom_attribute' in place of 'set_custom_metric'."
6265
warnings.warn(msg, DeprecationWarning)
63-
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_metric')
66+
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_metric[{}]'.format(key))
6467
internal_set_custom_attribute(key, value)
6568

6669

@@ -70,5 +73,8 @@ def set_custom_metrics_for_course_key(course_key): # pragma: no cover
7073
"""
7174
msg = "Use 'set_custom_attributes_for_course_key' in place of 'set_custom_metrics_for_course_key'."
7275
warnings.warn(msg, DeprecationWarning)
73-
internal_set_custom_attribute('deprecated_monitoring_utils', 'set_custom_metrics_for_course_key')
76+
internal_set_custom_attribute(
77+
'deprecated_monitoring_utils',
78+
'set_custom_metrics_for_course_key[{}]'.format(str(course_key))
79+
)
7480
internal_set_custom_attributes_for_course_key(course_key)

0 commit comments

Comments
 (0)