Skip to content

Commit

Permalink
add record_exception
Browse files Browse the repository at this point in the history
Add a wrapper to New Relic's record_exception,
which can be used to monitor caught exceptions.

Co-authored-by: Feanil Patel <[email protected]>
  • Loading branch information
robrap and feanil committed Nov 18, 2020
1 parent 248c811 commit 1633baa
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ Change Log
Unreleased
~~~~~~~~~~

[3.12.1] - 2020-11-18
[3.13.0] - 2020-11-18
~~~~~~~~~~~~~~~~~~~~~

Added
_____

* Added record_exception to monitor caught exceptions.

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.12.1"
__version__ = "3.13.0"

default_app_config = (
"edx_django_utils.apps.EdxDjangoUtilsConfig"
Expand Down
2 changes: 2 additions & 0 deletions edx_django_utils/monitoring/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Monitoring Utils

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.

See ``__init__.py`` for a list of everything included in the public API.

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.

Using Custom Attributes
Expand Down
8 changes: 7 additions & 1 deletion edx_django_utils/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
ignore_transaction,
set_monitoring_transaction_name
)
from .internal.utils import accumulate, increment, set_custom_attribute, set_custom_attributes_for_course_key
from .internal.utils import (
accumulate,
increment,
record_exception,
set_custom_attribute,
set_custom_attributes_for_course_key
)
# "set_custom_metric*" methods are deprecated
from .utils import set_custom_metric, set_custom_metrics_for_course_key
18 changes: 17 additions & 1 deletion edx_django_utils/monitoring/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

try:
import newrelic.agent
except ImportError:
except ImportError: # pragma: no cover
newrelic = None # pylint: disable=invalid-name


Expand Down Expand Up @@ -80,3 +80,19 @@ def set_custom_attribute(key, value):
if newrelic: # pragma: no cover
# note: parameter is new relic's older name for attributes
newrelic.agent.add_custom_parameter(key, value)


def record_exception():
"""
Records a caught exception to the monitoring system.
Note: By default, only unhandled exceptions are monitored. This function
can be called to record exceptions as monitored errors, even if you handle
the exception gracefully from a user perspective.
For more details, see:
https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/recordexception-python-agent-api
"""
if newrelic: # pragma: no cover
newrelic.agent.record_exception()
13 changes: 12 additions & 1 deletion edx_django_utils/monitoring/tests/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from mock import call, patch

from edx_django_utils.cache import RequestCache
from edx_django_utils.monitoring import CachedCustomMonitoringMiddleware, accumulate, get_current_transaction, increment
from edx_django_utils.monitoring import (
CachedCustomMonitoringMiddleware,
accumulate,
get_current_transaction,
increment,
record_exception
)

from ..middleware import CachedCustomMonitoringMiddleware as DeprecatedCachedCustomMonitoringMiddleware
from ..middleware import MonitoringCustomMetricsMiddleware as DeprecatedMonitoringCustomMetricsMiddleware
Expand Down Expand Up @@ -137,3 +143,8 @@ def test_deprecated_set_custom_attribute(self, mock_set_custom_attribute):
def test_deprecated_set_custom_attributes_for_course_key(self, mock_set_custom_attributes_for_course_key):
deprecated_set_custom_attributes_for_course_key('key')
mock_set_custom_attributes_for_course_key.assert_called_with('key')

@patch('newrelic.agent.record_exception')
def test_record_exception(self, mock_record_exception):
record_exception()
mock_record_exception.assert_called_once()

0 comments on commit 1633baa

Please sign in to comment.