Skip to content

Commit 2d7519c

Browse files
authored
Merge pull request #457 from openedx/diana/refactor-monitoring
feat: Add implementation for set_monitoring_transaction_name in DD.
2 parents b4c8425 + 4e893ef commit 2d7519c

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

CHANGELOG.rst

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

1212
.. There should always be an "Unreleased" section for changes pending release.
1313
14+
[6.1.0] - 2024-10-15
15+
---------------------
16+
Changed
17+
~~~~~~~
18+
* Added Datadog implementation of ``set_monitoring_transaction_name`` and refactored the functionality.
19+
1420
[6.0.0] - 2024-10-09
1521
---------------------
1622
Added

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.0.0"
5+
__version__ = "6.1.0"
66

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

edx_django_utils/monitoring/README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ Feature support matrix for built-in telemetry backends:
3232
- ✅
3333
- ❌
3434
- ✅
35-
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``, ``set_monitoring_transaction_name``)
35+
* - Set local root span name (``set_monitoring_transaction_name``)
36+
- ✅
37+
- ❌
38+
- ✅
39+
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``)
3640
- ✅
3741
- ❌
3842
- ❌

edx_django_utils/monitoring/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
MonitoringMemoryMiddleware,
1919
MonitoringSupportMiddleware
2020
)
21-
from .internal.transactions import get_current_transaction, ignore_transaction, set_monitoring_transaction_name
21+
from .internal.transactions import get_current_transaction, ignore_transaction
2222
from .internal.utils import (
2323
accumulate,
2424
background_task,
2525
function_trace,
2626
increment,
2727
record_exception,
2828
set_custom_attribute,
29-
set_custom_attributes_for_course_key
29+
set_custom_attributes_for_course_key,
30+
set_monitoring_transaction_name
3031
)
3132
# "set_custom_metric*" methods are deprecated
3233
from .utils import set_custom_metric, set_custom_metrics_for_course_key

edx_django_utils/monitoring/internal/backends.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def tag_root_span_with_error(self, exception):
7070
be implemented for OTEL.
7171
"""
7272

73+
@abstractmethod
74+
def set_local_root_span_name(self, name, group=None, priority=None):
75+
"""
76+
Sets the name, group, and priority for a span.
77+
"""
78+
7379

7480
class NewRelicBackend(TelemetryBackend):
7581
"""
@@ -108,6 +114,9 @@ def tag_root_span_with_error(self, exception):
108114
# Does not need to be implemented for NewRelic, because it is handled automatically.
109115
pass
110116

117+
def set_local_root_span_name(self, name, group=None, priority=None):
118+
newrelic.agent.set_transaction_name(name, group, priority)
119+
111120

112121
class OpenTelemetryBackend(TelemetryBackend):
113122
"""
@@ -137,6 +146,10 @@ def tag_root_span_with_error(self, exception):
137146
# Currently, this is not implemented for OTel
138147
pass
139148

149+
def set_local_root_span_name(self, name, group=None, priority=None):
150+
# Currently this is not implemented
151+
pass
152+
140153

141154
class DatadogBackend(TelemetryBackend):
142155
"""
@@ -165,6 +178,11 @@ def tag_root_span_with_error(self, exception):
165178
root_span = self.dd_tracer.current_root_span()
166179
root_span.set_exc_info(type(exception), exception, exception.__traceback__)
167180

181+
def set_local_root_span_name(self, name, group=None, priority=None):
182+
# For Datadog, this updates the 'resource_name' to the given name.
183+
local_root_span = self.dd_tracer.current_root_span()
184+
local_root_span.resource = name
185+
168186

169187
# We're using an lru_cache instead of assigning the result to a variable on
170188
# module load. With the default settings (pointing to a TelemetryBackend

edx_django_utils/monitoring/internal/transactions.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@
1616
newrelic = None # pylint: disable=invalid-name
1717

1818

19-
def set_monitoring_transaction_name(name, group=None, priority=None):
20-
"""
21-
Sets the transaction name for monitoring.
22-
23-
This is not cached, and only support reporting to New Relic.
24-
25-
"""
26-
if newrelic: # pragma: no cover
27-
newrelic.agent.set_transaction_name(name, group, priority)
28-
29-
3019
def ignore_transaction():
3120
"""
32-
Ignore the transaction in monitoring
21+
Ignore the transaction in monitoring. Only works for NewRelic.
3322
3423
This allows us to ignore code paths that are unhelpful to include, such as
3524
`/health/` checks.
25+
3626
"""
27+
# Note: This is not being ported over to backends, because we don't have
28+
# an equivalent for Datadog. For Datadog, use filter/ignore rules.
3729
if newrelic: # pragma: no cover
3830
newrelic.agent.ignore_transaction()
3931

edx_django_utils/monitoring/internal/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def function_trace(function_name):
112112
yield
113113

114114

115+
def set_monitoring_transaction_name(name, group=None, priority=None):
116+
"""
117+
Sets the name, group, and priority for the current root span.
118+
119+
Current root span refers to the most ancestral span in the current process, rather than
120+
to the trace root, which may be outside of the process.
121+
122+
Group and priority may not be supported by all backends.
123+
"""
124+
for backend in configured_backends():
125+
backend.set_local_root_span_name(name, group=group, priority=priority)
126+
127+
115128
def background_task(*args, **kwargs):
116129
"""
117130
Handles monitoring for background tasks that are not passed in through the web server like

0 commit comments

Comments
 (0)