edx_django_utils.monitoring.internal package#

Subpackages#

Submodules#

edx_django_utils.monitoring.internal.middleware module#

Middleware for monitoring.

At this time, monitoring details can only be reported to New Relic.

class edx_django_utils.monitoring.internal.middleware.CachedCustomMonitoringMiddleware(get_response=None)#

Bases: django.utils.deprecation.MiddlewareMixin

Middleware batch reports cached custom attributes at the end of a request.

Make sure to add below the request cache in MIDDLEWARE.

This middleware will only call on the newrelic agent if there are any attributes to report for this request, so it will not incur any processing overhead for request handlers which do not record custom attributes.

Note: New Relic adds custom attributes to events, which is what is being used here.

classmethod accumulate_attribute(name, value)#

Accumulate a custom attribute (name and value) in the attributes cache.

classmethod accumulate_metric(name, value)#

Deprecated method replaced by accumulate_attribute.

process_exception(request, exception)#

Django middleware handler to process an exception

process_response(request, response)#

Django middleware handler to process a response

class edx_django_utils.monitoring.internal.middleware.CookieMonitoringMiddleware(get_response)#

Bases: object

Middleware for monitoring the size and growth of all our cookies, to see if we’re running into browser limits.

get_log_message_and_monitor_cookies(request)#

Add logging and custom attributes for monitoring cookie sizes.

For cookie size monitoring, we don’t log raw contents of cookies because that might cause a security issue—we just want to see if any cookies are growing out of control. However, there is also an option for encrypted logging of cookie data where there appears to be some kind of data corruption occurring.

Useful NRQL Queries:

# Always available SELECT * FROM Transaction WHERE cookies.header.size > 6000

Attributes that are added by this middleware:

For all requests:

cookies.header.size: The total size in bytes of the cookie header

If COOKIE_HEADER_SIZE_LOGGING_THRESHOLD is reached:

cookies.header.size.computed

Related Settings (see annotations for details):

  • COOKIE_HEADER_SIZE_LOGGING_THRESHOLD

  • COOKIE_SAMPLING_REQUEST_COUNT

  • UNUSUAL_COOKIE_HEADER_PUBLIC_KEY

  • UNUSUAL_COOKIE_HEADER_LOG_CHUNK

Returns: The message to be logged. This is returned, rather than directly

logged, so that it can be processed at request time (before any cookies may be changed server-side), but logged at response time, once the user id is available for authenticated calls.

Log all headers when corrupt cookies are detected (if settings permit).

This log data is encrypted using the log-sensitive utility.

  • Logging requires that UNUSUAL_COOKIE_HEADER_PUBLIC_KEY is set.

  • Output is split across multiple lines using UNUSUAL_COOKIE_HEADER_LOG_CHUNK.

class edx_django_utils.monitoring.internal.middleware.DeploymentMonitoringMiddleware(get_response)#

Bases: object

Middleware to record environment values at the time of deployment for each service.

static record_django_version()#

Record the installed Django version as custom attribute

static record_python_version()#

Record the Python version as custom attribute

class edx_django_utils.monitoring.internal.middleware.MonitoringMemoryMiddleware(get_response=None)#

Bases: django.utils.deprecation.MiddlewareMixin

Middleware for monitoring memory usage.

Make sure to add below the request cache in MIDDLEWARE.

guid_key = 'guid_key'#
memory_data_key = 'memory_data'#
process_request(request)#

Store memory data to log later.

process_response(request, response)#

Logs memory data after processing response.

edx_django_utils.monitoring.internal.middleware.split_ascii_log_message(msg, chunk_size)#

Generator that splits a message string into chunks of at most chunk_size characters.

Message must consist of single-byte (ASCII-range) characters, otherwise chunks will not reliably be capped at the chunk_size.

A small collation tag will be added to the end of each chunk, and it may not be possible to reliably predict the length of a log message’s prefix (in its final output format), so the chunk_size should be set considerably smaller than max log message size.

edx_django_utils.monitoring.internal.transactions module#

This is a collection of transaction-related monitoring utilities.

Usage:

from edx_django_utils.monitoring import ignore_transaction … # called from inside a view you want ignored ignore_transaction()

Please remember to expose any new methods in the __init__.py file.

class edx_django_utils.monitoring.internal.transactions.MonitoringTransaction(transaction)#

Bases: object

Represents a monitoring transaction (likely the current transaction).

property name#

The name of the transaction.

For NewRelic, the name may look like:

openedx.core.djangoapps.contentserver.middleware:StaticContentServer

edx_django_utils.monitoring.internal.transactions.function_trace(function_name)#

Wraps a chunk of code that we want to appear as a separate, explicit, segment in our monitoring tools.

edx_django_utils.monitoring.internal.transactions.get_current_transaction()#

Returns the current transaction.

edx_django_utils.monitoring.internal.transactions.ignore_transaction()#

Ignore the transaction in monitoring

This allows us to ignore code paths that are unhelpful to include, such as /health/ checks.

edx_django_utils.monitoring.internal.transactions.set_monitoring_transaction_name(name, group=None, priority=None)#

Sets the transaction name for monitoring.

This is not cached, and only support reporting to New Relic.

edx_django_utils.monitoring.internal.utils module#

Defines several custom monitoring helpers, some of which work with the CachedCustomMonitoringMiddleware.

Usage:

from edx_django_utils.monitoring import accumulate … accumulate(‘xb_user_state.get_many.num_items’, 4)

There is no need to do anything else. The custom attributes are automatically cleared before the next request.

We try to keep track of our custom monitoring at: https://openedx.atlassian.net/wiki/spaces/PERF/pages/54362736/Custom+Attributes+in+New+Relic

At this time, the custom monitoring will only be reported to New Relic.

edx_django_utils.monitoring.internal.utils.accumulate(name, value)#

Accumulate monitoring custom attribute for the current request.

The named attribute is accumulated by a numerical amount using the sum. All attributes are queued up in the request_cache for this request. At the end of the request, the monitoring_utils middleware will batch report all queued accumulated attributes to the monitoring tool (e.g. New Relic).

Parameters
  • name (str) – The attribute name. It should be period-delimited, and increase in specificity from left to right. For example: ‘xb_user_state.get_many.num_items’.

  • value (number) – The amount to accumulate into the named attribute. When accumulate() is called multiple times for a given attribute name during a request, the sum of the values for each call is reported for that attribute. For attributes which don’t make sense to accumulate, use set_custom_attribute instead.

edx_django_utils.monitoring.internal.utils.background_task(*args, **kwargs)#

Handles monitoring for background tasks that are not passed in through the web server like celery and event consuming tasks.

For more details, see: https://docs.newrelic.com/docs/apm/agents/python-agent/supported-features/monitor-non-web-scripts-worker-processes-tasks-functions

edx_django_utils.monitoring.internal.utils.increment(name)#

Increment a monitoring custom attribute representing a counter.

Here we simply accumulate a new custom attribute with a value of 1, and the middleware should automatically aggregate this attribute.

edx_django_utils.monitoring.internal.utils.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

edx_django_utils.monitoring.internal.utils.set_custom_attribute(key, value)#

Set monitoring custom attribute.

This is not cached, and only support reporting to New Relic Insights.

edx_django_utils.monitoring.internal.utils.set_custom_attributes_for_course_key(course_key)#

Set monitoring custom attributes related to a course key.

This is not cached, and only support reporting to New Relic Insights.

Module contents#