Skip to content

Commit

Permalink
Ensure async generators are awaited (open-telemetry#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrjjvv committed Aug 12, 2024
1 parent f9dc90f commit 6981035
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2589](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2589))
- `opentelemetry-instrumentation-celery` propagates baggage
([#2385](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2385))
- `opentelemetry-instrumentation-asyncio` Fixes async generator coroutines not being awaited
([#2792](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2792))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def trace_item(self, coro_or_future):

async def trace_coroutine(self, coro):
if not hasattr(coro, "__name__"):
return coro
return await coro
start = default_timer()
attr = {
"type": "coroutine",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ def tearDown(self):

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

async_gen_instance = async_gen()
agen = anext(async_gen_instance)
await asyncio.create_task(agen)
return await asyncio.create_task(agen)

asyncio.run(main())
ret = asyncio.run(main())
self.assertEqual(ret, 2) # first iteration from range()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

0 comments on commit 6981035

Please sign in to comment.