fix(async-io): instrumented `asyncio.wait_for` properly raises `asyncio.TimeoutError` (#2637)

This commit is contained in:
Radoslav Kirilov 2024-07-02 18:21:15 +03:00 committed by GitHub
parent 529178d9ae
commit a29242f493
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 0 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- `opentelemetry-instrumentation-asyncio` instrumented `asyncio.wait_for` properly raises `asyncio.TimeoutError` as expected
([#2637](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2637))
- `opentelemetry-instrumentation-aws-lambda` Bugfix: AWS Lambda event source key incorrect for SNS in instrumentation library.
([#2612](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2612))
- `opentelemetry-instrumentation-system-metrics` Permit to use psutil 6.0+.

View File

@ -280,8 +280,11 @@ class AsyncioInstrumentor(BaseInstrumentor):
# CancelledError is raised when a coroutine is cancelled
# before it has a chance to run. We don't want to record
# this as an error.
# Still it needs to be raised in order for `asyncio.wait_for`
# to properly work with timeout and raise accordingly `asyncio.TimeoutError`
except asyncio.CancelledError:
attr["state"] = "cancelled"
raise
except Exception as exc:
exception = exc
state = determine_state(exception)

View File

@ -68,6 +68,19 @@ class TestAsyncioWait(TestBase):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
def test_asyncio_wait_for_with_timeout(self):
expected_timeout_error = None
async def main():
nonlocal expected_timeout_error
try:
await asyncio.wait_for(async_func(), 0.01)
except asyncio.TimeoutError as timeout_error:
expected_timeout_error = timeout_error
asyncio.run(main())
self.assertNotEqual(expected_timeout_error, None)
def test_asyncio_as_completed(self):
async def main():
if sys.version_info >= (3, 11):