Skip to content

Commit 6981035

Browse files
authored
Ensure async generators are awaited (#2792)
1 parent f9dc90f commit 6981035

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
([#2589](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2589))
3030
- `opentelemetry-instrumentation-celery` propagates baggage
3131
([#2385](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2385))
32+
- `opentelemetry-instrumentation-asyncio` Fixes async generator coroutines not being awaited
33+
([#2792](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2792))
3234

3335
## Version 1.26.0/0.47b0 (2024-07-23)
3436

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def trace_item(self, coro_or_future):
262262

263263
async def trace_coroutine(self, coro):
264264
if not hasattr(coro, "__name__"):
265-
return coro
265+
return await coro
266266
start = default_timer()
267267
attr = {
268268
"type": "coroutine",

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,23 @@ def tearDown(self):
4343

4444
# Asyncio anext() does not have __name__ attribute, which is used to determine if the coroutine should be traced.
4545
# This test is to ensure that the instrumentation does not break when the coroutine does not have __name__ attribute.
46+
# Additionally, ensure the coroutine is actually awaited.
4647
@skipIf(
4748
sys.version_info < (3, 10), "anext is only available in Python 3.10+"
4849
)
4950
def test_asyncio_anext(self):
5051
async def main():
5152
async def async_gen():
52-
for it in range(2):
53+
# nothing special about this range other than to avoid returning a zero
54+
# from a function named 'main' (which might cause confusion about intent)
55+
for it in range(2, 4):
5356
yield it
5457

5558
async_gen_instance = async_gen()
5659
agen = anext(async_gen_instance)
57-
await asyncio.create_task(agen)
60+
return await asyncio.create_task(agen)
5861

59-
asyncio.run(main())
62+
ret = asyncio.run(main())
63+
self.assertEqual(ret, 2) # first iteration from range()
6064
spans = self.memory_exporter.get_finished_spans()
6165
self.assertEqual(len(spans), 0)

0 commit comments

Comments
 (0)