Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create manual spans for monitoring backends. #435

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

[5.15.0] - 2024-07-29
---------------------
Added
~~~~~
* Added Datadog implementation of ``function_trace`` and allowed implementation to be configurable.

[5.14.2] - 2024-05-31
---------------------
Fixed
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__ = "5.14.2"
__version__ = "5.15.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 @@ -28,7 +28,11 @@ Feature support matrix for built-in telemetry backends:
- ✅ (on root span)
- ✅ (on current span)
- ✅ (on root span)
* - Retrieve and manipulate spans (``function_trace``, ``get_current_transaction``, ``ignore_transaction``, ``set_monitoring_transaction_name``)
* - Create a new span (``function_trace``)
- ✅
- ❌
- ✅
* - Retrieve and manipulate spans (``get_current_transaction``, ``ignore_transaction``, ``set_monitoring_transaction_name``)
- ✅
- ❌
- ❌
Expand Down
8 changes: 2 additions & 6 deletions edx_django_utils/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
FrontendMonitoringMiddleware,
MonitoringMemoryMiddleware
)
from .internal.transactions import (
function_trace,
get_current_transaction,
ignore_transaction,
set_monitoring_transaction_name
)
from .internal.transactions import get_current_transaction, ignore_transaction, set_monitoring_transaction_name
from .internal.utils import (
accumulate,
background_task,
function_trace,
increment,
record_exception,
set_custom_attribute,
Expand Down
26 changes: 26 additions & 0 deletions edx_django_utils/monitoring/internal/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
Record the exception that is currently being handled.
"""

@abstractmethod
def create_span(self, name):
"""
Start a tracing span with the given name, returning a context manager instance.

The caller must use the return value in a `with` statement or similar so that the
span is guaranteed to be closed appropriately.

Implementations should create a new child span parented to the current span,
or create a new root span if not currently in a span.
"""


class NewRelicBackend(TelemetryBackend):
"""
Expand Down Expand Up @@ -77,6 +89,13 @@
# https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/recordexception-python-agent-api/
newrelic.agent.record_exception()

def create_span(self, name):
if newrelic.version_info[0] >= 5:
return newrelic.agent.FunctionTrace(name)

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L94

Added line #L94 was not covered by tests
else:
nr_transaction = newrelic.agent.current_transaction()
return newrelic.agent.FunctionTrace(nr_transaction, name)

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L96-L97

Added lines #L96 - L97 were not covered by tests


class OpenTelemetryBackend(TelemetryBackend):
"""
Expand All @@ -98,6 +117,10 @@
def record_exception(self):
self.otel_trace.get_current_span().record_exception(sys.exc_info()[1])

def create_span(self, name):
# Currently, this is not implemented.
pass

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L122

Added line #L122 was not covered by tests


class DatadogBackend(TelemetryBackend):
"""
Expand All @@ -119,6 +142,9 @@
if span := self.dd_tracer.current_span():
span.set_traceback()

def create_span(self, name):
return self.dd_tracer.trace(name)

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/backends.py#L146

Added line #L146 was not covered by tests

dianakhuang marked this conversation as resolved.
Show resolved Hide resolved

# 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
25 changes: 0 additions & 25 deletions edx_django_utils/monitoring/internal/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

Please remember to expose any new methods in the `__init__.py` file.
"""

from contextlib import contextmanager

try:
import newrelic.agent
except ImportError:
Expand Down Expand Up @@ -41,28 +38,6 @@ def ignore_transaction():
newrelic.agent.ignore_transaction()


@contextmanager
def function_trace(function_name):
"""
Wraps a chunk of code that we want to appear as a separate, explicit,
segment in our monitoring tools.
"""
# Not covering this because if we mock it, we're not really testing anything
# anyway. If something did break, it should show up in tests for apps that
# use this code with newrelic enabled, on whatever version of newrelic they
# run.
if newrelic: # pragma: no cover
if newrelic.version_info[0] >= 5:
with newrelic.agent.FunctionTrace(function_name):
yield
else:
nr_transaction = newrelic.agent.current_transaction()
with newrelic.agent.FunctionTrace(nr_transaction, function_name):
yield
else:
yield


class MonitoringTransaction():
"""
Represents a monitoring transaction (likely the current transaction).
Expand Down
20 changes: 20 additions & 0 deletions edx_django_utils/monitoring/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
At this time, the custom monitoring will only be reported to New Relic.

"""
from contextlib import ExitStack, contextmanager

from .backends import configured_backends
from .middleware import CachedCustomMonitoringMiddleware

Expand Down Expand Up @@ -92,6 +94,24 @@
backend.record_exception()


@contextmanager
def function_trace(function_name):
"""
Wraps a chunk of code that we want to appear as a separate, explicit,
segment in our monitoring tools.
"""
# Not covering this because if we mock it, we're not really testing anything
# anyway. If something did break, it should show up in tests for apps that
# use this code with whatever uses it.
# ExitStack handles the underlying context managers.
with ExitStack() as stack:
for backend in configured_backends():
context = backend.create_span(function_name)

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/utils.py#L109

Added line #L109 was not covered by tests
if context is not None:
stack.enter_context(context)
yield

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

View check run for this annotation

Codecov / codecov/patch

edx_django_utils/monitoring/internal/utils.py#L111-L112

Added lines #L111 - L112 were 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