Skip to content

Commit

Permalink
feat: Add implementation for set_monitoring_transaction_name in DD.
Browse files Browse the repository at this point in the history
This also refactors the existing NewRelic implementation to use the
backends framework.

Also added comments about not porting ignore_transactions. This way of
ignoring spans is not supported by Datadog.
  • Loading branch information
dianakhuang committed Oct 15, 2024
1 parent b4c8425 commit 4e893ef
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
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.
[6.1.0] - 2024-10-15
---------------------
Changed
~~~~~~~
* Added Datadog implementation of ``set_monitoring_transaction_name`` and refactored the functionality.

[6.0.0] - 2024-10-09
---------------------
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__ = "6.0.0"
__version__ = "6.1.0"

default_app_config = (
"edx_django_utils.apps.EdxDjangoUtilsConfig"
Expand Down
6 changes: 5 additions & 1 deletion edx_django_utils/monitoring/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ Feature support matrix for built-in telemetry backends:
- ✅
- ❌
- ✅
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``, ``set_monitoring_transaction_name``)
* - Set local root span name (``set_monitoring_transaction_name``)
- ✅
- ❌
- ✅
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``)
- ✅
- ❌
- ❌
Expand Down
5 changes: 3 additions & 2 deletions edx_django_utils/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
MonitoringMemoryMiddleware,
MonitoringSupportMiddleware
)
from .internal.transactions import get_current_transaction, ignore_transaction, set_monitoring_transaction_name
from .internal.transactions import get_current_transaction, ignore_transaction
from .internal.utils import (
accumulate,
background_task,
function_trace,
increment,
record_exception,
set_custom_attribute,
set_custom_attributes_for_course_key
set_custom_attributes_for_course_key,
set_monitoring_transaction_name
)
# "set_custom_metric*" methods are deprecated
from .utils import set_custom_metric, set_custom_metrics_for_course_key
18 changes: 18 additions & 0 deletions edx_django_utils/monitoring/internal/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def tag_root_span_with_error(self, exception):
be implemented for OTEL.
"""

@abstractmethod
def set_local_root_span_name(self, name, group=None, priority=None):
"""
Sets the name, group, and priority for a span.
"""


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

def set_local_root_span_name(self, name, group=None, priority=None):
newrelic.agent.set_transaction_name(name, group, priority)

Check warning on line 118 in edx_django_utils/monitoring/internal/backends.py

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L118

Added line #L118 was not covered by tests


class OpenTelemetryBackend(TelemetryBackend):
"""
Expand Down Expand Up @@ -137,6 +146,10 @@ def tag_root_span_with_error(self, exception):
# Currently, this is not implemented for OTel
pass

def set_local_root_span_name(self, name, group=None, priority=None):
# Currently this is not implemented
pass

Check warning on line 151 in edx_django_utils/monitoring/internal/backends.py

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L151

Added line #L151 was not covered by tests


class DatadogBackend(TelemetryBackend):
"""
Expand Down Expand Up @@ -165,6 +178,11 @@ def tag_root_span_with_error(self, exception):
root_span = self.dd_tracer.current_root_span()
root_span.set_exc_info(type(exception), exception, exception.__traceback__)

def set_local_root_span_name(self, name, group=None, priority=None):
# For Datadog, this updates the 'resource_name' to the given name.
local_root_span = self.dd_tracer.current_root_span()
local_root_span.resource = name

Check warning on line 184 in edx_django_utils/monitoring/internal/backends.py

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L183-L184

Added lines #L183 - L184 were not covered by tests


# We're using an lru_cache instead of assigning the result to a variable on
# module load. With the default settings (pointing to a TelemetryBackend
Expand Down
16 changes: 4 additions & 12 deletions edx_django_utils/monitoring/internal/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,16 @@
newrelic = None # pylint: disable=invalid-name


def 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.
"""
if newrelic: # pragma: no cover
newrelic.agent.set_transaction_name(name, group, priority)


def ignore_transaction():
"""
Ignore the transaction in monitoring
Ignore the transaction in monitoring. Only works for NewRelic.
This allows us to ignore code paths that are unhelpful to include, such as
`/health/` checks.
"""
# Note: This is not being ported over to backends, because we don't have
# an equivalent for Datadog. For Datadog, use filter/ignore rules.
if newrelic: # pragma: no cover
newrelic.agent.ignore_transaction()

Expand Down
13 changes: 13 additions & 0 deletions edx_django_utils/monitoring/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ def function_trace(function_name):
yield


def set_monitoring_transaction_name(name, group=None, priority=None):
"""
Sets the name, group, and priority for the current root span.
Current root span refers to the most ancestral span in the current process, rather than
to the trace root, which may be outside of the process.
Group and priority may not be supported by all backends.
"""
for backend in configured_backends():
backend.set_local_root_span_name(name, group=group, priority=priority)

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/utils.py#L125

Added line #L125 was not covered by tests


def background_task(*args, **kwargs):
"""
Handles monitoring for background tasks that are not passed in through the web server like
Expand Down

0 comments on commit 4e893ef

Please sign in to comment.