Skip to content
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

chore: enable ModuleWatchdog globally #4098

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
]