Skip to content

Commit 74b8508

Browse files
aiokafka test workaround for CPython 3.9 bug
1 parent 2d7afac commit 74b8508

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

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+
# mock.patch is a hack for Python 3.9 see https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3880
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+
# mock.patch is a hack for Python 3.9 see https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3880
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)

0 commit comments

Comments
 (0)