Skip to content

Commit

Permalink
add python version check
Browse files Browse the repository at this point in the history
  • Loading branch information
bourbonkk committed Sep 11, 2023
1 parent 56d0c02 commit 55f2325
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test = [
"opentelemetry-test-utils == 0.41b0.dev",
"pytest",
"wrapt >= 1.0.0, < 2.0.0",
"pytest-asyncio",
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def uninstrument_gather(self):
unwrap(asyncio, "gather")

def instrument_to_thread(self):
# to_thread was added in Python 3.9
if sys.version_info < (3, 9):
return

def wrap_to_thread(method, instance, args, kwargs):
if args:
first_arg = args[0]
Expand All @@ -144,22 +148,30 @@ def wrap_to_thread(method, instance, args, kwargs):
_wrap(asyncio, "to_thread", wrap_to_thread)

def uninstrument_to_thread(self):
# to_thread was added in Python 3.9
if sys.version_info < (3, 9):
return
unwrap(asyncio, "to_thread")

def instrument_taskgroup_create_task(self):
if sys.version_info >= (3, 11):
def wrap_taskgroup_create_task(method, instance, args, kwargs):
if args:
coro = args[0]
wrapped_coro = self.trace_coroutine(coro)
wrapped_args = (wrapped_coro,) + args[1:]
return method(*wrapped_args, **kwargs)
# TaskGroup.create_task was added in Python 3.11
if sys.version_info < (3, 11):
return

def wrap_taskgroup_create_task(method, instance, args, kwargs):
if args:
coro = args[0]
wrapped_coro = self.trace_coroutine(coro)
wrapped_args = (wrapped_coro,) + args[1:]
return method(*wrapped_args, **kwargs)

_wrap(asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task)
_wrap(asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task)

def uninstrument_taskgroup_create_task(self):
if sys.version_info >= (3, 11):
unwrap(asyncio.TaskGroup, "create_task")
# TaskGroup.create_task was added in Python 3.11
if sys.version_info < (3, 11):
return
unwrap(asyncio.TaskGroup, "create_task")

def trace_func(self, func):
"""Trace a function."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def tearDown(self):
AsyncioInstrumentor().uninstrument()

def test_task_group_create_task(self):
# TaskGroup is only available in Python 3.11+
async def main():
if py11:
async with asyncio.TaskGroup() as tg:
Expand All @@ -46,6 +47,5 @@ async def main():

asyncio.run(main())
spans = self.memory_exporter.get_finished_spans()
assert spans
if py11:
self.assertEqual(len(spans), 10)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import sys

from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer
Expand All @@ -32,6 +33,7 @@ def tearDown(self):
AsyncioInstrumentor().uninstrument()

def test_to_thread(self):
# to_thread is only available in Python 3.9+
def multiply(x, y):
return x * y

Expand All @@ -41,5 +43,6 @@ async def to_thread():

asyncio.run(to_thread())
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
assert spans[0].name == "asyncio.to_thread_func-multiply"
if sys.version_info >= (3, 9):
self.assertEqual(len(spans), 1)
assert spans[0].name == "asyncio.to_thread_func-multiply"

0 comments on commit 55f2325

Please sign in to comment.