Skip to content

Commit 3b256c6

Browse files
fix(debugging): function probes on same lazy module [backport 2.0] (#7311)
Backport 8e68cfa from #7286 to 2.0. Fix a typo that caused a module lookup to fail with a KeyError exception, even though it was followed by a check against None. This caused function probes on the same module that was not yet loaded to fail to instrument and be reported in the ``ERROR`` status in the UI. ## 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/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## 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. - [x] 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) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent dc808d1 commit 3b256c6

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

ddtrace/debugging/_debugger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def register_module_hook(cls, module_name, hook):
131131
if module_name in cls._locations:
132132
# We already have a hook for this origin, don't register a new one
133133
# but invoke it directly instead, if the module was already loaded.
134-
module = sys.modules[module_name]
134+
module = sys.modules.get(module_name)
135135
if module is not None:
136136
hook(module)
137137

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
dynamic instrumentation: fix an issue that caused function probes on the
5+
same module to fail to instrument and be reported in the ``ERROR`` status
6+
in the UI if the module was not yet imported.

tests/debugging/test_debugger.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,28 @@ def test_debugger_multiple_function_probes_on_same_function():
512512
Stuff.instancestuff.__dd_wrappers__
513513

514514

515+
def test_debugger_multiple_function_probes_on_same_lazy_module():
516+
sys.modules.pop("tests.submod.stuff", None)
517+
518+
probes = [
519+
create_snapshot_function_probe(
520+
probe_id="probe-instance-method-%d" % i,
521+
module="tests.submod.stuff",
522+
func_qname="Stuff.instancestuff",
523+
rate=float("inf"),
524+
)
525+
for i in range(3)
526+
]
527+
528+
with debugger() as d:
529+
d.add_probes(*probes)
530+
531+
import tests.submod.stuff # noqa
532+
533+
assert len(d._probe_registry) == len(probes)
534+
assert all(_.error_type is None for _ in d._probe_registry.values())
535+
536+
515537
# DEV: The following tests are to ensure compatibility with the tracer
516538
import wrapt as wrapt # noqa
517539

@@ -861,7 +883,7 @@ def test_debugger_log_live_probe_generate_messages():
861883
" ",
862884
{"dsl": "bar", "json": {"ref": "bar"}},
863885
"!",
864-
)
886+
),
865887
),
866888
)
867889

@@ -999,7 +1021,7 @@ def test_debugger_modified_probe():
9991021
version=1,
10001022
source_file="tests/submod/stuff.py",
10011023
line=36,
1002-
**compile_template("hello world")
1024+
**compile_template("hello world"),
10031025
)
10041026
)
10051027

@@ -1015,7 +1037,7 @@ def test_debugger_modified_probe():
10151037
version=2,
10161038
source_file="tests/submod/stuff.py",
10171039
line=36,
1018-
**compile_template("hello brave new world")
1040+
**compile_template("hello brave new world"),
10191041
)
10201042
)
10211043

0 commit comments

Comments
 (0)