diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1ad4c890..a55fce30 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,12 @@ Change Log .. There should always be an "Unreleased" section for changes pending release. +7.2.0 - 2025-02-18 +------------------ +Added +~~~~~ +* Added ``monitor_django_management_command`` to enable monitoring of Django management commands. + 7.1.0 - 2024-12-05 ------------------ Added diff --git a/edx_django_utils/__init__.py b/edx_django_utils/__init__.py index a2e76089..536a76ff 100644 --- a/edx_django_utils/__init__.py +++ b/edx_django_utils/__init__.py @@ -2,7 +2,7 @@ EdX utilities for Django Application development.. """ -__version__ = "7.1.0" +__version__ = "7.2.0" default_app_config = ( "edx_django_utils.apps.EdxDjangoUtilsConfig" diff --git a/edx_django_utils/monitoring/__init__.py b/edx_django_utils/monitoring/__init__.py index 7c199d89..5f5fb19a 100644 --- a/edx_django_utils/monitoring/__init__.py +++ b/edx_django_utils/monitoring/__init__.py @@ -24,6 +24,7 @@ accumulate, function_trace, increment, + monitor_django_management_command, record_exception, set_custom_attribute, set_custom_attributes_for_course_key, diff --git a/edx_django_utils/monitoring/internal/utils.py b/edx_django_utils/monitoring/internal/utils.py index 288fb435..c5ad8511 100644 --- a/edx_django_utils/monitoring/internal/utils.py +++ b/edx_django_utils/monitoring/internal/utils.py @@ -19,9 +19,17 @@ """ from contextlib import ExitStack, contextmanager +from django.conf import settings + from .backends import configured_backends from .middleware import CachedCustomMonitoringMiddleware +DJANGO_MANAGEMENT_MONITORING_OPERATION_NAME = getattr( + settings, + 'DJANGO_MANAGEMENT_MONITORING_OPERATION_NAME', + 'django.command' +) + try: import newrelic.agent except ImportError: # pragma: no cover @@ -143,3 +151,19 @@ def noop_decorator(func): return newrelic.agent.background_task(*args, **kwargs) else: return noop_decorator + + +@contextmanager +def monitor_django_management_command(name): + """ + A context manager for monitoring Django management commands. + + This function creates a monitoring span using `function_trace`, allowing + the execution of the command to be tracked explicitly in monitoring tools. + It also sets the transaction name to the given `name` using + `set_monitoring_transaction_name`. + """ + + with function_trace(DJANGO_MANAGEMENT_MONITORING_OPERATION_NAME): + set_monitoring_transaction_name(name) + yield