diff --git a/ddtrace/internal/wrapping/context.py b/ddtrace/internal/wrapping/context.py index e9308a78dfc..1153f9dcf6a 100644 --- a/ddtrace/internal/wrapping/context.py +++ b/ddtrace/internal/wrapping/context.py @@ -1,6 +1,7 @@ from abc import ABC from contextvars import ContextVar from inspect import iscoroutinefunction +from inspect import isgeneratorfunction import sys from types import FrameType from types import FunctionType @@ -671,9 +672,9 @@ def wrap(self) -> None: pass i += 1 - # Search for the GEN_START instruction + # Search for the GEN_START instruction, which needs to stay on top. i = 0 - if sys.version_info >= (3, 10) and iscoroutinefunction(f): + if sys.version_info >= (3, 10) and (iscoroutinefunction(f) or isgeneratorfunction(f)): for i, instr in enumerate(bc, 1): try: if instr.name == "GEN_START": diff --git a/releasenotes/notes/fix-generator-wrapping-86af0ae55ba2723a.yaml b/releasenotes/notes/fix-generator-wrapping-86af0ae55ba2723a.yaml new file mode 100644 index 00000000000..690e7211b8a --- /dev/null +++ b/releasenotes/notes/fix-generator-wrapping-86af0ae55ba2723a.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + dynamic instrumentation: fixed an issue with the instrumentation of + generators with Python 3.10.