Skip to content

Commit e987057

Browse files
chore(tracer): deprecates patch_all (#11918)
## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: Emmett Butler <[email protected]>
1 parent c4644f0 commit e987057

File tree

21 files changed

+70
-74
lines changed

21 files changed

+70
-74
lines changed

benchmarks/ddtrace_run/scenario.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def run(self):
2222

2323
# initialize subprocess args
2424
subp_cmd = []
25-
code = "import ddtrace; ddtrace.patch_all()\n"
25+
code = "import ddtrace; ddtrace._monkey._patch_all()\n"
2626
if self.ddtrace_run:
2727
subp_cmd = ["ddtrace-run"]
2828
code = ""

ddtrace/_monkey.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
from ddtrace.appsec import load_common_appsec_modules
1010
from ddtrace.internal.telemetry.constants import TELEMETRY_NAMESPACE
1111
from ddtrace.settings.asm import config as asm_config
12+
from ddtrace.vendor.debtcollector import deprecate
1213

1314
from .internal import telemetry
1415
from .internal.logger import get_logger
1516
from .internal.utils import formats
17+
from .internal.utils.deprecations import DDTraceDeprecationWarning # noqa: E402
1618

1719

1820
if TYPE_CHECKING: # pragma: no cover
@@ -224,8 +226,18 @@ def patch_all(**patch_modules: bool) -> None:
224226
225227
:param dict patch_modules: Override whether particular modules are patched or not.
226228
227-
>>> patch_all(redis=False, cassandra=False)
229+
>>> _patch_all(redis=False, cassandra=False)
228230
"""
231+
deprecate(
232+
"patch_all is deprecated and will be removed in a future version of the tracer.",
233+
message="""patch_all is deprecated in favor of ``import ddtrace.auto`` and ``DD_PATCH_MODULES``
234+
environment variable if needed.""",
235+
category=DDTraceDeprecationWarning,
236+
)
237+
_patch_all(**patch_modules)
238+
239+
240+
def _patch_all(**patch_modules: bool) -> None:
229241
modules = PATCH_MODULES.copy()
230242

231243
# The enabled setting can be overridden by environment variables

ddtrace/bootstrap/preload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _(_):
8787
LLMObs.enable(_auto=True)
8888

8989
if asbool(os.getenv("DD_TRACE_ENABLED", default=True)):
90-
from ddtrace import patch_all
90+
from ddtrace._monkey import _patch_all
9191

9292
@register_post_preload
9393
def _():
@@ -97,7 +97,7 @@ def _():
9797
modules_to_patch = os.getenv("DD_PATCH_MODULES")
9898
modules_to_str = parse_tags_str(modules_to_patch)
9999
modules_to_bool = {k: asbool(v) for k, v in modules_to_str.items()}
100-
patch_all(**modules_to_bool)
100+
_patch_all(**modules_to_bool)
101101

102102
if config._trace_methods:
103103
_install_trace_methods(config._trace_methods)

ddtrace/contrib/_aws_lambda.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
Global Configuration
1414
~~~~~~~~~~~~~~~~~~~~
1515
16-
This integration is configured automatically. The `datadog_lambda` package
17-
calls ``patch_all`` when ``DD_TRACE_ENABLED`` is set to ``true``.
18-
It's not recommended to call ``patch`` for it manually. Since it would not do
19-
anything for other environments that do not meet the criteria above.
16+
This integration is configured automatically when `ddtrace-run` or `import ddtrace.auto` is used.
2017
2118
2219
Configuration

ddtrace/contrib/_httplib.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
99
The httplib integration is disabled by default. It can be enabled when using
1010
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`
11-
using the ``DD_TRACE_HTTPLIB_ENABLED`` environment variable::
11+
using the `DD_PATCH_MODULES` environment variable or `DD_TRACE_HTTPLIB_ENABLED`::
1212
13-
DD_TRACE_HTTPLIB_ENABLED=true ddtrace-run ....
14-
15-
The integration can also be enabled manually in code with
16-
:func:`patch_all()<ddtrace.patch_all>`::
17-
18-
from ddtrace import patch_all
19-
patch_all(httplib=True)
13+
DD_PATCH_MODULES=httplib:true ddtrace-run ....
2014
2115
2216
Global Configuration

ddtrace/contrib/_kombu.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Instrument kombu to report AMQP messaging.
22
3-
``patch_all`` will not automatically patch your Kombu client to make it work, as this would conflict with the
3+
ref:`import ddtrace.auto<ddtraceauto>` and `ddtrace-run` will not automatically patch your Kombu client to
4+
make it work, as this would conflict with the
45
Celery integration. You must specifically request kombu be patched, as in the example below.
56
67
Note: To permit distributed tracing for the kombu integration you must enable the tracer with priority
@@ -9,10 +10,11 @@
910
1011
Without enabling distributed tracing, spans within a trace generated by the kombu integration might be dropped
1112
without the whole trace being dropped.
12-
::
1313
14-
from ddtrace import patch
15-
from ddtrace.trace import Pin
14+
Run with `DD_PATCH_MODULES=kombu:true`::
15+
16+
import ddtrace.auto
17+
from ddtrace import Pin
1618
import kombu
1719
1820
# If not patched yet, you can patch kombu specifically

ddtrace/contrib/_sanic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
99
Sanic tracing can also be enabled explicitly::
1010
11-
from ddtrace import patch_all
12-
patch_all(sanic=True)
11+
import ddtrace.auto
1312
1413
from sanic import Sanic
1514
from sanic.response import text

ddtrace/contrib/_snowflake.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,8 @@
99
The integration is not enabled automatically when using
1010
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`.
1111
12-
Use ``DD_TRACE_SNOWFLAKE_ENABLED=true`` to enable it with ``ddtrace-run``
13-
14-
or :func:`patch()<ddtrace.patch>` to manually enable the integration::
15-
16-
from ddtrace import patch
17-
patch(snowflake=True)
18-
19-
or use :func:`patch_all()<ddtrace.patch_all>` to manually enable the integration::
20-
21-
from ddtrace import patch_all
22-
patch_all(snowflake=True)
23-
24-
12+
Use environment variable ``DD_TRACE_SNOWFLAKE_ENABLED=true`` or `DD_PATCH_MODULES:snowflake:true`
13+
to manually enable the integration.
2514
2615
2716
Global Configuration

ddtrace/contrib/_urllib3.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
Enabling
77
~~~~~~~~
88
9-
The ``urllib3`` integration is not enabled by default. Use ``patch_all()``
10-
with the environment variable ``DD_TRACE_URLLIB3_ENABLED`` set, or call
11-
:func:`patch()<ddtrace.patch>` with the ``urllib3`` argument set to ``True`` to manually
12-
enable the integration, before importing and using ``urllib3``::
13-
14-
from ddtrace import patch
15-
patch(urllib3=True)
9+
The ``urllib3`` integration is not enabled by default. Use either ``ddtrace-run``
10+
or ``import ddtrace.auto`` with ``DD_PATCH_MODULES`` or ``DD_TRACE_URLLIB3_ENABLED`` to enable it.
11+
``DD_PATCH_MODULES=urllib3 ddtrace-run python app.py`` or
12+
``DD_PATCH_MODULES=urllib3:true python app.py``::
1613
14+
import ddtrace.auto
1715
# use urllib3 like usual
1816
1917

ddtrace/contrib/internal/pytest/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
DDTRACE_HELP_MSG = "Enable tracing of pytest functions."
4141
NO_DDTRACE_HELP_MSG = "Disable tracing of pytest functions."
4242
DDTRACE_INCLUDE_CLASS_HELP_MSG = "Prepend 'ClassName.' to names of class-based tests."
43-
PATCH_ALL_HELP_MSG = "Call ddtrace.patch_all before running tests."
43+
PATCH_ALL_HELP_MSG = "Call ddtrace._patch_all before running tests."
4444

4545

4646
def is_enabled(config):
@@ -175,7 +175,7 @@ def patch_all(request):
175175
"""Patch all available modules for Datadog tracing when ddtrace-patch-all
176176
is specified in command or .ini.
177177
"""
178-
import ddtrace
179-
180178
if request.config.getoption("ddtrace-patch-all") or request.config.getini("ddtrace-patch-all"):
181-
ddtrace.patch_all()
179+
from ddtrace._monkey import _patch_all
180+
181+
_patch_all()

docs/api.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Tracing
99
1010
.. automodule:: ddtrace.auto
1111

12-
.. autofunction:: ddtrace.patch_all
13-
1412
.. autofunction:: ddtrace.patch
1513

1614
.. autoclass:: ddtrace.trace.Tracer
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
deprecations:
3+
- |
4+
tracer: ``patch_all`` is deprecated. As an alternative to ``patch_all``, you can use ``import ddtrace.auto`` along with ``DD_PATCH_MODULES`` if specific module patching is necessary.

tests/appsec/iast/fixtures/entrypoint/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ def add_test():
2222

2323

2424
def create_app_patch_all():
25-
from ddtrace import patch_all
25+
import ddtrace.auto # noqa: F401
2626

27-
patch_all()
2827
app = Flask(__name__)
2928
return app
3029

tests/commands/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_dogstatsd_client_env_url_path(self):
144144

145145
def test_patch_modules_from_env(self):
146146
"""
147-
DD_PATCH_MODULES overrides the defaults for patch_all()
147+
DD_PATCH_MODULES overrides the defaults for _patch_all()
148148
"""
149149
with self.override_env(
150150
env=dict(

tests/contrib/algoliasearch/test.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ddtrace import config
2-
from ddtrace import patch_all
2+
from ddtrace._monkey import _patch_all
33
from ddtrace.contrib.internal.algoliasearch.patch import algoliasearch_version
44
from ddtrace.contrib.internal.algoliasearch.patch import patch
55
from ddtrace.contrib.internal.algoliasearch.patch import unpatch
@@ -156,8 +156,9 @@ def test_patch_unpatch(self):
156156
assert len(spans) == 1
157157

158158
def test_patch_all_auto_enable(self):
159-
patch_all()
159+
_patch_all()
160160
Pin._override(self.index, tracer=self.tracer)
161+
161162
self.perform_search("test search")
162163

163164
spans = self.get_spans()
@@ -178,7 +179,7 @@ def test_user_specified_service_default(self):
178179
When a service name is specified by the user
179180
The algoliasearch integration shouldn't use it as the service name
180181
"""
181-
patch_all()
182+
_patch_all()
182183
Pin._override(self.index, tracer=self.tracer)
183184
self.perform_search("test search")
184185
spans = self.get_spans()
@@ -194,7 +195,7 @@ def test_user_specified_service_v0(self):
194195
When a service name is specified by the user
195196
The algoliasearch integration shouldn't use it as the service name
196197
"""
197-
patch_all()
198+
_patch_all()
198199
Pin._override(self.index, tracer=self.tracer)
199200
self.perform_search("test search")
200201
spans = self.get_spans()
@@ -210,7 +211,7 @@ def test_user_specified_service_v1(self):
210211
In the v1 service name schema, services default to $DD_SERVICE,
211212
so make sure that is used and not the v0 schema 'algoliasearch'
212213
"""
213-
patch_all()
214+
_patch_all()
214215
Pin._override(self.index, tracer=self.tracer)
215216
self.perform_search("test search")
216217
spans = self.get_spans()
@@ -222,7 +223,7 @@ def test_user_specified_service_v1(self):
222223

223224
@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v0"))
224225
def test_span_name_v0_schema(self):
225-
patch_all()
226+
_patch_all()
226227
Pin._override(self.index, tracer=self.tracer)
227228
self.perform_search("test search")
228229
spans = self.get_spans()
@@ -234,7 +235,7 @@ def test_span_name_v0_schema(self):
234235

235236
@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v1"))
236237
def test_span_name_v1_schema(self):
237-
patch_all()
238+
_patch_all()
238239
Pin._override(self.index, tracer=self.tracer)
239240
self.perform_search("test search")
240241
spans = self.get_spans()

tests/contrib/django/test_django.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,8 @@ def test_django_use_handler_resource_format_env(client, test_spans):
21202120
"python",
21212121
"-c",
21222122
(
2123-
"from ddtrace import config, patch_all; patch_all(); "
2123+
"from ddtrace import config; "
2124+
"from ddtrace._monkey import _patch_all; _patch_all(); "
21242125
"import django; "
21252126
"assert config.django.use_handler_resource_format; print('Test success')"
21262127
),
@@ -2139,7 +2140,8 @@ def test_django_use_handler_with_url_name_resource_format_env(client, test_spans
21392140
"python",
21402141
"-c",
21412142
(
2142-
"from ddtrace import config, patch_all; patch_all(); "
2143+
"from ddtrace import config; "
2144+
"from ddtrace._monkey import _patch_all; _patch_all(); "
21432145
"import django; "
21442146
"assert config.django.use_handler_with_url_name_resource_format; print('Test success')"
21452147
),
@@ -2219,7 +2221,8 @@ def test_django_use_legacy_resource_format_env(client, test_spans):
22192221
"python",
22202222
"-c",
22212223
(
2222-
"from ddtrace import config, patch_all; patch_all(); "
2224+
"from ddtrace import config; "
2225+
"from ddtrace._monkey import _patch_all; _patch_all(); "
22232226
"import django; "
22242227
"assert config.django.use_legacy_resource_format; print('Test success')"
22252228
),

tests/contrib/psycopg/test_psycopg_snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_connect_traced_via_env(run_python_code_in_subprocess):
7272
import ddtrace
7373
from tests.contrib.config import POSTGRES_CONFIG
7474
75-
ddtrace.patch_all()
75+
ddtrace._monkey._patch_all()
7676
7777
conn = psycopg.connect(**POSTGRES_CONFIG)
7878
assert conn

tests/contrib/psycopg2/test_psycopg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ def test_manual_wrap_extension_adapt(self):
247247

248248
@skipIf(PSYCOPG2_VERSION < (2, 7), "quote_ident not available in psycopg2<2.7")
249249
def test_manual_wrap_extension_quote_ident(self):
250-
from ddtrace import patch_all
250+
from ddtrace._monkey import _patch_all
251251

252-
patch_all()
252+
_patch_all()
253253
from psycopg2.extensions import quote_ident
254254

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

498498
@skipIf(PSYCOPG2_VERSION < (2, 7), "quote_ident not available in psycopg2<2.7")
499499
def test_manual_wrap_extension_quote_ident_standalone():
500-
from ddtrace import patch_all
500+
from ddtrace._monkey import _patch_all
501501

502-
patch_all()
502+
_patch_all()
503503
from psycopg2.extensions import quote_ident
504504

505505
# NOTE: this will crash if it doesn't work.

tests/contrib/psycopg2/test_psycopg_snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_connect_traced_via_env(run_python_code_in_subprocess):
5454
import ddtrace
5555
from tests.contrib.config import POSTGRES_CONFIG
5656
57-
ddtrace.patch_all()
57+
ddtrace._monkey._patch_all()
5858
5959
conn = psycopg2.connect(**POSTGRES_CONFIG)
6060
assert conn

tests/contrib/starlette/test_starlette.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,17 @@ def test_incorrect_patching(run_python_code_in_subprocess):
499499
from starlette.testclient import TestClient
500500
import sqlalchemy
501501
502-
from ddtrace import patch_all
502+
from ddtrace._monkey import _patch_all
503503
504504
505505
from tests.contrib.starlette.app import get_app
506506
507507
engine = sqlalchemy.create_engine("sqlite:///test.db")
508508
app = get_app(engine)
509509
510-
# Calling patch_all late
510+
# Calling _patch_all late
511511
# DEV: The test client uses `requests` so we want to ignore them for this scenario
512-
patch_all(requests=False, http=False)
512+
_patch_all(requests=False, http=False)
513513
with TestClient(app) as test_client:
514514
r = test_client.get("/200")
515515

0 commit comments

Comments
 (0)