from ddtrace import patch_all
patch_all()
import aredis
What is the result that you get?
failed to import ddtrace module 'ddtrace.contrib.aredis' when patching on import
Traceback (most recent call last):
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/_monkey.py", line 165, in on_import
imported_module = importlib.import_module(path)
File "/home/coder/.pyenv/versions/3.9.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/internal/module.py", line 211, in _exec_module
self.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/contrib/aredis/__init__.py", line 76, in <module>
from .patch import get_version
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/internal/module.py", line 211, in _exec_module
self.loader.exec_module(module)
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/contrib/aredis/patch.py", line 14, in <module>
from ..redis.asyncio_patch import _run_redis_command_async
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/internal/module.py", line 211, in _exec_module
self.loader.exec_module(module)
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/contrib/redis/__init__.py", line 75, in <module>
with require_modules(required_modules) as missing_modules:
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/internal/utils/importlib.py", line 20, in __init__
import_module(module)
File "/home/coder/.pyenv/versions/3.9.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/vendor/wrapt/importer.py", line 177, in _exec_module
notify_module_loaded(module)
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/vendor/wrapt/decorators.py", line 470, in _synchronized
return wrapped(*args, **kwargs)
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/vendor/wrapt/importer.py", line 136, in notify_module_loaded
hook(module)
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/_monkey.py", line 176, in on_import
imported_module.patch()
AttributeError: partially initialized module 'ddtrace.contrib.redis' has no attribute 'patch' (most likely due to a circular import)
Notice the frame about halfway through the trace:
File "/workspace/test/.venv/lib/python3.9/site-packages/ddtrace/contrib/aredis/patch.py", line 14, in <module>
from ..redis.asyncio_patch import _run_redis_command_async
Summary of problem
aredis
,aioredis
, andyaaredis
integrations import from theredis
integration.If for example
aredis
andredis
are both installed, a circular import occurs when importingaredis
after patching.This same error has been an issue before: https://github.com/DataDog/dd-trace-py/issues/5601#issuecomment-1516265155
and fixed before: https://github.com/DataDog/dd-trace-py/pull/5608, by moving the utils being imported from the
redis
integration into a commontrace_utils_redis
file.However a cross-integration import was added a few months ago, breaking it again: https://github.com/DataDog/dd-trace-py/blob/67c5ec7a4c397199014823f649a810c708a6113d/ddtrace/contrib/aredis/patch.py#L14-L16
https://github.com/DataDog/dd-trace-py/blob/v2.3.1/ddtrace/contrib/aredis/patch.py#L14-L16
Which version of dd-trace-py are you using?
2.3.1
Which version of pip are you using?
pip 23.3.1
python 3.9.9
Which libraries and their versions are you using?
pip freeze
:How can we reproduce your problem?
Minimal reproduction:
pip install ddtrace aredis redis
What is the result that you get?
Notice the frame about halfway through the trace:
What is the result that you expected?
No error thrown