Skip to content

fix(di): interaction with external wrapping contexts [backport 3.9] #13741

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

Merged
merged 2 commits into from
Jun 23, 2025

Conversation

github-actions[bot]
Copy link
Contributor

Backport c02a068 from #13586 to 3.9.

We fix an incompatibility within the interaction between Dynamic Instrumentation and external wrapping context (e.g. Code Origin). This was due to a loss of link between a function and its original code object due to a wrapping context being wrapped on a function not retrieved via the function discovery machinery. We make sure that any wrapping via wrapping context preserves the link between the function and its original code object. With the universal wrapping context mechanism, we don't have to worry about potential multiple wrapping, as bytecode is modified only once.

Refs: DEBUG-3970

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

We fix an incompatibility within the interaction between Dynamic
Instrumentation and external wrapping context (e.g. Code Origin). This
was due to a loss of link between a function and its original code
object due to a wrapping context being wrapped on a function not
retrieved via the function discovery machinery. We make sure that any
wrapping via wrapping context preserves the link between the function
and its original code object. With the universal wrapping context
mechanism, we don't have to worry about potential multiple wrapping, as
bytecode is modified only once.

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

(cherry picked from commit c02a068)
@github-actions github-actions bot added the Dynamic Instrumentation Dynamic Instrumentation/Live Debugger label Jun 23, 2025
@github-actions github-actions bot requested review from a team as code owners June 23, 2025 07:27
@P403n1x87 P403n1x87 closed this Jun 23, 2025
@P403n1x87 P403n1x87 reopened this Jun 23, 2025
@P403n1x87 P403n1x87 enabled auto-merge (squash) June 23, 2025 07:27
Copy link
Contributor Author

CODEOWNERS have been resolved as:

releasenotes/notes/fix-di-with-external-wrapping-context-e26b46a6fc7f6a47.yaml  @DataDog/apm-python
ddtrace/debugging/_function/discovery.py                                @DataDog/debugger-python
ddtrace/internal/utils/inspection.py                                    @DataDog/apm-core-python
ddtrace/internal/wrapping/context.py                                    @DataDog/apm-core-python
tests/debugging/conftest.py                                             @DataDog/debugger-python
tests/debugging/function/test_discovery.py                              @DataDog/debugger-python
tests/debugging/test_debugger_span_decoration.py                        @DataDog/debugger-python

Copy link
Contributor Author

github-actions bot commented Jun 23, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 279 ± 4 ms.

The average import time from base is: 279 ± 4 ms.

The import time difference between this PR and base is: 0.1 ± 0.2 ms.

The difference is not statistically significant (z = 0.70).

Import time breakdown

The following import paths have appeared:

ddtrace.auto 2.790 ms (1.00%)
ddtrace 2.790 ms (1.00%)
ddtrace._logger 2.790 ms (1.00%)
ddtrace.internal.telemetry 2.790 ms (1.00%)
ddtrace.settings._agent 2.790 ms (1.00%)
ddtrace.settings 2.790 ms (1.00%)
ddtrace.settings.integration 2.790 ms (1.00%)
ddtrace.vendor.debtcollector 2.790 ms (1.00%)
ddtrace.vendor 2.790 ms (1.00%)
ddtrace.internal.module 2.790 ms (1.00%)
ddtrace.internal.wrapping.context 2.790 ms (1.00%)
ddtrace.internal.utils.inspection 2.790 ms (1.00%)
ddtrace.internal.safety 2.482 ms (0.89%)
wrapt 2.153 ms (0.77%)
wrapt.__wrapt__ 0.981 ms (0.35%)
wrapt.wrappers 0.510 ms (0.18%)
wrapt._wrappers 0.283 ms (0.10%)
wrapt.decorators 0.427 ms (0.15%)
wrapt.arguments 0.121 ms (0.04%)
wrapt.importer 0.209 ms (0.07%)
wrapt.patches 0.180 ms (0.06%)
wrapt.weakrefs 0.154 ms (0.06%)
ddtrace.internal.utils.attrdict 0.142 ms (0.05%)

The following import paths have disappeared:

ddtrace.auto 4.294 ms (1.54%)
ddtrace 3.576 ms (1.28%)
ddtrace._logger 3.576 ms (1.28%)
ddtrace.internal.telemetry 3.576 ms (1.28%)
ddtrace.settings._agent 3.576 ms (1.28%)
ddtrace.settings 3.576 ms (1.28%)
ddtrace.settings.integration 3.576 ms (1.28%)
ddtrace.vendor.debtcollector 3.322 ms (1.19%)
ddtrace.vendor.debtcollector.moves 3.322 ms (1.19%)
wrapt 3.322 ms (1.19%)
wrapt.__wrapt__ 1.360 ms (0.49%)
wrapt.wrappers 0.574 ms (0.21%)
wrapt._wrappers 0.477 ms (0.17%)
wrapt.decorators 0.698 ms (0.25%)
wrapt.arguments 0.254 ms (0.09%)
wrapt.importer 0.341 ms (0.12%)
wrapt.patches 0.315 ms (0.11%)
wrapt.weakrefs 0.283 ms (0.10%)
ddtrace.internal.utils.attrdict 0.254 ms (0.09%)
ddtrace.bootstrap.sitecustomize 0.718 ms (0.26%)
ddtrace.bootstrap.preload 0.718 ms (0.26%)
ddtrace.internal.symbol_db.remoteconfig 0.718 ms (0.26%)
ddtrace.internal.symbol_db.symbols 0.718 ms (0.26%)
ddtrace.internal.utils.inspection 0.392 ms (0.14%)
ddtrace.internal.safety 0.327 ms (0.12%)

The following import paths have grown:

ddtrace.auto 2.719 ms (0.97%)
ddtrace 1.696 ms (0.61%)
ddtrace._logger 1.696 ms (0.61%)
ddtrace.internal.telemetry 1.696 ms (0.61%)
ddtrace.settings._agent 1.187 ms (0.43%)
ddtrace.settings 1.104 ms (0.40%)
ddtrace.settings.integration 1.104 ms (0.40%)
ddtrace.vendor.debtcollector 1.104 ms (0.40%)
ddtrace.vendor.debtcollector._utils 0.902 ms (0.32%)
ddtrace.vendor.debtcollector.removals 0.095 ms (0.03%)
ddtrace.vendor 0.086 ms (0.03%)
ddtrace.internal.module 0.086 ms (0.03%)
ddtrace.internal.wrapping.context 0.086 ms (0.03%)
ddtrace.vendor.debtcollector.moves 0.021 ms (0.01%)
socket 0.083 ms (0.03%)
array 0.083 ms (0.03%)
ddtrace.internal.telemetry.writer 0.410 ms (0.15%)
http.client 0.410 ms (0.15%)
ssl 0.330 ms (0.12%)
_ssl 0.330 ms (0.12%)
ddtrace.settings._otel_remapper 0.100 ms (0.04%)
ddtrace.bootstrap.sitecustomize 1.023 ms (0.37%)
ddtrace.appsec._common_module_patches 0.852 ms (0.31%)
ddtrace.appsec._asm_request_context 0.852 ms (0.31%)
ddtrace.appsec._utils 0.852 ms (0.31%)
ddtrace._trace.trace_handlers 0.079 ms (0.03%)
ddtrace.contrib.trace_utils 0.079 ms (0.03%)
ddtrace.contrib.internal.trace_utils 0.079 ms (0.03%)
shlex 0.059 ms (0.02%)
ddtrace.contrib.internal.subprocess.constants 0.033 ms (0.01%)

The following import paths have shrunk:

ddtrace.auto 2.889 ms (1.04%)
ddtrace.bootstrap.sitecustomize 1.655 ms (0.59%)
ddtrace.appsec._common_module_patches 0.882 ms (0.32%)
ddtrace.appsec._asm_request_context 0.813 ms (0.29%)
ddtrace.appsec._metrics 0.068 ms (0.02%)
ddtrace.bootstrap.preload 0.679 ms (0.24%)
ddtrace.internal.remoteconfig.client 0.537 ms (0.19%)
ddtrace.internal.flare.flare 0.075 ms (0.03%)
logging.handlers 0.075 ms (0.03%)
ddtrace.debugging._import 0.067 ms (0.02%)
ddtrace.debugging._function.discovery 0.067 ms (0.02%)
ddtrace._trace.trace_handlers 0.094 ms (0.03%)
ddtrace 1.234 ms (0.44%)
ddtrace._logger 0.569 ms (0.20%)
ddtrace.internal.telemetry 0.569 ms (0.20%)
ddtrace.internal.telemetry.writer 0.441 ms (0.16%)
http.client 0.441 ms (0.16%)
ssl 0.281 ms (0.10%)
http 0.096 ms (0.03%)
email.parser 0.064 ms (0.02%)
ddtrace.settings._agent 0.129 ms (0.05%)
ddtrace.settings 0.071 ms (0.03%)
ddtrace.settings.integration 0.071 ms (0.03%)
ddtrace.vendor.debtcollector 0.071 ms (0.03%)
ddtrace.settings._core 0.058 ms (0.02%)
envier 0.058 ms (0.02%)
envier.env 0.058 ms (0.02%)
ddtrace.internal._unpatched 0.026 ms (0.01%)
json 0.026 ms (0.01%)
json.decoder 0.026 ms (0.01%)
re 0.026 ms (0.01%)
enum 0.026 ms (0.01%)
types 0.026 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 23, 2025

Benchmarks

Benchmark execution time: 2025-06-23 19:30:29

Comparing candidate commit 67e953c in PR branch backport-13586-to-3.9 with baseline commit 37766dd in branch 3.9.

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

scenario:iastaspects-lower_aspect

  • 🟥 execution_time [+186.233ns; +238.564ns] or [+8.241%; +10.556%]

scenario:iastaspects-lstrip_aspect

  • 🟥 execution_time [+1.260µs; +1.462µs] or [+9.665%; +11.216%]

scenario:iastaspects-strip_aspect

  • 🟥 execution_time [+1.349µs; +1.553µs] or [+10.366%; +11.929%]

@P403n1x87 P403n1x87 merged commit a3c6229 into 3.9 Jun 23, 2025
795 checks passed
@P403n1x87 P403n1x87 deleted the backport-13586-to-3.9 branch June 23, 2025 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dynamic Instrumentation Dynamic Instrumentation/Live Debugger
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants