Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure async generators are awaited #2792

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)