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(tracer): deprecates patch_all #11918

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion benchmarks/ddtrace_run/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def run(self):

# initialize subprocess args
subp_cmd = []
code = "import ddtrace; ddtrace.patch_all()\n"
code = "import ddtrace; ddtrace._patch_all()\n"
if self.ddtrace_run:
subp_cmd = ["ddtrace-run"]
code = ""
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import ddtrace.internal.telemetry # noqa: E402

from ._monkey import patch # noqa: E402
from ._monkey import patch_all # noqa: E402
from ._monkey import _patch_all # noqa: E402
from .internal.compat import PYTHON_VERSION_INFO # noqa: E402
from .internal.utils.deprecations import DDTraceDeprecationWarning # noqa: E402
from ddtrace._trace.pin import Pin # noqa: E402
Expand Down Expand Up @@ -56,7 +56,7 @@

__all__ = [
"patch",
"patch_all",
"_patch_all",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Customers that once were able to

from ddtrace import patch_all
patch_all()

will now get an import error. Or am I missing something? Will this proposed change happen in a major version bump?

"Pin",
"Span",
"Tracer",
Expand Down
14 changes: 13 additions & 1 deletion ddtrace/_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

from ddtrace.appsec import load_common_appsec_modules
from ddtrace.internal.telemetry.constants import TELEMETRY_NAMESPACE
from ddtrace.vendor.debtcollector import deprecate

from .appsec._iast._utils import _is_iast_enabled
from .internal import telemetry
from .internal.logger import get_logger
from .internal.utils import formats
from .internal.utils.deprecations import DDTraceDeprecationWarning
from .settings import _config as config


Expand Down Expand Up @@ -209,6 +211,16 @@ def on_import(hook):


def patch_all(**patch_modules):
deprecate(
"patch_all is deprecated and will be removed in a future version of the tracer.",
message="""patch_all is deprecated in favor of ``import ddtrace.auto`` and ``DD_PATCH_MODULES``
environment variable if needed.""",
category=DDTraceDeprecationWarning,
)
_patch_all(**patch_modules)


def _patch_all(**patch_modules):
# type: (bool) -> None
"""Enables ddtrace library instrumentation.

Expand All @@ -219,7 +231,7 @@ def patch_all(**patch_modules):

:param dict patch_modules: Override whether particular modules are patched or not.

>>> patch_all(redis=False, cassandra=False)
>>> _patch_all(redis=False, cassandra=False)
"""
modules = PATCH_MODULES.copy()

Expand Down
4 changes: 2 additions & 2 deletions ddtrace/bootstrap/preload.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _(_):
LLMObs.enable()

if asbool(os.getenv("DD_TRACE_ENABLED", default=True)):
from ddtrace import patch_all
from ddtrace import _patch_all

@register_post_preload
def _():
Expand All @@ -114,7 +114,7 @@ def _():
modules_to_patch = os.getenv("DD_PATCH_MODULES")
modules_to_str = parse_tags_str(modules_to_patch)
modules_to_bool = {k: asbool(v) for k, v in modules_to_str.items()}
patch_all(**modules_to_bool)
_patch_all(**modules_to_bool)

if config._trace_methods:
_install_trace_methods(config._trace_methods)
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/contrib/aws_lambda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~~~~~~~~~~~~~~~~~~~~

This integration is configured automatically. The `datadog_lambda` package
calls ``patch_all`` when ``DD_TRACE_ENABLED`` is set to ``true``.
calls ``_patch_all`` when ``DD_TRACE_ENABLED`` is set to ``true``.
It's not recommended to call ``patch`` for it manually. Since it would not do
anything for other environments that do not meet the criteria above.

Expand Down
10 changes: 2 additions & 8 deletions ddtrace/contrib/httplib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@

The httplib integration is disabled by default. It can be enabled when using
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`
using the ``DD_TRACE_HTTPLIB_ENABLED`` environment variable::
using the `DD_PATCH_MODULES` environment variable::

DD_TRACE_HTTPLIB_ENABLED=true ddtrace-run ....

The integration can also be enabled manually in code with
:func:`patch_all()<ddtrace.patch_all>`::

from ddtrace import patch_all
patch_all(httplib=True)
DD_PATCH_MODULES=httplib:true ddtrace-run ....


Global Configuration
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/contrib/internal/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ def patch_all(request):
import ddtrace

if request.config.getoption("ddtrace-patch-all") or request.config.getini("ddtrace-patch-all"):
ddtrace.patch_all()
import ddtrace.auto # noqa: F401
8 changes: 5 additions & 3 deletions ddtrace/contrib/kombu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""Instrument kombu to report AMQP messaging.

``patch_all`` will not automatically patch your Kombu client to make it work, as this would conflict with the
ref:`import ddtrace.auto<ddtraceauto>` and `ddtrace-run` will not automatically patch your Kombu client to
make it work, as this would conflict with the
Celery integration. You must specifically request kombu be patched, as in the example below.

Note: To permit distributed tracing for the kombu integration you must enable the tracer with priority
sampling. Refer to the documentation here:
https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#priority-sampling

Without enabling distributed tracing, spans within a trace generated by the kombu integration might be dropped
without the whole trace being dropped.
without the whole trace being dropped. Use either `DD_PATCH_MODULES=kombu:true` as an environment variable
or patch it manually with the following code:
::

from ddtrace import patch
from ddtrace.trace import Pin
import kombu

# If not patched yet, you can patch kombu specifically
# Patch kombu
patch(kombu=True)

# This will report a span with the default settings
Expand Down
3 changes: 1 addition & 2 deletions ddtrace/contrib/sanic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

Sanic tracing can also be enabled explicitly::

from ddtrace import patch_all
patch_all(sanic=True)
import ddtrace.auto

from sanic import Sanic
from sanic.response import text
Expand Down
6 changes: 0 additions & 6 deletions ddtrace/contrib/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
from ddtrace import patch
patch(snowflake=True)

or use :func:`patch_all()<ddtrace.patch_all>` to manually enable the integration::

from ddtrace import patch_all
patch_all(snowflake=True)




Global Configuration
Expand Down
12 changes: 5 additions & 7 deletions ddtrace/contrib/urllib3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
Enabling
~~~~~~~~

The ``urllib3`` integration is not enabled by default. Use ``patch_all()``
with the environment variable ``DD_TRACE_URLLIB3_ENABLED`` set, or call
:func:`patch()<ddtrace.patch>` with the ``urllib3`` argument set to ``True`` to manually
enable the integration, before importing and using ``urllib3``::

from ddtrace import patch
patch(urllib3=True)
The ``urllib3`` integration is not enabled by default. Use either ``ddtrace-run``
or ``import ddtrace.auto`` with ``DD_PATCH_MODULES`` to enable it.
``DD_PATCH_MODULES=urllib3 ddtrace-run python app.py`` or
``DD_PATCH_MODULES=urllib3:true python app.py``::

import ddtrace.auto
# use urllib3 like usual


Expand Down
2 changes: 0 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ Tracing

.. automodule:: ddtrace.auto

.. autofunction:: ddtrace.patch_all

.. autofunction:: ddtrace.patch

.. autoclass:: ddtrace.Tracer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
deprecations:
- |
tracer: ``patch_all`` is deprecated and will be removed in 3.0. As an alternative to ``patch_all``, you can use ``import ddtrace.auto`` along with ``DD_PATCH_MODULES`` if specific module patching is necessary.
4 changes: 2 additions & 2 deletions tests/appsec/iast/fixtures/entrypoint/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def add_test():


def create_app_patch_all():
from ddtrace import patch_all
from ddtrace import _patch_all

patch_all()
_patch_all()
app = Flask(__name__)
return app

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_dogstatsd_client_env_url_path(self):

def test_patch_modules_from_env(self):
"""
DD_PATCH_MODULES overrides the defaults for patch_all()
DD_PATCH_MODULES overrides the defaults for _patch_all()
"""
with self.override_env(
env=dict(
Expand Down
14 changes: 7 additions & 7 deletions tests/contrib/algoliasearch/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ddtrace import _patch_all
from ddtrace import config
from ddtrace import patch_all
from ddtrace.contrib.internal.algoliasearch.patch import algoliasearch_version
from ddtrace.contrib.internal.algoliasearch.patch import patch
from ddtrace.contrib.internal.algoliasearch.patch import unpatch
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_patch_unpatch(self):
assert len(spans) == 1

def test_patch_all_auto_enable(self):
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")

Expand All @@ -178,7 +178,7 @@ def test_user_specified_service_default(self):
When a service name is specified by the user
The algoliasearch integration shouldn't use it as the service name
"""
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")
spans = self.get_spans()
Expand All @@ -194,7 +194,7 @@ def test_user_specified_service_v0(self):
When a service name is specified by the user
The algoliasearch integration shouldn't use it as the service name
"""
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")
spans = self.get_spans()
Expand All @@ -210,7 +210,7 @@ def test_user_specified_service_v1(self):
In the v1 service name schema, services default to $DD_SERVICE,
so make sure that is used and not the v0 schema 'algoliasearch'
"""
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")
spans = self.get_spans()
Expand All @@ -222,7 +222,7 @@ def test_user_specified_service_v1(self):

@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v0"))
def test_span_name_v0_schema(self):
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")
spans = self.get_spans()
Expand All @@ -234,7 +234,7 @@ def test_span_name_v0_schema(self):

@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v1"))
def test_span_name_v1_schema(self):
patch_all()
_patch_all()
Pin.override(self.index, tracer=self.tracer)
self.perform_search("test search")
spans = self.get_spans()
Expand Down
6 changes: 3 additions & 3 deletions tests/contrib/django/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ def test_django_use_handler_resource_format_env(client, test_spans):
"python",
"-c",
(
"from ddtrace import config, patch_all; patch_all(); "
"from ddtrace import config, _patch_all; _patch_all(); "
"import django; "
"assert config.django.use_handler_resource_format; print('Test success')"
),
Expand All @@ -2037,7 +2037,7 @@ def test_django_use_handler_with_url_name_resource_format_env(client, test_spans
"python",
"-c",
(
"from ddtrace import config, patch_all; patch_all(); "
"from ddtrace import config, _patch_all; _patch_all(); "
"import django; "
"assert config.django.use_handler_with_url_name_resource_format; print('Test success')"
),
Expand Down Expand Up @@ -2117,7 +2117,7 @@ def test_django_use_legacy_resource_format_env(client, test_spans):
"python",
"-c",
(
"from ddtrace import config, patch_all; patch_all(); "
"from ddtrace import config, _patch_all; _patch_all(); "
"import django; "
"assert config.django.use_legacy_resource_format; print('Test success')"
),
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/psycopg/test_psycopg_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_connect_traced_via_env(run_python_code_in_subprocess):
import ddtrace
from tests.contrib.config import POSTGRES_CONFIG

ddtrace.patch_all()
ddtrace._patch_all()

conn = psycopg.connect(**POSTGRES_CONFIG)
assert conn
Expand Down
8 changes: 4 additions & 4 deletions tests/contrib/psycopg2/test_psycopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ def test_manual_wrap_extension_adapt(self):

@skipIf(PSYCOPG2_VERSION < (2, 7), "quote_ident not available in psycopg2<2.7")
def test_manual_wrap_extension_quote_ident(self):
from ddtrace import patch_all
from ddtrace import _patch_all

patch_all()
_patch_all()
from psycopg2.extensions import quote_ident

# NOTE: this will crash if it doesn't work.
Expand Down Expand Up @@ -497,9 +497,9 @@ def test_postgres_dbm_propagation_comment(self):

@skipIf(PSYCOPG2_VERSION < (2, 7), "quote_ident not available in psycopg2<2.7")
def test_manual_wrap_extension_quote_ident_standalone():
from ddtrace import patch_all
from ddtrace import _patch_all

patch_all()
_patch_all()
from psycopg2.extensions import quote_ident

# NOTE: this will crash if it doesn't work.
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/psycopg2/test_psycopg_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_connect_traced_via_env(run_python_code_in_subprocess):
import ddtrace
from tests.contrib.config import POSTGRES_CONFIG

ddtrace.patch_all()
ddtrace._patch_all()

conn = psycopg2.connect(**POSTGRES_CONFIG)
assert conn
Expand Down
6 changes: 3 additions & 3 deletions tests/contrib/starlette/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,17 +497,17 @@ def test_incorrect_patching(run_python_code_in_subprocess):
from starlette.testclient import TestClient
import sqlalchemy

from ddtrace import patch_all
from ddtrace import _patch_all


from tests.contrib.starlette.app import get_app

engine = sqlalchemy.create_engine("sqlite:///test.db")
app = get_app(engine)

# Calling patch_all late
# Calling _patch_all late
# DEV: The test client uses `requests` so we want to ignore them for this scenario
patch_all(requests=False, http=False)
_patch_all(requests=False, http=False)
with TestClient(app) as test_client:
r = test_client.get("/200")

Expand Down
Loading
Loading