Skip to content

Commit b491897

Browse files
authored
Merge pull request #458 from openedx/diana/refactor-monitoring
feat: Remove unusued background_task transaction.
2 parents 2d7519c + 169acb7 commit b491897

File tree

8 files changed

+50
-77
lines changed

8 files changed

+50
-77
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ Change Log
1111

1212
.. There should always be an "Unreleased" section for changes pending release.
1313
14+
7.0.0 - 2024-10-16
15+
------------------
16+
Removed
17+
~~~~~~~
18+
* Remove unused ``background_task`` monitoring function.
19+
* Remove ``get_current_transaction`` (used internally only) from the public API.
20+
1421
[6.1.0] - 2024-10-15
1522
---------------------
1623
Changed

edx_django_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
EdX utilities for Django Application development..
33
"""
44

5-
__version__ = "6.1.0"
5+
__version__ = "7.0.0"
66

77
default_app_config = (
88
"edx_django_utils.apps.EdxDjangoUtilsConfig"

edx_django_utils/monitoring/README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@ Feature support matrix for built-in telemetry backends:
3636
- ✅
3737
- ❌
3838
- ✅
39-
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``)
39+
* - Manipulate spans (``ignore_transaction``)
4040
- ✅
4141
- ❌
4242
- ❌
4343
* - Record exceptions (``record_exception``)
4444
- ✅
4545
- ✅
4646
- ✅
47-
* - Instrument non-web tasks (``background_task``)
48-
- ✅
49-
- ❌
50-
- ❌
5147

5248
Additional requirements for using these backends:
5349

edx_django_utils/monitoring/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
MonitoringMemoryMiddleware,
1919
MonitoringSupportMiddleware
2020
)
21-
from .internal.transactions import get_current_transaction, ignore_transaction
21+
from .internal.transactions import ignore_transaction
2222
from .internal.utils import (
2323
accumulate,
24-
background_task,
2524
function_trace,
2625
increment,
2726
record_exception,

edx_django_utils/monitoring/internal/code_owner/middleware.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.urls import resolve
77
from django.urls.exceptions import Resolver404
88

9-
from ..transactions import get_current_transaction
109
from ..utils import set_custom_attribute
1110
from .utils import (
1211
_get_catch_all_code_owner,
@@ -15,9 +14,48 @@
1514
set_code_owner_custom_attributes
1615
)
1716

17+
try:
18+
import newrelic.agent
19+
except ImportError:
20+
newrelic = None # pylint: disable=invalid-name
21+
1822
log = logging.getLogger(__name__)
1923

2024

25+
class MonitoringTransaction():
26+
"""
27+
Represents a monitoring transaction (likely the current transaction).
28+
"""
29+
def __init__(self, transaction):
30+
self.transaction = transaction
31+
32+
@property
33+
def name(self):
34+
"""
35+
The name of the transaction.
36+
37+
For NewRelic, the name may look like:
38+
openedx.core.djangoapps.contentserver.middleware:StaticContentServer
39+
40+
"""
41+
if self.transaction and hasattr(self.transaction, 'name'):
42+
return self.transaction.name
43+
return None
44+
45+
46+
def get_current_transaction():
47+
"""
48+
Returns the current transaction. This is only used internally and won't
49+
be ported over to the backends framework, because transactions will be
50+
very different based on the backend.
51+
"""
52+
current_transaction = None
53+
if newrelic:
54+
current_transaction = newrelic.agent.current_transaction()
55+
56+
return MonitoringTransaction(current_transaction)
57+
58+
2159
class CodeOwnerMonitoringMiddleware:
2260
"""
2361
Django middleware object to set custom attributes for the owner of each view.

edx_django_utils/monitoring/internal/transactions.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,3 @@ def ignore_transaction():
2828
# an equivalent for Datadog. For Datadog, use filter/ignore rules.
2929
if newrelic: # pragma: no cover
3030
newrelic.agent.ignore_transaction()
31-
32-
33-
class MonitoringTransaction():
34-
"""
35-
Represents a monitoring transaction (likely the current transaction).
36-
"""
37-
def __init__(self, transaction):
38-
self.transaction = transaction
39-
40-
@property
41-
def name(self):
42-
"""
43-
The name of the transaction.
44-
45-
For NewRelic, the name may look like:
46-
openedx.core.djangoapps.contentserver.middleware:StaticContentServer
47-
48-
"""
49-
if self.transaction and hasattr(self.transaction, 'name'):
50-
return self.transaction.name
51-
return None
52-
53-
54-
def get_current_transaction():
55-
"""
56-
Returns the current transaction.
57-
"""
58-
current_transaction = None
59-
if newrelic:
60-
current_transaction = newrelic.agent.current_transaction()
61-
62-
return MonitoringTransaction(current_transaction)

edx_django_utils/monitoring/tests/test_custom_monitoring.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.test import TestCase, override_settings
1010

1111
from edx_django_utils.cache import RequestCache
12-
from edx_django_utils.monitoring import MonitoringSupportMiddleware, accumulate, get_current_transaction, increment
12+
from edx_django_utils.monitoring import MonitoringSupportMiddleware, accumulate, increment
1313

1414
from ..middleware import CachedCustomMonitoringMiddleware as DeprecatedCachedCustomMonitoringMiddleware
1515
from ..middleware import MonitoringCustomMetricsMiddleware as DeprecatedMonitoringCustomMetricsMiddleware
@@ -128,16 +128,6 @@ def test_error_tagging(self, mock_get_root_span):
128128
type(fake_exception), fake_exception, fake_exception.__traceback__
129129
)
130130

131-
@patch('newrelic.agent')
132-
def test_get_current_transaction(self, mock_newrelic_agent):
133-
mock_newrelic_agent.current_transaction().name = 'fake-transaction'
134-
current_transaction = get_current_transaction()
135-
self.assertEqual(current_transaction.name, 'fake-transaction')
136-
137-
def test_get_current_transaction_without_newrelic(self):
138-
current_transaction = get_current_transaction()
139-
self.assertEqual(current_transaction.name, None)
140-
141131
@patch('edx_django_utils.monitoring.utils.internal_accumulate')
142132
def test_deprecated_accumulate(self, mock_accumulate):
143133
deprecated_accumulate('key', 1)

edx_django_utils/monitoring/tests/test_utils.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)