Skip to content

Commit 5d3a3cc

Browse files
authored
fix(bootstrap): make ddtrace.auto work as expected [backport #5716 to 1.12] (#5739)
Backport of #5716 to 1.12 Importing ddtrace.auto causes the import of ddtrace, which pollutes the module space before the sitecustomize logic can freeze the set of start-up modules and do the clean-up when required. This made the import of ddtrace.auto not behave as expected, that is as a programmatic equivalent of the ddtrace-run command. With the proposed change, we compute the initial set of modules in `ddtrace` and import that in the sitecustomize script. This way we ensure that the set is computed at the right time, regardless of how `ddtrace` is used. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] PR description includes explicit acknowledgement/acceptance of the performance implications of this PR as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment.
1 parent 901f56a commit 5d3a3cc

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

ddtrace/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import sys
2+
3+
4+
LOADED_MODULES = frozenset(sys.modules.keys())
5+
16
from ddtrace.internal.module import ModuleWatchdog
27

38

ddtrace/bootstrap/sitecustomize.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
Bootstrapping code that is run when using the `ddtrace-run` Python entrypoint
33
Add all monkey-patching that needs to run by default here
44
"""
5-
import sys
6-
7-
8-
LOADED_MODULES = frozenset(sys.modules.keys())
5+
from ddtrace import LOADED_MODULES # isort:skip
96

107
import logging # noqa
118
import os # noqa
9+
import sys
1210
from typing import Any # noqa
1311
from typing import Dict # noqa
1412
import warnings # noqa
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
bootstrap: fixed an issue with the behavior of ``ddtrace.auto`` that could
5+
have caused incompatibilities with frameworks such as ``gevent`` when used
6+
as a programmatic alternative to the ``ddtrace-run`` command.

tests/internal/test_auto.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.mark.subprocess(env=dict(DD_UNLOAD_MODULES_FROM_SITECUSTOMIZE="true"))
5+
def test_auto():
6+
import sys
7+
8+
import ddtrace.auto # noqa
9+
10+
assert "threading" not in sys.modules

0 commit comments

Comments
 (0)