Global error reporting framework for Django, Sentry, and DataDog
- Python >= 3.6
- django >= 2.2.9
- sentrysdk >= 1.5.4
- ddtrace >=0.59.0
- django-datadog-logger >= 0.5.0
Install django-error-reporting
:
pip install django-error-reporting
Add it to your INSTALLED_APPS
:
INSTALLED_APPS = (
...
"django_error_reporting.apps.DjangoErrorReportingConfig",
...
)
Add this to your MIDDLEWARE
:
MIDDLEWARE = (
...
"django_error_reporting.middleware.ErrorReportingMiddleware"
)
Add this to your INSTALLED_APPS
:
INSTALLED_APPS = (
...
"ddtrace.contrib.django",
)
Add this to your MIDDLEWARE
:
MIDDLEWARE = (
...
"django_error_reporting.middleware.DataDogExceptionMiddleware",
)
If you plan on implementing log ingestion, you'll also have to do the following:
Add this to your INSTALLED_APPS
:
INSTALLED_APPS = (
...
"django_datadog_logger"
)
And this to your MIDDLEWARE
:
MIDDLEWARE = (
...
"django_datadog_logger.middleware.error_log.ErrorLoggingMiddleware",
"django_datadog_logger.middleware.request_log.RequestLoggingMiddleware",
)
Tuple of enabled integrations.
Default: ("datadog", "sentry")
Indicates whether event tags related to the request should be added.
These will include:
http.headers.ip_address
- remote IP address (seeDER_HEADERS_IP_ADDRESS
)
If DER_INCLUDE_REQUEST_TAGS
is enabled, this defines the IP address header. This needs to correspond to a HTTP header in the format accepted by request.META
.
Defaults to REMOTE_ADDR
.
A dictionary of headers to convert to event tags in the format of {<tag name>: <META name>}
where <META name>
is the key acccepted by request.META
.
In general, it would be preferable to set these tags via ddtrace's DD_TRACE_HEADER_TAGS
environment variable.
Defaults to None
.
Your Sentry DSN.
Default: None
List of Sentry integrations.
Default: []
Float for sample rate.
Default: 0.0
Environment name for Sentry.
Default: local
Request bodies setting.
Default: always
Release.
Default: "0"
.
Indicates if Sentry should use debug mode.
Defaults to settings.DEBUG
.
Minimum logging level for logs output by DER.
Defaults to ERROR
.
Indicates whether to setup DataDog logging using django-datadog-logger
.
Minimum logging level for logs output for DataDog.
Defaults to DER_LOGGING_LEVEL
.
Indicates whether to send all DataDog logs to console.
Defaults to False
Log file for DataDog. This must correspond to
Defaults to None
which disables file logging.
Minimum logging level for logs output for Sentry.
Defaults to DER_LOGGING_LEVEL
.
When the app is loaded (i.e., ready()
is called), it will do the following:
- Load default settings into the project settings
- If DataDog integration is enabled:
- Verify installed apps and middleware were added and throw
NotImplementedError
for missing.- If DataDog logging is enabled:
- Verify installed apps and middleware were added and throw
NotImplementedError
for missing. - Set
LOGGING
with formatters, handlers, and loggers.
- Verify installed apps and middleware were added and throw
- If DataDog logging is enabled:
- Verify installed apps and middleware were added and throw
- If Sentry integration is enabled:
- Verify
DER_SENTRY_DSN
is set and throwNotImplementedError
if missing. - Initialize Sentry.
- Verify
This middleware adds event tags (using add_event_tag
) for each request.
It also adds a trace_id
to the session which is a unique identifier for a request. If using AWS load balancers, it will use the trace ID from it; otherwise, uuid.uuid4()
is used.
To add app-specific tags, you can set a callback with ERROR_REPORTING_TAGGING_CALLBACK
which should accept a Request
instance and the add_event_tag
function as arguments.
When an exception is captured, this middleware will set the appropriate span tags on the root span of the trace.
This middleware should be added as late as possible.
For each implementation, there is a module in contrib
and it must contain a setup()
function which will be called during ready()
to initialize and perform start-up tasks.
Adds an event tag to the data sent to Sentry and/or DataDog.
If settings.DEBUG
is True
, print msg
.