Skip to content

Commit 55f2325

Browse files
committed
add python version check
1 parent 56d0c02 commit 55f2325

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-asyncio/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test = [
3636
"opentelemetry-test-utils == 0.41b0.dev",
3737
"pytest",
3838
"wrapt >= 1.0.0, < 2.0.0",
39+
"pytest-asyncio",
3940
]
4041

4142
[project.entry-points.opentelemetry_instrumentor]

Diff for: instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/__init__.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ def uninstrument_gather(self):
132132
unwrap(asyncio, "gather")
133133

134134
def instrument_to_thread(self):
135+
# to_thread was added in Python 3.9
136+
if sys.version_info < (3, 9):
137+
return
138+
135139
def wrap_to_thread(method, instance, args, kwargs):
136140
if args:
137141
first_arg = args[0]
@@ -144,22 +148,30 @@ def wrap_to_thread(method, instance, args, kwargs):
144148
_wrap(asyncio, "to_thread", wrap_to_thread)
145149

146150
def uninstrument_to_thread(self):
151+
# to_thread was added in Python 3.9
152+
if sys.version_info < (3, 9):
153+
return
147154
unwrap(asyncio, "to_thread")
148155

149156
def instrument_taskgroup_create_task(self):
150-
if sys.version_info >= (3, 11):
151-
def wrap_taskgroup_create_task(method, instance, args, kwargs):
152-
if args:
153-
coro = args[0]
154-
wrapped_coro = self.trace_coroutine(coro)
155-
wrapped_args = (wrapped_coro,) + args[1:]
156-
return method(*wrapped_args, **kwargs)
157+
# TaskGroup.create_task was added in Python 3.11
158+
if sys.version_info < (3, 11):
159+
return
160+
161+
def wrap_taskgroup_create_task(method, instance, args, kwargs):
162+
if args:
163+
coro = args[0]
164+
wrapped_coro = self.trace_coroutine(coro)
165+
wrapped_args = (wrapped_coro,) + args[1:]
166+
return method(*wrapped_args, **kwargs)
157167

158-
_wrap(asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task)
168+
_wrap(asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task)
159169

160170
def uninstrument_taskgroup_create_task(self):
161-
if sys.version_info >= (3, 11):
162-
unwrap(asyncio.TaskGroup, "create_task")
171+
# TaskGroup.create_task was added in Python 3.11
172+
if sys.version_info < (3, 11):
173+
return
174+
unwrap(asyncio.TaskGroup, "create_task")
163175

164176
def trace_func(self, func):
165177
"""Trace a function."""

Diff for: instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_taskgroup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def tearDown(self):
3838
AsyncioInstrumentor().uninstrument()
3939

4040
def test_task_group_create_task(self):
41+
# TaskGroup is only available in Python 3.11+
4142
async def main():
4243
if py11:
4344
async with asyncio.TaskGroup() as tg:
@@ -46,6 +47,5 @@ async def main():
4647

4748
asyncio.run(main())
4849
spans = self.memory_exporter.get_finished_spans()
49-
assert spans
5050
if py11:
5151
self.assertEqual(len(spans), 10)

Diff for: instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_to_thread.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import asyncio
15+
import sys
1516

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

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

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

4244
asyncio.run(to_thread())
4345
spans = self.memory_exporter.get_finished_spans()
44-
self.assertEqual(len(spans), 1)
45-
assert spans[0].name == "asyncio.to_thread_func-multiply"
46+
if sys.version_info >= (3, 9):
47+
self.assertEqual(len(spans), 1)
48+
assert spans[0].name == "asyncio.to_thread_func-multiply"

0 commit comments

Comments
 (0)