This is our primary abstraction from 3rd party monitoring libraries such as New Relic, Datadog, and OpenTelemetry. 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 a monitoring library, please extend this library to implement the feature that you want. Applications should never include import newrelic.agent (or similar) directly.
The most complete feature support is for New Relic (the default), but there is also relatively complete support for Datadog and preliminary support for OpenTelemetry. Private or alternative implementations can also be used.
The Django setting OPENEDX_TELEMETRY can be set to a list of implementations, e.g. ['edx_django_utils.monitoring.NewRelicBackend', 'edx_django_utils.monitoring.OpenTelemetryBackend']. All of the implementations that can be loaded will be used for all applicable telemetry calls.
Feature support matrix for built-in telemetry backends:
| New Relic | OpenTelemetry | Datadog | |
|---|---|---|---|
Custom span attributes (set_custom_attribute, accumulate, increment, etc.) |
✅ (on root span) | ✅ (on current span) | ✅ (on root span) |
Create a new span (function_trace) |
✅ | ❌ | ✅ |
Set local root span name (set_monitoring_transaction_name) |
✅ | ❌ | ✅ |
Manipulate spans (ignore_transaction) |
✅ | ❌ | ❌ |
Record exceptions (record_exception) |
✅ | ✅ | ✅ |
Additional requirements for using these backends:
edx_django_utils.monitoring.NewRelicBackend:- Install the
newrelicPython package - Initialize newrelic, either via the
newrelic-admin run-programwrapper ornewrelic.agentAPI calls during server startup
- Install the
edx_django_utils.monitoring.OpenTelemetryBackend:- Install the
opentelemetry-apiPython package - Initialize opentelemetry, either via the
opentelemetry-instrumentwrapper oropentelemetry.instrumentationAPI calls during server startup
- Install the
edx_django_utils.monitoring.DatadogBackend:- Install the
ddtracePython package - Initialize ddtrace, either via the
ddtrace-runwrapper orddtrace.autoAPI calls during server startup
- Install the
For help writing and using custom attributes, see docs/how_tos/using_custom_attributes.rst.
Here is how you add the middleware:
MIDDLEWARE = (
'edx_django_utils.cache.middleware.RequestCacheMiddleware',
# Add monitoring middleware immediately after RequestCacheMiddleware
'edx_django_utils.monitoring.MonitoringSupportMiddleware',
'edx_django_utils.monitoring.DeploymentMonitoringMiddleware',
'edx_django_utils.monitoring.CookieMonitoringMiddleware',
'edx_django_utils.monitoring.CodeOwnerMonitoringMiddleware',
'edx_django_utils.monitoring.FrontendMonitoringMiddleware',
'edx_django_utils.monitoring.MonitoringMemoryMiddleware',
)
The middleware MonitoringSupportMiddleware provides a number of monitoring capabilities:
- It enables plugging in outside monitoring capabilities, by sending the signals
monitoring_support_process_request,monitoring_support_process_response, andmonitoring_support_process_exception. These can be useful because this middleware should be available in all Open edX services, and should appear early enough in the list of middleware to monitor most requests, even those that respond early from another middleware. - It allows certain utility methods, like
accumulateandincrement, to work appropriately. - It adds error span tags to the root span.
In order to use the monitoring signals, import them as follows:
from edx_django_utils.monitoring.signals import monitoring_support_process_response
See docstring for CodeOwnerMonitoringMiddleware for configuring the code_owner custom attribute for your IDA.
See docstring for configuring CookieMonitoringMiddleware to monitor cookie header size.
Also see monitoring/scripts/process_cookie_monitoring_logs.py for processing log messages.
Simply add DeploymentMonitoringMiddleware to monitor the python and django version of each request. See docstring for details.
This middleware FrontendMonitoringMiddleware inserts frontend monitoring related HTML script tags to the response, see docstring for details.
In addition to adding the FrontendMonitoringMiddleware, you will need to enable a waffle switch edx_django_utils.monitoring.enable_frontend_monitoring_middleware to enable the frontend monitoring.
In addition to adding the MonitoringMemoryMiddleware, you will need to enable a waffle switch edx_django_utils.monitoring.enable_memory_middleware to enable the additional monitoring.