Skip to content

Commit 60fb936

Browse files
xrmxlzchen
andauthoredJul 10, 2024··
instrumentation/asyncio: catch CancelledError exception in tests (#2690)
* instrumentation/asyncio: catch CancelledError exception in tests After a29242f we are re-raising the CancelledError so we need to catch it on the caller side. Fix #2688 * instrument/asyncio: don't test anext on python < 3.10 Since it's not available there. * instrumentation/asyncio: use unittest.skipIf instead of reimplementing it --------- Co-authored-by: Leighton Chen <lechen@microsoft.com>
1 parent 0b20c95 commit 60fb936

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed
 

‎instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_anext.py

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import asyncio
15+
import sys
16+
from unittest import skipIf
1517
from unittest.mock import patch
1618

1719
# pylint: disable=no-name-in-module
@@ -41,6 +43,9 @@ def tearDown(self):
4143

4244
# Asyncio anext() does not have __name__ attribute, which is used to determine if the coroutine should be traced.
4345
# This test is to ensure that the instrumentation does not break when the coroutine does not have __name__ attribute.
46+
@skipIf(
47+
sys.version_info < (3, 10), "anext is only available in Python 3.10+"
48+
)
4449
def test_asyncio_anext(self):
4550
async def main():
4651
async def async_gen():

‎instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_cancellation.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def tearDown(self):
4545

4646
def test_cancel(self):
4747
with self._tracer.start_as_current_span("root", kind=SpanKind.SERVER):
48-
asyncio.run(cancellation_create_task())
48+
try:
49+
asyncio.run(cancellation_create_task())
50+
except asyncio.CancelledError:
51+
pass
4952
spans = self.memory_exporter.get_finished_spans()
5053
self.assertEqual(len(spans), 3)
5154
self.assertEqual(spans[0].context.trace_id, spans[1].context.trace_id)

‎instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_taskgroup.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import asyncio
1515
import sys
16+
from unittest import skipIf
1617
from unittest.mock import patch
1718

1819
# pylint: disable=no-name-in-module
@@ -25,10 +26,6 @@
2526

2627
from .common_test_func import async_func
2728

28-
py11 = False
29-
if sys.version_info >= (3, 11):
30-
py11 = True
31-
3229

3330
class TestAsyncioTaskgroup(TestBase):
3431
@patch.dict(
@@ -46,11 +43,11 @@ def tearDown(self):
4643
super().tearDown()
4744
AsyncioInstrumentor().uninstrument()
4845

46+
@skipIf(
47+
sys.version_info < (3, 11),
48+
"TaskGroup is only available in Python 3.11+",
49+
)
4950
def test_task_group_create_task(self):
50-
# TaskGroup is only available in Python 3.11+
51-
if not py11:
52-
return
53-
5451
async def main():
5552
async with asyncio.TaskGroup() as tg: # pylint: disable=no-member
5653
for _ in range(10):

‎instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_to_thread.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import asyncio
1515
import sys
16+
from unittest import skipIf
1617
from unittest.mock import patch
1718

1819
# pylint: disable=no-name-in-module
@@ -40,34 +41,34 @@ def tearDown(self):
4041
super().tearDown()
4142
AsyncioInstrumentor().uninstrument()
4243

44+
@skipIf(
45+
sys.version_info < (3, 9), "to_thread is only available in Python 3.9+"
46+
)
4347
def test_to_thread(self):
44-
# to_thread is only available in Python 3.9+
45-
if sys.version_info >= (3, 9):
46-
47-
def multiply(x, y):
48-
return x * y
48+
def multiply(x, y):
49+
return x * y
4950

50-
async def to_thread():
51-
result = await asyncio.to_thread(multiply, 2, 3)
52-
assert result == 6
51+
async def to_thread():
52+
result = await asyncio.to_thread(multiply, 2, 3)
53+
assert result == 6
5354

54-
with self._tracer.start_as_current_span("root"):
55-
asyncio.run(to_thread())
56-
spans = self.memory_exporter.get_finished_spans()
55+
with self._tracer.start_as_current_span("root"):
56+
asyncio.run(to_thread())
57+
spans = self.memory_exporter.get_finished_spans()
5758

58-
self.assertEqual(len(spans), 2)
59-
assert spans[0].name == "asyncio to_thread-multiply"
60-
for metric in (
61-
self.memory_metrics_reader.get_metrics_data()
62-
.resource_metrics[0]
63-
.scope_metrics[0]
64-
.metrics
65-
):
66-
if metric.name == "asyncio.process.duration":
67-
for point in metric.data.data_points:
68-
self.assertEqual(point.attributes["type"], "to_thread")
69-
self.assertEqual(point.attributes["name"], "multiply")
70-
if metric.name == "asyncio.process.created":
71-
for point in metric.data.data_points:
72-
self.assertEqual(point.attributes["type"], "to_thread")
73-
self.assertEqual(point.attributes["name"], "multiply")
59+
self.assertEqual(len(spans), 2)
60+
assert spans[0].name == "asyncio to_thread-multiply"
61+
for metric in (
62+
self.memory_metrics_reader.get_metrics_data()
63+
.resource_metrics[0]
64+
.scope_metrics[0]
65+
.metrics
66+
):
67+
if metric.name == "asyncio.process.duration":
68+
for point in metric.data.data_points:
69+
self.assertEqual(point.attributes["type"], "to_thread")
70+
self.assertEqual(point.attributes["name"], "multiply")
71+
if metric.name == "asyncio.process.created":
72+
for point in metric.data.data_points:
73+
self.assertEqual(point.attributes["type"], "to_thread")
74+
self.assertEqual(point.attributes["name"], "multiply")

0 commit comments

Comments
 (0)
Please sign in to comment.