Syntax
newrelic.agent.notice_error(error=None, attributes={}, expected=None, ignore=None, status_code=None, application=None)
Records details of a Python exception as an error.
Description
By default, the Python agent only reports unhandled exceptions. Use notice_error
to record any Python exception as an error, which can then be found in the UI. If no parameters are provided, the details of the currently handled exception will be used.
You can record up to five distinct exceptions per transaction, and up to 20 total exceptions across all transactions per harvest cycle.
When notice_error()
is called within the context of a monitored web request or background task, the details of the exception will be reported against the application that the request or task is being reported to.
If called outside of the context of a monitored web request or background task, the call will be ignored unless the application
keyword argument is provided and an application object corresponding to the application against which the exception should be recorded is provided. A suitable application object can be obtained using the newrelic.agent.application()
function.
Parameters
팁
In almost all cases, notice_error
will require no parameters.
Parameter | Description |
---|---|
tuple | Optional and rarely used. A tuple containing exception information |
dict | Optional. Custom attributes to add to the error event (in addition to any custom attributes already added to the transaction). If high-security mode is enabled, this will not work. |
boolean, iterable[String], callable(exception_class, exception_instance, traceback)->boolean | Optional. Errors to mark as expected can be passed in as an iterable of strings in the form |
boolean, iterable[String], callable(exception_class, exception_instance, traceback)->boolean | Optional. Errors to ignore can be passed in as an iterable of strings in the form |
string, integer, callable(exception_class, exception_instance, traceback) | Optional. The exception status code. This value can be a string, integer, or a callable that takes in exception information |
application object | Optional. If called outside of the context of a monitored web request or background task, the call will be ignored unless the |
Return values
None.
Examples
Simple example of reporting exceptions
In a large majority of cases, you won't need to pass any parameters. You would just call the following where you want to report an exception:
newrelic.agent.notice_error()
Example using boolean
An example of notice_error
using a boolean value. This indicates that an error should be expected.
newrelic.agent.notice_error(expected=True)
Call with sys.exc_info() tuple and additional parameters
An example of notice_error
using sys.exc_info()
data:
def complex_ignore_errors(exc, val, tb): # do some logic here return False
newrelic.agent.notice_error(attributes={'my_special_exception': True}, ignore=complex_ignore_errors)
Example using callback
If you need to filter exceptions dynamically based on the attributes of a specific exception type, you can supply a callback function:
def _ignore_errors(exc, value, tb): if instance(value, HTTPError): if value.status == 404: return True
newrelic.agent.notice_error(ignore=_ignore_errors)
If the exception is to be ignored/expected, set the return value for the callable to True
. Return False
if the exception should never be ignored/ expected regardless of any other checks, and None
if subsequent checks and inbuilt rules should determine if the exception should be ignored/expected. A callback would normally return either True
or None
.