Skip to content

Commit

Permalink
initial find and replace along with integration docs mods
Browse files Browse the repository at this point in the history
  • Loading branch information
ZStriker19 committed Jan 13, 2025
1 parent a58f139 commit fb33c53
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 66 deletions.
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 @@ -23,7 +23,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 .pin import Pin # noqa: E402
Expand Down Expand Up @@ -54,7 +54,7 @@

__all__ = [
"patch",
"patch_all",
"_patch_all",
"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 @@ -6,11 +6,13 @@
from wrapt.importer import when_imported

from ddtrace.appsec import load_common_appsec_modules
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 @@ -205,6 +207,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
"""Automatically patches all available modules.
Expand All @@ -215,7 +227,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
9 changes: 5 additions & 4 deletions ddtrace/contrib/kombu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""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
Expand All @@ -9,9 +10,9 @@
Without enabling distributed tracing, spans within a trace generated by the kombu integration might be dropped
without the whole trace being dropped.
::
from ddtrace import Pin, patch
Run with `DD_PATCH_MODULES=kombu:true`::
import ddtrace.auto
from ddtrace import Pin
import kombu
# If not patched yet, you can patch kombu specifically
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/contrib/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
DDTRACE_HELP_MSG = "Enable tracing of pytest functions."
NO_DDTRACE_HELP_MSG = "Disable tracing of pytest functions."
DDTRACE_INCLUDE_CLASS_HELP_MSG = "Prepend 'ClassName.' to names of class-based tests."
PATCH_ALL_HELP_MSG = "Call ddtrace.patch_all before running tests."
PATCH_ALL_HELP_MSG = "Call ddtrace._patch_all before running tests."


def is_enabled(config):
Expand Down Expand Up @@ -156,11 +156,11 @@ def ddtracer():


@pytest.fixture(scope="session", autouse=True)
def patch_all(request):
def _patch_all(request):
"""Patch all available modules for Datadog tracing when ddtrace-patch-all
is specified in command or .ini.
"""
import ddtrace

if request.config.getoption("ddtrace-patch-all") or request.config.getini("ddtrace-patch-all"):
ddtrace.patch_all()
ddtrace._patch_all()
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
8 changes: 1 addition & 7 deletions ddtrace/contrib/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
The integration is not enabled automatically when using
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`.
Use :func:`patch()<ddtrace.patch>` to manually enable the integration::
from ddtrace import patch, patch_all
patch(snowflake=True)
patch_all(snowflake=True)
or the ``DD_TRACE_SNOWFLAKE_ENABLED=true`` to enable it with ``ddtrace-run``.
Use environment variable `DD_PATCH_MODULES:snowflake:true` to manually enable the integration::
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
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.algoliasearch.patch import algoliasearch_version
from ddtrace.contrib.algoliasearch.patch import patch
from ddtrace.contrib.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 @@ -2017,7 +2017,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 @@ -2036,7 +2036,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 @@ -2116,7 +2116,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

0 comments on commit fb33c53

Please sign in to comment.