DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
551 stars 413 forks source link

OpenTelemetry context API is broken #10744

Closed yann-combarnous closed 1 month ago

yann-combarnous commented 1 month ago

Summary of problem

Trying to import from ddtrace.opentelemetry import TracerProvider and getting the following error:

ImportError: cannot import name 'TracerProvider' from partially initialized module 'ddtrace.opentelemetry' (most likely due to a circular import)

Seems similar to #9917

Which version of dd-trace-py are you using?

2.12.2

Which version of pip are you using?

pip 24.1

Which libraries and their versions are you using?

`pip freeze` aiokafka==0.11.0 annotated-types==0.7.0 anyio==4.5.0 async-timeout==4.0.3 azure-core==1.31.0 azure-eventhub==5.12.1 bytecode==0.15.1 certifi==2024.8.30 cffi==1.17.1 cfgv==3.4.0 charset-normalizer==3.3.2 click==8.1.7 confluent-kafka==2.5.3 cryptography==43.0.1 ddtrace==2.12.2 Deprecated==1.2.14 distlib==0.3.8 dnspython==2.6.1 elastic-transport==8.15.0 elasticsearch==8.15.1 envier==0.5.2 fast-depends==2.4.11 faststream==0.5.23 filelock==3.16.1 h11==0.14.0 hvac==2.3.0 identify==2.6.1 idna==3.10 importlib_metadata==8.4.0 iniconfig==2.0.0 kafka==1.3.5 kafka-python==2.0.2 markdown-it-py==3.0.0 mdurl==0.1.2 msal==1.31.0 nodeenv==1.9.1 opentelemetry-api==1.27.0 opentelemetry-distro==0.48b0 opentelemetry-instrumentation==0.48b0 opentelemetry-sdk==1.27.0 opentelemetry-semantic-conventions==0.48b0 packaging==24.1 platformdirs==4.3.6 pluggy==1.5.0 pre-commit==3.8.0 protobuf==5.28.2 pycparser==2.22 pycron==3.0.0 pydantic==2.9.1 pydantic-mongo==2.3.0 pydantic_core==2.23.3 Pygments==2.18.0 PyJWT==2.9.0 pymongo==4.8.0 pytest==8.3.3 pytest-mock==3.14.0 python-dotenv==1.0.1 pytz==2024.2 PyYAML==6.0.2 redis==5.0.8 requests==2.32.3 rich==13.8.1 ruff==0.6.5 setuptools==75.1.0 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 taskiq==0.11.7 taskiq-dependencies==1.5.3 taskiq-faststream==0.2.0 typer==0.12.5 typing_extensions==4.12.2 tzdata==2024.1 urllib3==2.2.3 uvicorn==0.30.6 virtualenv==20.26.5 wrapt==1.16.0 xmltodict==0.13.0 zipp==3.20.2

How can we reproduce your problem?

What is the result that you get?

What is the result that you expected?

mabdinur commented 1 month ago

Hey @yann-combarnous,

Thanks opening this issue. I am able to reproduce this error with ddtrace<2.8.7 but not in more recent versions of the library. This fix might be missing an edge case. I was wondering if you could modify the reproduction below to use v2.12.2 (or provide a sample app) this will help us narrow down the issue.

Reproduction

  1. python3.10 -m venv .v && source .v/bin/activate
  2. pip install ddtrace==2.8.6 opentelemetry-api==1.27.0
  3. DD_TRACE_OTEL_ENABLED=true python -c "from ddtrace.opentelemetry import TracerProvider"

Error

Failed to load context: ddcontextvars_context, fallback to contextvars_context
Traceback (most recent call last):
  File ".v/lib/python3.10/site-packages/opentelemetry/context/__init__.py", line 52, in _load_runtime_context
    ).load()()
  File ".v/lib/python3.10/site-packages/importlib_metadata/__init__.py", line 211, in load
    module = import_module(match.group('module'))
  File "/Users/munirabdinur/.pyenv/versions/3.10.5/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "v/lib/python3.10/site-packages/ddtrace/internal/module.py", line 220, in _exec_module
    self.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File ".v/lib/python3.10/site-packages/ddtrace/opentelemetry/_context.py", line 6, in <module>
    from opentelemetry.trace import NonRecordingSpan as OtelNonRecordingSpan
  File "/.v/lib/python3.10/site-packages/ddtrace/internal/module.py", line 220, in _exec_module
    self.loader.exec_module(module)
  File ".v/lib/python3.10/site-packages/opentelemetry/trace/__init__.py", line 90, in <module>
    from opentelemetry.trace.propagation import (
  File ".v/lib/python3.10/site-packages/ddtrace/internal/module.py", line 220, in _exec_module
    self.loader.exec_module(module)
  File ".v/lib/python3.10/site-packages/opentelemetry/trace/propagation/__init__.py", line 16, in <module>
    from opentelemetry.context import create_key, get_value, set_value
ImportError: cannot import name 'create_key' from partially initialized module 'opentelemetry.context' (most likely due to a circular import) v/lib/python3.10/site-packages/opentelemetry/context/__init__.py)
mabdinur commented 1 month ago

@yann-combarnous closing this issue due to inactivity. Feel free to reopen this issue if you're still seeing this bug in the latest release of ddtrace 😄