-
Notifications
You must be signed in to change notification settings - Fork 413
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
fix(opentelemetry): resolve circular imports #9929
Conversation
26b2540
to
79e6dc8
Compare
|
d8e9b91
to
190e4e8
Compare
BenchmarksBenchmark execution time: 2024-07-25 19:11:01 Comparing candidate commit 59a3247 in PR branch Found 1 performance improvements and 3 performance regressions! Performance is the same for 210 metrics, 2 unstable metrics. scenario:otelspan-start
scenario:otelspan-start-finish
scenario:otelspan-start-finish-telemetry
|
Do we need to get rid of |
I renamed ddtrace/opentelemetry/init.py exposes the ddtrace TracerProvider to users. Users are expected to use this class to enable opentelemetry support. The ddtrace TracerProvider imports from the This is definitely a hacky workaround. Ideally we should reach out to opentelemetry-python maintainers and ask them to revisit this change: open-telemetry/opentelemetry-python@52abb61 |
190e4e8
to
459fc76
Compare
Datadog ReportBranch report: ✅ 0 Failed, 118157 Passed, 60122 Skipped, 3h 54m 26.84s Total duration (21m 55.52s time saved) |
459fc76
to
2b1543a
Compare
2b1543a
to
05d8b27
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9929 +/- ##
===========================================
- Coverage 27.08% 10.55% -16.54%
===========================================
Files 1385 1368 -17
Lines 129571 127970 -1601
===========================================
- Hits 35093 13503 -21590
- Misses 94478 114467 +19989 ☔ View full report in Codecov by Sentry. |
I still see the original file there. |
05d8b27
to
3e03800
Compare
Ahh I must've reverted a change during a rebase. The file is removed: https://github.com/DataDog/dd-trace-py/pull/9929/files#diff-62a0b204575d0586747747152ea77d0466cf05d05ca2bd5f4327478ea9583682. For consistency we can also move |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating codeowners and suitespec too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some NB nits. Thanks for the great description!
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.9 2.9
# Navigate to the new working tree
cd .worktrees/backport-2.9
# Create a new branch
git switch --create backport-9929-to-2.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 3237351b4a35dfd52e3d3d05c81bfe5b51279372
# Push it to GitHub
git push --set-upstream origin backport-9929-to-2.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.9 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.10 2.10
# Navigate to the new working tree
cd .worktrees/backport-2.10
# Create a new branch
git switch --create backport-9929-to-2.10
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 3237351b4a35dfd52e3d3d05c81bfe5b51279372
# Push it to GitHub
git push --set-upstream origin backport-9929-to-2.10
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.10 Then, create a pull request where the |
Resolves: #9917 ## Background ddtrace replaces the default opentelemetry RuntimeContext with a datadog-aware runtime context. This allows the ddtrace library to correlate datadog and opentelemetry spans in the same trace. The OpenTelemetry API exposes an [entrypoint](https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/context/__init__.py#L48) for overriding the default RuntimeContext ([ContextVarsRuntimeContext](https://github.com/open-telemetry/opentelemetry-python/blob/v1.25.0/opentelemetry-api/src/opentelemetry/context/contextvars_context.py#L19)). The ddtrace library implements this [entrypoint](https://github.com/DataDog/dd-trace-py/blob/v2.10.0rc3/pyproject.toml#L56) and overrides `ContextVarsRuntimeContext` with [DDRuntimeContext](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/opentelemetry/_context.py#L26). ### When `DD_TRACE_OTEL_ENABLED=True` and `opentelemetry-api==1.25.0` is used the following occurs: -> ddtrace imports [set_tracer_provider](https://github.com/DataDog/dd-trace-py/blob/9cce458b5ffeabe1dfee22687c89b8635856b9b6/ddtrace/bootstrap/preload.py#L107) from `openelemetry.trace` -> On import [openelemetry.trace](https://github.com/open-telemetry/opentelemetry-python/blob/3f95781a64cc6b897e6e34df907cd187c1e4c2f4/opentelemetry-api/src/opentelemetry/trace/__init__.py#L86) the `opentelemetry.context` package is imported. -> On [import opentelemetry.context](https://github.com/open-telemetry/opentelemetry-python/blob/3f95781a64cc6b897e6e34df907cd187c1e4c2f4/opentelemetry-api/src/opentelemetry/context/__init__.py#L69) the `_RUNTIME_CONTEXT` object is initialized this calls the `_load_runtime_context` method. -> On [_load_runtime_context](https://github.com/open-telemetry/opentelemetry-python/blob/3f95781a64cc6b897e6e34df907cd187c1e4c2f4/opentelemetry-api/src/opentelemetry/context/__init__.py#L47) the DDRuntimeContext class is imported from `ddtrace.opentelemetry._context`. -> On [import ddtrace.opentelemetry._context](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/opentelemetry/_context.py#L6) the `opentelemetry.trace` package is loaded. This packaged is used in DDRuntimeContext to access [types and functions](https://github.com/DataDog/dd-trace-py/blob/9cce458b5ffeabe1dfee22687c89b8635856b9b6/ddtrace/opentelemetry/_context.py#L9) used to generate OpenTelemetry compatible spans. -> On import `opentelemetry.trace` the `opentelemetry.context.__init__ `module is indirectly imported. -> BOOM circular import -> `_load_runtime_context` handles the raised exception,`DDRuntimeContext` is not set, and the default `ContextVarsRuntimeContext` is used. This breaks span parenting and ddtrace opentelemetry support. ### What changed in opentelemetry-api==1.25.0? Previously, `_load_runtime_context` was lazily loaded and `_RuntimeContext` was initialized when the first span was created. This changed in this commit: open-telemetry/opentelemetry-python@52abb61. With this change the _RuntimeContext is ALWAYS initialized as a side effect of importing the `opentelemetry.trace` package. ## Description Since `_load_runtime_context` is no longer lazy loaded we must ensure `opentelemetry.context` is NOT imported as a side effect of importing `DDRuntimeContext`. We achieve this behavior by moving the `ddtrace.opentelemetry._context.py` module and inlining problematic imports. ## Regression Test Run the ddtrace opentelemetry testsuite with `opentelemetry-api==1.25.0` (currently we use 1.22.0). This should be sufficient to reproduce this bug. ## Checklist - [x] 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 - [x] 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)
Backport #9929 ## 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)
Resolves: #9917
Background
ddtrace replaces the default opentelemetry RuntimeContext with a datadog-aware runtime context. This allows the ddtrace library to correlate datadog and opentelemetry spans in the same trace.
The OpenTelemetry API exposes an entrypoint for overriding the default RuntimeContext (ContextVarsRuntimeContext). The ddtrace library implements this entrypoint and overrides
ContextVarsRuntimeContext
with DDRuntimeContext.When
DD_TRACE_OTEL_ENABLED=True
andopentelemetry-api==1.25.0
is used the following occurs:-> ddtrace imports set_tracer_provider from
openelemetry.trace
-> On import openelemetry.trace the
opentelemetry.context
package is imported.-> On import opentelemetry.context the
_RUNTIME_CONTEXT
object is initialized this calls the_load_runtime_context
method.-> On _load_runtime_context the DDRuntimeContext class is imported from
ddtrace.opentelemetry._context
.-> On import ddtrace.opentelemetry._context the
opentelemetry.trace
package is loaded. This packaged is used in DDRuntimeContext to access types and functions used to generate OpenTelemetry compatible spans.-> On import
opentelemetry.trace
theopentelemetry.context.__init__
module is indirectly imported.-> BOOM circular import
->
_load_runtime_context
handles the raised exception,DDRuntimeContext
is not set, and the defaultContextVarsRuntimeContext
is used. This breaks span parenting and ddtrace opentelemetry support.What changed in opentelemetry-api==1.25.0?
Previously,
_load_runtime_context
was lazily loaded and_RuntimeContext
was initialized when the first span was created. This changed in this commit: open-telemetry/opentelemetry-python@52abb61. With this change the _RuntimeContext is ALWAYS initialized as a side effect of importing theopentelemetry.trace
package.Description
Since
_load_runtime_context
is no longer lazy loaded we must ensureopentelemetry.context
is NOT imported as a side effect of importingDDRuntimeContext
. We achieve this behavior by moving theddtrace.opentelemetry._context.py
module and inlining problematic imports.Regression Test
Run the ddtrace opentelemetry testsuite with
opentelemetry-api==1.25.0
(currently we use 1.22.0). This should be sufficient to reproduce this bug.Checklist
Reviewer Checklist