Skip to content

Commit

Permalink
chore: enable ModuleWatchdog globally (#4098)
Browse files Browse the repository at this point in the history
## Description

This change installs an instance of the `ModuleWatchdog` for registering module import hooks.

## Checklist
- [x] Title must conform to [conventional commit](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional).
- [x] Add additional sections for `feat` and `fix` pull requests.
- [x] Ensure tests are passing for affected code.
- [x] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description.

## Reviewer Checklist
- [ ] Title is accurate.
- [ ] Description motivates each change.
- [ ] No unnecessary changes were introduced in this PR.
- [ ] PR cannot be broken up into smaller PRs.
- [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary.
- [ ] Tests provided or description of manual testing performed is included in the code or PR.
- [x] Release note has been added for fixes and features, or else `changelog/no-changelog` label added.
- [ ] All relevant GitHub issues are correctly linked.
- [ ] Backports are identified and tagged with Mergifyio.
- [ ] Add to milestone.
  • Loading branch information
P403n1x87 authored Aug 18, 2022
1 parent 8c0ddb9 commit 81548b9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
7 changes: 6 additions & 1 deletion ddtrace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from ._logger import configure_ddtrace_logger
from ddtrace.internal.module import ModuleWatchdog


ModuleWatchdog.install()

from ._logger import configure_ddtrace_logger # noqa: E402


# configure ddtrace logger before other modules log
Expand Down
24 changes: 18 additions & 6 deletions tests/internal/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
import tests.test_module


@pytest.fixture(autouse=True, scope="module")
def ensure_no_module_watchdog():
# DEV: The library might use the ModuleWatchdog and install it at a very
# early stage. This fixture ensures that the watchdog is not installed
# before the tests start.
was_installed = ModuleWatchdog.is_installed()
if was_installed:
ModuleWatchdog.uninstall()

try:
yield
finally:
if was_installed:
ModuleWatchdog.install()


@pytest.fixture
def module_watchdog():
ModuleWatchdog.install()
Expand Down Expand Up @@ -65,8 +81,6 @@ def test_import_origin_hook_for_module_not_yet_imported():
path = os.getenv("MODULE_ORIGIN")
hook = mock.Mock()

ModuleWatchdog.install()

ModuleWatchdog.register_origin_hook(path, hook)

hook.assert_not_called()
Expand Down Expand Up @@ -100,8 +114,6 @@ def test_import_module_hook_for_module_not_yet_imported():
name = "tests.test_module"
hook = mock.Mock()

ModuleWatchdog.install()

ModuleWatchdog.register_module_hook(name, hook)

hook.assert_not_called()
Expand Down Expand Up @@ -136,8 +148,6 @@ def test_module_deleted():
path = os.getenv("MODULE_ORIGIN")
hook = mock.Mock()

ModuleWatchdog.install()

ModuleWatchdog.register_origin_hook(path, hook)
ModuleWatchdog.register_module_hook(name, hook)

Expand Down Expand Up @@ -327,3 +337,5 @@ def test_module_watchdog_dict_shallow_copy():

# Ensure that they match
assert original_modules == new_modules

ModuleWatchdog.uninstall()
8 changes: 4 additions & 4 deletions tests/profiling/collector/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_lock_acquire_events():
# It's called through pytest so I'm sure it's gonna be that long, right?
assert len(event.frames) > 3
assert event.nframes > 3
assert event.frames[0] == (__file__, 65, "test_lock_acquire_events")
assert event.frames[0] == (__file__.replace(".pyc", ".py"), 65, "test_lock_acquire_events")
assert event.sampling_pct == 100


Expand Down Expand Up @@ -187,7 +187,7 @@ def test_lock_release_events():
# It's called through pytest so I'm sure it's gonna be that long, right?
assert len(event.frames) > 3
assert event.nframes > 3
assert event.frames[0] == (__file__, 180, "test_lock_release_events")
assert event.frames[0] == (__file__.replace(".pyc", ".py"), 180, "test_lock_release_events")
assert event.sampling_pct == 100


Expand Down Expand Up @@ -217,7 +217,7 @@ def play_with_lock():
# It's called through pytest so I'm sure it's gonna be that long, right?
assert len(event.frames) > 3
assert event.nframes > 3
assert event.frames[0] == (__file__, 200, "play_with_lock")
assert event.frames[0] == (__file__.replace(".pyc", ".py"), 200, "play_with_lock")
assert event.sampling_pct == 100
assert event.task_id == t.ident
assert event.task_name == "foobar"
Expand All @@ -234,7 +234,7 @@ def play_with_lock():
# It's called through pytest so I'm sure it's gonna be that long, right?
assert len(event.frames) > 3
assert event.nframes > 3
assert event.frames[0] == (__file__, 201, "play_with_lock")
assert event.frames[0] == (__file__.replace(".pyc", ".py"), 201, "play_with_lock")
assert event.sampling_pct == 100
assert event.task_id == t.ident
assert event.task_name == "foobar"
Expand Down
10 changes: 4 additions & 6 deletions tests/profiling/collector/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ def test_check_traceback_to_frames():
exc_type, exc_value, traceback = sys.exc_info()
frames, nframes = _traceback.traceback_to_frames(traceback, 10)
assert nframes == 2

this_file = __file__.replace(".pyc", ".py")
assert frames == [
(__file__, 7, "_x"),
(
__file__,
15,
"test_check_traceback_to_frames",
),
(this_file, 7, "_x"),
(this_file, 15, "test_check_traceback_to_frames"),
]

0 comments on commit 81548b9

Please sign in to comment.