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(pin): deprecates multi tracer support in ddtrace.Pin [3.0] #11884

Merged
merged 9 commits into from
Jan 16, 2025
34 changes: 27 additions & 7 deletions ddtrace/_trace/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import wrapt

import ddtrace
from ddtrace.vendor.debtcollector import deprecate

from ..internal.logger import get_logger

Expand Down Expand Up @@ -41,6 +42,12 @@ def __init__(
_config=None, # type: Optional[Dict[str, Any]]
):
# type: (...) -> None
if tracer is not None and tracer is not ddtrace.tracer:
deprecate(
"Initializing ddtrace.Pin with `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)
tracer = tracer or ddtrace.tracer
self.tags = tags
self.tracer = tracer
Expand Down Expand Up @@ -72,15 +79,15 @@ def __repr__(self):
def _find(*objs):
# type: (Any) -> Optional[Pin]
"""
Return the first :class:`ddtrace.trace.Pin` found on any of the provided objects or `None` if none were found
Return the first :class:`ddtrace.pin.Pin` found on any of the provided objects or `None` if none were found


>>> pin = Pin._find(wrapper, instance, conn)

:param objs: The objects to search for a :class:`ddtrace.trace.Pin` on
:param objs: The objects to search for a :class:`ddtrace.pin.Pin` on
:type objs: List of objects
:rtype: :class:`ddtrace.trace.Pin`, None
:returns: The first found :class:`ddtrace.trace.Pin` or `None` is none was found
:rtype: :class:`ddtrace.pin.Pin`, None
:returns: The first found :class:`ddtrace.pin.Pin` or `None` is none was found
"""
for obj in objs:
pin = Pin.get_from(obj)
Expand All @@ -98,10 +105,10 @@ def get_from(obj):

>>> pin = Pin.get_from(conn)

:param obj: The object to look for a :class:`ddtrace.trace.Pin` on
:param obj: The object to look for a :class:`ddtrace.pin.Pin` on
:type obj: object
:rtype: :class:`ddtrace.trace.Pin`, None
:returns: :class:`ddtrace.trace.Pin` associated with the object or None
:rtype: :class:`ddtrace.pin.Pin`, None
:returns: :class:`ddtrace.pin.Pin` associated with the object, or None if none was found
"""
if hasattr(obj, "__getddpin__"):
return obj.__getddpin__()
Expand Down Expand Up @@ -132,6 +139,12 @@ def override(
>>> # Override a pin for a specific connection
>>> Pin.override(conn, service='user-db')
"""
if tracer is not None:
deprecate(
"Calling ddtrace.Pin.override(...) with the `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)
if not obj:
return

Expand Down Expand Up @@ -193,6 +206,13 @@ def clone(
if not tags and self.tags:
tags = self.tags.copy()

if tracer is not None:
deprecate(
"Initializing ddtrace.Pin with `tracer` argument is deprecated",
message="All Pin instances should use the global tracer instance",
removal_version="3.0.0",
)

# we use a copy instead of a deepcopy because we expect configurations
# to have only a root level dictionary without nested objects. Using
# deepcopy introduces a big overhead:
Expand Down
3 changes: 2 additions & 1 deletion ddtrace/contrib/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from ddtrace import patch
from ddtrace.trace import Pin


patch(grpc=True)

# override the pin on the client
Expand All @@ -62,7 +63,7 @@
from grpc.framework.foundation import logging_pool

from ddtrace import patch
from ddtrace.trace import Pin, Tracer
from ddtrace.trace import Pin

patch(grpc=True)

Expand Down
8 changes: 7 additions & 1 deletion ddtrace/contrib/internal/django/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import wrapt
from wrapt.importer import when_imported

import ddtrace
from ddtrace import config
from ddtrace.appsec._utils import _UserInfoRetriever
from ddtrace.constants import SPAN_KIND
Expand Down Expand Up @@ -147,7 +148,12 @@ def cursor(django, pin, func, instance, args, kwargs):
tags = {"django.db.vendor": vendor, "django.db.alias": alias}
tags.update(getattr(conn, "_datadog_tags", {}))

pin = Pin(service, tags=tags, tracer=pin.tracer)
# Calling ddtrace.pin.Pin(...) with the `tracer` argument generates a deprecation warning.
# Remove this if statement when the `tracer` argument is removed
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
if pin.tracer is ddtrace.tracer:
pin = Pin(service, tags=tags)
P403n1x87 marked this conversation as resolved.
Show resolved Hide resolved
else:
pin = Pin(service, tags=tags, tracer=pin.tracer)

cursor = func(*args, **kwargs)

Expand Down
9 changes: 7 additions & 2 deletions ddtrace/contrib/internal/mongoengine/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ class WrappedConnect(wrapt.ObjectProxy):

def __init__(self, connect):
super(WrappedConnect, self).__init__(connect)
ddtrace.trace.Pin(_SERVICE, tracer=ddtrace.tracer).onto(self)
ddtrace.trace.Pin(_SERVICE).onto(self)

def __call__(self, *args, **kwargs):
client = self.__wrapped__(*args, **kwargs)
pin = ddtrace.trace.Pin.get_from(self)
if pin:
ddtrace.trace.Pin(service=pin.service, tracer=pin.tracer).onto(client)
# Calling ddtrace.pin.Pin(...) with the `tracer` argument generates a deprecation warning.
# Remove this if statement when the `tracer` argument is removed
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
if pin.tracer is ddtrace.tracer:
ddtrace.trace.Pin(service=pin.service).onto(client)
else:
ddtrace.trace.Pin(service=pin.service, tracer=pin.tracer).onto(client)

return client
7 changes: 6 additions & 1 deletion ddtrace/contrib/internal/pylibmc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def __init__(self, client=None, service=memcached.SERVICE, tracer=None, *args, *
super(TracedClient, self).__init__(client)

schematized_service = schematize_service_name(service)
pin = ddtrace.trace.Pin(service=schematized_service, tracer=tracer)
# Calling ddtrace.pin.Pin(...) with the `tracer` argument generates a deprecation warning.
# Remove this if statement when the `tracer` argument is removed
if tracer is ddtrace.tracer:
pin = ddtrace.trace.Pin(service=schematized_service)
else:
pin = ddtrace.trace.Pin(service=schematized_service, tracer=tracer)
pin.onto(self)

# attempt to collect the pool of urls this client talks to
Expand Down
7 changes: 6 additions & 1 deletion ddtrace/contrib/internal/sqlalchemy/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ def __init__(self, tracer, service, engine):
self.name = schematize_database_operation("%s.query" % self.vendor, database_provider=self.vendor)

# attach the PIN
Pin(tracer=tracer, service=self.service).onto(engine)
# Calling ddtrace.pin.Pin(...) with the `tracer` argument generates a deprecation warning.
# Remove this if statement when the `tracer` argument is removed
if self.tracer is ddtrace.tracer:
Pin(service=self.service).onto(engine)
else:
Pin(tracer=tracer, service=self.service).onto(engine)

listen(engine, "before_cursor_execute", self._before_cur_exec)
listen(engine, "after_cursor_execute", self._after_cur_exec)
Expand Down
7 changes: 6 additions & 1 deletion ddtrace/contrib/internal/tornado/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ def tracer_config(__init__, app, args, kwargs):
tracer.set_tags(tags)

# configure the PIN object for template rendering
ddtrace.trace.Pin(service=service, tracer=tracer).onto(template)
# Required for backwards compatibility. Remove the else clause when
# the `ddtrace.Pin` object no longer accepts the Pin argument.
if tracer is ddtrace.tracer:
ddtrace.trace.Pin(service=service).onto(template)
else:
ddtrace.trace.Pin(service=service, tracer=tracer).onto(template)
4 changes: 2 additions & 2 deletions ddtrace/contrib/vertica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
``Pin`` API::

from ddtrace import patch
from ddtrace.trace import Pin, Tracer
from ddtrace.trace import Pin
patch(vertica=True)

import vertica_python

conn = vertica_python.connect(**YOUR_VERTICA_CONFIG)

# override the service and tracer to be used
# override the service
Pin.override(conn, service='myverticaservice')
"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
deprecations:
- |
tracer: Deprecates the ability to use multiple tracer instances with ddtrace.Pin. In v3.0.0 pin objects will only use the global tracer.
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
Loading