Skip to content

Commit

Permalink
feat: add monitor_django_management_command
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-56 committed Feb 18, 2025
1 parent 7755284 commit 7b543c3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion edx_django_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions edx_django_utils/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
accumulate,
function_trace,
increment,
monitor_django_management_command,
record_exception,
set_custom_attribute,
set_custom_attributes_for_course_key,
Expand Down
24 changes: 24 additions & 0 deletions edx_django_utils/monitoring/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Check warning on line 169 in edx_django_utils/monitoring/internal/utils.py

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/utils.py#L167-L169

Added lines #L167 - L169 were not covered by tests

0 comments on commit 7b543c3

Please sign in to comment.