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(integrations): remove required_modules from integrations #11719

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mabdinur
Copy link
Contributor

@mabdinur mabdinur commented Dec 12, 2024

Since ddtrace v1.9.0 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 the web3 integration can only be enabled if web3_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.

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
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

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 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

Copy link
Contributor

github-actions bot commented Dec 12, 2024

CODEOWNERS have been resolved as:

ddtrace/contrib/aiobotocore/__init__.py                                 @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/aiohttp/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/aiohttp_jinja2/__init__.py                              @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/aiomysql/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/aiopg/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/aioredis/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/algoliasearch/__init__.py                               @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/anthropic/__init__.py                                   @DataDog/ml-observability
ddtrace/contrib/aredis/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/asgi/__init__.py                                        @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/asyncio/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/asyncpg/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/avro/__init__.py                                        @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/boto/__init__.py                                        @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/botocore/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/bottle/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/cassandra/__init__.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/celery/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/cherrypy/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/consul/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/coverage/__init__.py                                    @DataDog/ci-app-libraries
ddtrace/contrib/django/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/dogpile_cache/__init__.py                               @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/dramatiq/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/falcon/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/fastapi/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/flask/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/flask_cache/__init__.py                                 @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/freezegun/__init__.py                                   @DataDog/ci-app-libraries
ddtrace/contrib/futures/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/gevent/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/google_generativeai/__init__.py                         @DataDog/ml-observability
ddtrace/contrib/graphql/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/grpc/__init__.py                                        @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/httplib/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/httpx/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/jinja2/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/kafka/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/langchain/__init__.py                                   @DataDog/ml-observability
ddtrace/contrib/logbook/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/logging/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/loguru/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/mako/__init__.py                                        @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/mariadb/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/molten/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/mongoengine/__init__.py                                 @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/mysql/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/mysqldb/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/openai/__init__.py                                      @DataDog/ml-observability
ddtrace/contrib/protobuf/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pylibmc/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pymemcache/__init__.py                                  @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pymongo/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pymysql/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pynamodb/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pyodbc/__init__.py                                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/pyramid/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/redis/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/rediscluster/__init__.py                                @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/requests/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/sanic/__init__.py                                       @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/selenium/__init__.py                                    @DataDog/ci-app-libraries
ddtrace/contrib/snowflake/__init__.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/sqlalchemy/__init__.py                                  @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/sqlite3/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/starlette/__init__.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/structlog/__init__.py                                   @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/subprocess/__init__.py                                  @DataDog/asm-python
ddtrace/contrib/tornado/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/unittest/__init__.py                                    @DataDog/ci-app-libraries
ddtrace/contrib/urllib/__init__.py                                      @DataDog/asm-python
ddtrace/contrib/urllib3/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/vertexai/__init__.py                                    @DataDog/ml-observability
ddtrace/contrib/vertica/__init__.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/contrib/webbrowser/__init__.py                                  @DataDog/asm-python
ddtrace/contrib/yaaredis/__init__.py                                    @DataDog/apm-core-python @DataDog/apm-idm-python
docs/conf.py                                                            @DataDog/python-guild
templates/integration/__init__.py                                       @DataDog/apm-core-python

@mabdinur mabdinur force-pushed the munir/get-rid-of-require-modules-integrations branch 2 times, most recently from 2634736 to 2a29668 Compare December 13, 2024 16:08
@mabdinur mabdinur changed the title chore(integrations): chore(integrations): remove required_modules from some integrations Dec 13, 2024
@pr-commenter
Copy link

pr-commenter bot commented Dec 13, 2024

Benchmarks

Benchmark execution time: 2024-12-27 20:30:33

Comparing candidate commit a523807 in PR branch munir/get-rid-of-require-modules-integrations with baseline commit 5cee25e in branch main.

Found 5 performance improvements and 0 performance regressions! Performance is the same for 389 metrics, 2 unstable metrics.

scenario:iast_aspects-ospathdirname_aspect

  • 🟩 execution_time [-550.874ns; -480.865ns] or [-13.266%; -11.580%]

scenario:iast_aspects-ospathsplit_aspect

  • 🟩 execution_time [-411.527ns; -348.536ns] or [-9.667%; -8.187%]

scenario:iast_aspects-ospathsplitdrive_aspect

  • 🟩 execution_time [-290.053ns; -253.499ns] or [-9.912%; -8.662%]

scenario:iast_aspects-ospathsplitext_aspect

  • 🟩 execution_time [-469.777ns; -415.800ns] or [-11.525%; -10.201%]

scenario:iast_aspects-replace_aspect

  • 🟩 execution_time [-576.228ns; -509.589ns] or [-7.998%; -7.073%]

@mabdinur mabdinur force-pushed the munir/get-rid-of-require-modules-integrations branch from 2a29668 to 7e13140 Compare December 16, 2024 16:42
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented Dec 16, 2024

Datadog Report

Branch report: munir/get-rid-of-require-modules-integrations
Commit report: 85296e1
Test service: dd-trace-py

✅ 0 Failed, 55 Passed, 1413 Skipped, 1m 17.72s Total duration (32m 37.73s time saved)

@mabdinur mabdinur marked this pull request as ready for review December 19, 2024 23:13
@mabdinur mabdinur requested review from a team as code owners December 19, 2024 23:13
@mabdinur mabdinur requested a review from liashenko December 19, 2024 23:13
@mabdinur mabdinur enabled auto-merge (squash) December 20, 2024 16:36
@mabdinur mabdinur added the changelog/no-changelog A changelog entry is not required for this PR. label Dec 23, 2024
@mabdinur mabdinur changed the title chore(integrations): remove required_modules from some integrations chore(integrations): remove required_modules from integrations Dec 23, 2024
Copy link
Contributor

@erikayasuda erikayasuda left a comment

Choose a reason for hiding this comment

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

🙌

from ddtrace.contrib.internal.coverage.patch import get_version
from ddtrace.contrib.internal.coverage.patch import patch
from ddtrace.contrib.internal.coverage.patch import unpatch
log = get_logger(__name__)
Copy link
Contributor

Choose a reason for hiding this comment

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

NB: Just curious, do you know why we have this here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably some copy-pasta. It seems to have been there since the beginning of this file's history, and was already unused back then. 😛 We can probably remove it.

from ddtrace.contrib.internal.django.patch import unpatch

__all__ = ["patch", "unpatch", "_patch", "get_version"]
__all__ = ["patch", "unpatch", "_patch", "get_version"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we prepare to exclude _patch while we're doing this clean up, and deprecate it for 3.x?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly I think we could make patch, unpatch and get_version functions internal for all integrations. There's no good reason to make these operations public.

If we really need to expose an api for unpatching an integration (which I don't think we should) we could introduce one unpatch/unpatch_all method in _monkey.py. This is something we could discuss in the next guild meeting.

pyproject.toml Outdated Show resolved Hide resolved
docs/conf.py Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog A changelog entry is not required for this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants