Skip to content

Commit 8297dde

Browse files
phillipuniverseemdnetoxrmx
authored
Replace asyncio.iscoroutinefunction with inspect.iscoroutinefunction (#3880)
* Replace asyncio.iscoroutinefunction with inspect.iscoroutinefunction Fixes #3879 * Fix import ordering * aiokafka test workaround for CPython 3.9 bug * Apply suggestions from code review --------- Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 8356368 commit 8297dde

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242
([#3882](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3882))
4343
- `opentelemetry-instrumentation-aiohttp-server`: delay initialization of tracer, meter and excluded urls to instrumentation for testability
4444
([#3836](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3836))
45+
- Replace Python 3.14-deprecated `asyncio.iscoroutinefunction` with `inspect.iscoroutinefunction`.
46+
([#3880](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3880))
4547
- `opentelemetry-instrumentation-elasticsearch`: Enhance elasticsearch query body sanitization
4648
([#3919](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3919))
4749
- `opentelemetry-instrumentation-pymongo`: Fix span error descriptions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async def produce():
9595

9696
from __future__ import annotations
9797

98-
from asyncio import iscoroutinefunction
98+
from inspect import iscoroutinefunction
9999
from typing import TYPE_CHECKING, Collection
100100

101101
import aiokafka

instrumentation/opentelemetry-instrumentation-aiokafka/tests/test_instrumentation.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,21 @@ async def async_consume_hook(span, *_) -> None:
261261
async def test_getone_consume_hook(self) -> None:
262262
async_consume_hook_mock = mock.AsyncMock()
263263

264+
def is_async_consume_hook_mock(obj: Any) -> bool:
265+
return obj is async_consume_hook_mock
266+
264267
AIOKafkaInstrumentor().uninstrument()
265-
AIOKafkaInstrumentor().instrument(
266-
tracer_provider=self.tracer_provider,
267-
async_consume_hook=async_consume_hook_mock,
268-
)
268+
269+
# TODO: remove mock.patch when we drop Python 3.9 support
270+
with mock.patch(
271+
"opentelemetry.instrumentation.aiokafka.iscoroutinefunction"
272+
) as iscoro:
273+
iscoro.side_effect = is_async_consume_hook_mock
274+
275+
AIOKafkaInstrumentor().instrument(
276+
tracer_provider=self.tracer_provider,
277+
async_consume_hook=async_consume_hook_mock,
278+
)
269279

270280
consumer = await self.consumer_factory()
271281
self.addAsyncCleanup(consumer.stop)
@@ -448,11 +458,20 @@ async def test_send_baggage(self) -> None:
448458
async def test_send_produce_hook(self) -> None:
449459
async_produce_hook_mock = mock.AsyncMock()
450460

461+
def is_async_produce_hook_mock(obj: Any) -> bool:
462+
return obj is async_produce_hook_mock
463+
451464
AIOKafkaInstrumentor().uninstrument()
452-
AIOKafkaInstrumentor().instrument(
453-
tracer_provider=self.tracer_provider,
454-
async_produce_hook=async_produce_hook_mock,
455-
)
465+
# TODO: remove mock.patch when we drop Python 3.9 support
466+
with mock.patch(
467+
"opentelemetry.instrumentation.aiokafka.iscoroutinefunction"
468+
) as iscoro:
469+
iscoro.side_effect = is_async_produce_hook_mock
470+
471+
AIOKafkaInstrumentor().instrument(
472+
tracer_provider=self.tracer_provider,
473+
async_produce_hook=async_produce_hook_mock,
474+
)
456475

457476
producer = await self.producer_factory()
458477
self.addAsyncCleanup(producer.stop)

instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import asyncio
1+
import inspect
22
import typing
33
from collections.abc import Coroutine
44

@@ -197,7 +197,7 @@ async def __aenter__(self):
197197

198198
async def __aexit__(self, exc_type, exc, t_b):
199199
try:
200-
if asyncio.iscoroutinefunction(self._obj.close):
200+
if inspect.iscoroutinefunction(self._obj.close):
201201
await self._obj.close()
202202
else:
203203
self._obj.close()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ async def async_response_hook(span, request, response):
225225

226226
import logging
227227
import typing
228-
from asyncio import iscoroutinefunction
229228
from functools import partial
229+
from inspect import iscoroutinefunction
230230
from timeit import default_timer
231231
from types import TracebackType
232232

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import abc
1818
import asyncio
19+
import inspect
1920
import typing
2021
from unittest import mock
2122

@@ -1086,7 +1087,7 @@ def test_custom_tracer_provider(self):
10861087
def test_response_hook(self):
10871088
response_hook_key = (
10881089
"async_response_hook"
1089-
if asyncio.iscoroutinefunction(self.response_hook)
1090+
if inspect.iscoroutinefunction(self.response_hook)
10901091
else "response_hook"
10911092
)
10921093
response_hook_kwargs = {response_hook_key: self.response_hook}
@@ -1133,7 +1134,7 @@ def test_response_hook_sync_async_kwargs(self):
11331134
def test_request_hook(self):
11341135
request_hook_key = (
11351136
"async_request_hook"
1136-
if asyncio.iscoroutinefunction(self.request_hook)
1137+
if inspect.iscoroutinefunction(self.request_hook)
11371138
else "request_hook"
11381139
)
11391140
request_hook_kwargs = {request_hook_key: self.request_hook}

0 commit comments

Comments
 (0)