Skip to content

Commit

Permalink
chore(integrations): remove required_modules from some integrations (#…
Browse files Browse the repository at this point in the history
…11719)

Since ddtrace
[v1.9.0](856fa23)
modules are only patched after import. There is no need to check whether
a module exists in `ddtrace.contrib.integration_name.__init__`. If a
library is not installed, it won't be patched.

`require_modules` should only be used when a ddtrace integration
requires additional libraries (for example is web_integration can only
be enabled if web_integration_driver is also installed). Right now there
is no integrations that have such a dependency.

With this change objects exposed in
`ddtrace.contrib.<integration_name>.__all__` will always be available.
Accessing these objects will not raise an attributeerror and crash an
application: [ex](#11603).

## Checklist
- [ ] 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
- [ ] 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)
  • Loading branch information
mabdinur authored Jan 2, 2025
1 parent 6613185 commit e1eccbd
Show file tree
Hide file tree
Showing 78 changed files with 753 additions and 1,044 deletions.
23 changes: 10 additions & 13 deletions ddtrace/contrib/aiobotocore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,19 @@
Default: ``False``
"""
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["aiobotocore.client"]
# Required to allow users to import from `ddtrace.contrib.aiobotocore.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aiobotocore.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

# Expose public methods
from ddtrace.contrib.internal.aiobotocore.patch import get_version
from ddtrace.contrib.internal.aiobotocore.patch import patch
# Expose public methods
from ddtrace.contrib.internal.aiobotocore.patch import get_version
from ddtrace.contrib.internal.aiobotocore.patch import patch

__all__ = ["patch", "get_version"]

__all__ = ["patch", "get_version"]
27 changes: 12 additions & 15 deletions ddtrace/contrib/aiohttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,21 @@ async def home_handler(request):
:ref:`All HTTP tags <http-tagging>` are supported for this integration.
"""
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["aiohttp"]
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.aiohttp.middlewares import trace_app
from ddtrace.contrib.internal.aiohttp.patch import get_version
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.aiohttp.middlewares import trace_app
from ddtrace.contrib.internal.aiohttp.patch import get_version

# Expose public methods
from ddtrace.contrib.internal.aiohttp.patch import patch
from ddtrace.contrib.internal.aiohttp.patch import unpatch
# Expose public methods
from ddtrace.contrib.internal.aiohttp.patch import patch
from ddtrace.contrib.internal.aiohttp.patch import unpatch

__all__ = ["patch", "unpatch", "trace_app", "get_version"]

__all__ = ["patch", "unpatch", "trace_app", "get_version"]
23 changes: 10 additions & 13 deletions ddtrace/contrib/aiohttp_jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
from ddtrace import patch
patch(aiohttp_jinja2=True)
"""
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["aiohttp_jinja2"]
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

from ddtrace.contrib.internal.aiohttp_jinja2.patch import get_version
from ddtrace.contrib.internal.aiohttp_jinja2.patch import patch
from ddtrace.contrib.internal.aiohttp_jinja2.patch import unpatch
from ddtrace.contrib.internal.aiohttp_jinja2.patch import get_version
from ddtrace.contrib.internal.aiohttp_jinja2.patch import patch
from ddtrace.contrib.internal.aiohttp_jinja2.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]

__all__ = ["patch", "unpatch", "get_version"]
22 changes: 9 additions & 13 deletions ddtrace/contrib/aiomysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,18 @@
await cur.execute("SELECT 6*7 AS the_answer;")
"""

from ddtrace.internal.utils.importlib import require_modules

# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

required_modules = ["aiomysql"]

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.aiomysql.patch import get_version
from ddtrace.contrib.internal.aiomysql.patch import patch
from ddtrace.contrib.internal.aiomysql.patch import unpatch

from ddtrace.contrib.internal.aiomysql.patch import get_version
from ddtrace.contrib.internal.aiomysql.patch import patch
from ddtrace.contrib.internal.aiomysql.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]
__all__ = ["patch", "unpatch", "get_version"]
21 changes: 9 additions & 12 deletions ddtrace/contrib/aiopg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
# Use a pin to specify metadata related to this connection
Pin.override(db, service='postgres-users')
"""
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["aiopg"]
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aiohttp.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

from ddtrace.contrib.internal.aiopg.patch import get_version
from ddtrace.contrib.internal.aiopg.patch import patch
from ddtrace.contrib.internal.aiopg.patch import get_version
from ddtrace.contrib.internal.aiopg.patch import patch

__all__ = ["patch", "get_version"]

__all__ = ["patch", "get_version"]
23 changes: 9 additions & 14 deletions ddtrace/contrib/aioredis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,17 @@
myaioredis = aioredis.Aioredis()
Pin.override(myaioredis, service="myaioredis")
"""
from ddtrace.internal.utils.importlib import require_modules # noqa:E402
import warnings as _w # noqa: E402


required_modules = ["aioredis"]
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
# Required to allow users to import from `ddtrace.contrib.aioredis.patch` directly
from . import patch as _ # noqa: F401, I001

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aioredis.patch` directly
import warnings as _w
from ddtrace.contrib.internal.aioredis.patch import get_version # noqa: E402
from ddtrace.contrib.internal.aioredis.patch import patch # noqa: E402
from ddtrace.contrib.internal.aioredis.patch import unpatch # noqa: E402

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

from ddtrace.contrib.internal.aioredis.patch import get_version
from ddtrace.contrib.internal.aioredis.patch import patch
from ddtrace.contrib.internal.aioredis.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]
__all__ = ["patch", "unpatch", "get_version"]
22 changes: 9 additions & 13 deletions ddtrace/contrib/algoliasearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@
.. __: https://www.algolia.com
"""

from ddtrace.internal.utils.importlib import require_modules

# Required to allow users to import from `ddtrace.contrib.algoliasearch.patch` directly
import warnings as _w

required_modules = ["algoliasearch", "algoliasearch.version"]

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.algoliasearch.patch` directly
import warnings as _w
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.algoliasearch.patch import get_version
from ddtrace.contrib.internal.algoliasearch.patch import patch
from ddtrace.contrib.internal.algoliasearch.patch import unpatch

from ddtrace.contrib.internal.algoliasearch.patch import get_version
from ddtrace.contrib.internal.algoliasearch.patch import patch
from ddtrace.contrib.internal.algoliasearch.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]
__all__ = ["patch", "unpatch", "get_version"]
23 changes: 10 additions & 13 deletions ddtrace/contrib/anthropic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,19 @@
Pin.override(anthropic, service="my-anthropic-service")
""" # noqa: E501
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["anthropic"]
# Required to allow users to import from `ddtrace.contrib.anthropic.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.anthropic.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

from ddtrace.contrib.internal.anthropic.patch import get_version
from ddtrace.contrib.internal.anthropic.patch import patch
from ddtrace.contrib.internal.anthropic.patch import unpatch
from ddtrace.contrib.internal.anthropic.patch import get_version
from ddtrace.contrib.internal.anthropic.patch import patch
from ddtrace.contrib.internal.anthropic.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]

__all__ = ["patch", "unpatch", "get_version"]
20 changes: 8 additions & 12 deletions ddtrace/contrib/aredis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,17 @@ async def example():
await client.get("my-key")
"""

from ddtrace.internal.utils.importlib import require_modules

# Required to allow users to import from `ddtrace.contrib.aredis.patch` directly
import warnings as _w

required_modules = ["aredis", "aredis.client"]

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.aredis.patch` directly
import warnings as _w
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.aredis.patch import get_version
from ddtrace.contrib.internal.aredis.patch import patch

from ddtrace.contrib.internal.aredis.patch import get_version
from ddtrace.contrib.internal.aredis.patch import patch

__all__ = ["patch", "get_version"]
__all__ = ["patch", "get_version"]
16 changes: 4 additions & 12 deletions ddtrace/contrib/asgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ def handle_request(scope, send):
.. __: https://asgi.readthedocs.io/
"""

from ddtrace.internal.utils.importlib import require_modules

from ddtrace.contrib.internal.asgi.middleware import TraceMiddleware
from ddtrace.contrib.internal.asgi.middleware import get_version
from ddtrace.contrib.internal.asgi.middleware import span_from_scope

required_modules = []

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.asgi.patch` directly
from . import middleware as _ # noqa: F401, I001

from ddtrace.contrib.internal.asgi.middleware import TraceMiddleware
from ddtrace.contrib.internal.asgi.middleware import get_version
from ddtrace.contrib.internal.asgi.middleware import span_from_scope

__all__ = ["TraceMiddleware", "span_from_scope", "get_version"]
__all__ = ["TraceMiddleware", "span_from_scope", "get_version"]
35 changes: 15 additions & 20 deletions ddtrace/contrib/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
This integration provides context management for tracing the execution flow
of concurrent execution of ``asyncio.Task``.
"""
from ddtrace.internal.utils.importlib import require_modules
# Required to allow users to import from `ddtrace.contrib.asyncio.patch` directly
# Expose public methods
import warnings as _w # noqa:E402


required_modules = ["asyncio"]
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace._trace.provider import DefaultContextProvider
from ddtrace.contrib.internal.asyncio.helpers import ensure_future
from ddtrace.contrib.internal.asyncio.helpers import run_in_executor
from ddtrace.contrib.internal.asyncio.helpers import set_call_context
from ddtrace.contrib.internal.asyncio.patch import get_version
from ddtrace.contrib.internal.asyncio.patch import patch
from ddtrace.contrib.internal.asyncio.patch import unpatch # noqa: F401

with require_modules(required_modules) as missing_modules:
if not missing_modules:
from ddtrace._trace.provider import DefaultContextProvider

context_provider = DefaultContextProvider()
context_provider = DefaultContextProvider()

# Required to allow users to import from `ddtrace.contrib.asyncio.patch` directly
# Expose public methods
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
from ddtrace.contrib.internal.asyncio.helpers import ensure_future
from ddtrace.contrib.internal.asyncio.helpers import run_in_executor
from ddtrace.contrib.internal.asyncio.helpers import set_call_context
from ddtrace.contrib.internal.asyncio.patch import get_version
from ddtrace.contrib.internal.asyncio.patch import patch
from ddtrace.contrib.internal.asyncio.patch import unpatch # noqa: F401

__all__ = ["context_provider", "set_call_context", "ensure_future", "run_in_executor", "patch", "get_version"]
__all__ = ["context_provider", "set_call_context", "ensure_future", "run_in_executor", "patch", "get_version"]
23 changes: 10 additions & 13 deletions ddtrace/contrib/asyncpg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,19 @@
conn = asyncpg.connect("postgres://localhost:5432")
Pin.override(conn, service="custom-service")
"""
from ddtrace.internal.utils.importlib import require_modules


required_modules = ["asyncpg"]
# Required to allow users to import from `ddtrace.contrib.asyncpg.patch` directly
import warnings as _w

with require_modules(required_modules) as missing_modules:
if not missing_modules:
# Required to allow users to import from `ddtrace.contrib.asyncpg.patch` directly
import warnings as _w

with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001
with _w.catch_warnings():
_w.simplefilter("ignore", DeprecationWarning)
from . import patch as _ # noqa: F401, I001

from ddtrace.contrib.internal.asyncpg.patch import get_version
from ddtrace.contrib.internal.asyncpg.patch import patch
from ddtrace.contrib.internal.asyncpg.patch import unpatch
from ddtrace.contrib.internal.asyncpg.patch import get_version
from ddtrace.contrib.internal.asyncpg.patch import patch
from ddtrace.contrib.internal.asyncpg.patch import unpatch

__all__ = ["patch", "unpatch", "get_version"]

__all__ = ["patch", "unpatch", "get_version"]
Loading

0 comments on commit e1eccbd

Please sign in to comment.