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

chore: deprecate the span= arguments on ExecutionContext methods #7957

Merged
merged 10 commits into from
Jan 3, 2024
39 changes: 39 additions & 0 deletions ddtrace/internal/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def _on_jsonify_context_started_flask(ctx):
from typing import Optional # noqa:F401
from typing import Tuple # noqa:F401

from ddtrace.vendor.debtcollector import deprecate

from ..utils.deprecations import DDTraceDeprecationWarning
from . import event_hub # noqa:F401
from .event_hub import EventResultDict # noqa:F401
from .event_hub import dispatch
Expand All @@ -134,12 +137,24 @@ def _on_jsonify_context_started_flask(ctx):

_CURRENT_CONTEXT = None
ROOT_CONTEXT_ID = "__root"
SPAN_DEPRECATION_MESSAGE = (
"The 'span' keyword argument on ExecutionContext methods is deprecated and will be removed in a future version."
)
SPAN_DEPRECATION_SUGGESTION = (
"Please store contextual data on the ExecutionContext object using other kwargs and/or set_item()"
)


class ExecutionContext:
__slots__ = ["identifier", "_data", "_parents", "_span", "_token"]

def __init__(self, identifier, parent=None, span=None, **kwargs):
if span is not None:
deprecate(
SPAN_DEPRECATION_MESSAGE,
message=SPAN_DEPRECATION_SUGGESTION,
category=DDTraceDeprecationWarning,
)
self.identifier = identifier
self._data = {}
self._parents = []
Expand Down Expand Up @@ -260,6 +275,12 @@ def context_with_data(identifier, parent=None, **kwargs):

def get_item(data_key, span=None):
# type: (str, Optional[Span]) -> Optional[Any]
if span is not None:
deprecate(
SPAN_DEPRECATION_MESSAGE,
message=SPAN_DEPRECATION_SUGGESTION,
category=DDTraceDeprecationWarning,
)
if span is not None and span._local_root is not None:
return span._local_root._get_ctx_item(data_key)
else:
Expand All @@ -268,6 +289,12 @@ def get_item(data_key, span=None):

def get_items(data_keys, span=None):
# type: (List[str], Optional[Span]) -> Optional[Any]
if span is not None:
deprecate(
SPAN_DEPRECATION_MESSAGE,
message=SPAN_DEPRECATION_SUGGESTION,
category=DDTraceDeprecationWarning,
)
if span is not None and span._local_root is not None:
return [span._local_root._get_ctx_item(key) for key in data_keys]
else:
Expand All @@ -282,6 +309,12 @@ def set_safe(data_key, data_value):
# NB Don't call these set_* functions from `ddtrace.contrib`, only from product code!
def set_item(data_key, data_value, span=None):
# type: (str, Optional[Any], Optional[Span]) -> None
if span is not None:
deprecate(
SPAN_DEPRECATION_MESSAGE,
message=SPAN_DEPRECATION_SUGGESTION,
category=DDTraceDeprecationWarning,
)
if span is not None and span._local_root is not None:
span._local_root._set_ctx_item(data_key, data_value)
else:
Expand All @@ -290,6 +323,12 @@ def set_item(data_key, data_value, span=None):

def set_items(keys_values, span=None):
# type: (Dict[str, Optional[Any]], Optional[Span]) -> None
if span is not None:
deprecate(
SPAN_DEPRECATION_MESSAGE,
message=SPAN_DEPRECATION_SUGGESTION,
category=DDTraceDeprecationWarning,
)
if span is not None and span._local_root is not None:
span._local_root._set_ctx_items(keys_values)
else:
Expand Down