Skip to content

Commit 4326b6f

Browse files
authored
Merge branch 'main' into asgi-suppress-http-spans
2 parents e7c83eb + 4d266a0 commit 4326b6f

5 files changed

Lines changed: 67 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242
([#4302](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4302))
4343
- `opentelemetry-instrumentation-grpc`: Fix bidirectional streaming RPCs raising `AttributeError: 'generator' object has no attribute 'add_done_callback'`
4444
([#4259](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4259))
45+
- `opentelemetry-instrumentation-aiokafka`: fix `Unclosed AIOKafkaProducer` warning and `RuntimeWarning: coroutine was never awaited` in tests
46+
([#4384](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4384))
47+
- `opentelemetry-instrumentation-aiokafka`: Fix compatibility with aiokafka 0.13 by calling
48+
`_key_serializer`/`_value_serializer` directly instead of the internal `_serialize` method
49+
whose signature changed in 0.13 from `(topic, key, value)` to `(key, value, headers)`
50+
([#4379](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4379))
4551

4652
### Breaking changes
4753

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,17 @@ async def _extract_send_partition(
162162
key = _extract_send_key(args, kwargs)
163163
value = _extract_send_value(args, kwargs)
164164
partition = _extract_argument("partition", 3, None, args, kwargs)
165-
key_bytes, value_bytes = cast(
166-
"tuple[bytes | None, bytes | None]",
167-
instance._serialize(topic, key, value), # type: ignore[reportUnknownMemberType]
165+
key_bytes = cast(
166+
"bytes | None",
167+
instance._key_serializer(key) # type: ignore[reportUnknownMemberType]
168+
if instance._key_serializer # type: ignore[reportUnknownMemberType]
169+
else key,
170+
)
171+
value_bytes = cast(
172+
"bytes | None",
173+
instance._value_serializer(value) # type: ignore[reportUnknownMemberType]
174+
if instance._value_serializer # type: ignore[reportUnknownMemberType]
175+
else value,
168176
)
169177
valid_types = (bytes, bytearray, memoryview, type(None))
170178
if (
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
aiokafka==0.11.0
1+
aiokafka==0.13.0; python_version >= "3.10"
2+
aiokafka==0.12.0; python_version < "3.10"
23
pytest==7.4.4
34
-e opentelemetry-instrumentation
45
-e instrumentation/opentelemetry-instrumentation-aiokafka

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

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
from __future__ import annotations
1616

17+
import sys
1718
import uuid
1819
from typing import Any, Sequence, cast
19-
from unittest import IsolatedAsyncioTestCase, TestCase, mock
20+
from unittest import IsolatedAsyncioTestCase, TestCase, mock, skipIf
2021

2122
import aiokafka
2223
from aiokafka import (
@@ -63,6 +64,10 @@ def test_instrument_api(self) -> None:
6364
)
6465

6566

67+
@skipIf(
68+
sys.version_info < (3, 10),
69+
"aiokafka >= 0.13 requires Python 3.10+",
70+
)
6671
class TestAIOKafkaInstrumentation(TestBase, IsolatedAsyncioTestCase):
6772
@staticmethod
6873
def consumer_record_factory(
@@ -126,7 +131,7 @@ async def consumer_factory(**consumer_kwargs: Any) -> AIOKafkaConsumer:
126131

127132
@staticmethod
128133
async def producer_factory() -> AIOKafkaProducer:
129-
producer = AIOKafkaProducer(api_version="1.0")
134+
producer = AIOKafkaProducer()
130135

131136
producer.client._wait_on_metadata = mock.AsyncMock()
132137
producer.client.bootstrap = mock.AsyncMock()
@@ -498,37 +503,43 @@ async def test_send_and_wait(self) -> None:
498503
AIOKafkaInstrumentor().instrument(tracer_provider=self.tracer_provider)
499504

500505
producer = await self.producer_factory()
501-
add_message_mock: mock.AsyncMock = (
502-
producer._message_accumulator.add_message
503-
)
504-
add_message_mock.side_effect = [mock.AsyncMock()(), mock.AsyncMock()()]
505-
506-
tracer = self.tracer_provider.get_tracer(__name__)
507-
with tracer.start_as_current_span("test_span") as span:
508-
await producer.send_and_wait("topic_1", b"value_1")
506+
try:
507+
add_message_mock: mock.AsyncMock = (
508+
producer._message_accumulator.add_message
509+
)
510+
add_message_mock.side_effect = [
511+
mock.AsyncMock()(),
512+
mock.AsyncMock()(),
513+
]
509514

510-
add_message_mock.assert_awaited_with(
511-
TopicPartition(topic="topic_1", partition=1),
512-
None,
513-
b"value_1",
514-
40.0,
515-
timestamp_ms=None,
516-
headers=[("traceparent", mock.ANY)],
517-
)
518-
assert (
519-
add_message_mock.call_args_list[0]
520-
.kwargs["headers"][0][1]
521-
.startswith(
522-
f"00-{format_trace_id(span.get_span_context().trace_id)}-".encode()
515+
tracer = self.tracer_provider.get_tracer(__name__)
516+
with tracer.start_as_current_span("test_span") as span:
517+
await producer.send_and_wait("topic_1", b"value_1")
518+
519+
add_message_mock.assert_awaited_with(
520+
TopicPartition(topic="topic_1", partition=1),
521+
None,
522+
b"value_1",
523+
40.0,
524+
timestamp_ms=None,
525+
headers=[("traceparent", mock.ANY)],
526+
)
527+
assert (
528+
add_message_mock.call_args_list[0]
529+
.kwargs["headers"][0][1]
530+
.startswith(
531+
f"00-{format_trace_id(span.get_span_context().trace_id)}-".encode()
532+
)
523533
)
524-
)
525534

526-
await producer.send_and_wait("topic_2", b"value_2")
527-
add_message_mock.assert_awaited_with(
528-
TopicPartition(topic="topic_2", partition=1),
529-
None,
530-
b"value_2",
531-
40.0,
532-
timestamp_ms=None,
533-
headers=[("traceparent", mock.ANY)],
534-
)
535+
await producer.send_and_wait("topic_2", b"value_2")
536+
add_message_mock.assert_awaited_with(
537+
TopicPartition(topic="topic_2", partition=1),
538+
None,
539+
b"value_2",
540+
40.0,
541+
timestamp_ms=None,
542+
headers=[("traceparent", mock.ANY)],
543+
)
544+
finally:
545+
await producer.stop()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ async def test_wrap_getmany(
256256
extract_bootstrap_servers: mock.MagicMock,
257257
_enrich_getmany_poll_span: mock.MagicMock,
258258
_enrich_getmany_topic_span: mock.MagicMock,
259-
_create_consumer_span: mock.MagicMock,
259+
_create_consumer_span: mock.AsyncMock,
260260
extract: mock.MagicMock,
261261
) -> None:
262262
tracer = mock.MagicMock()
@@ -270,6 +270,7 @@ async def test_wrap_getmany(
270270
}
271271
)
272272
kafka_consumer = mock.MagicMock()
273+
_create_consumer_span.return_value = mock.MagicMock()
273274

274275
wrapped_getmany = _wrap_getmany(tracer, consume_hook)
275276
records = await wrapped_getmany(
@@ -370,7 +371,8 @@ async def test_create_consumer_span(
370371

371372
async def test_kafka_properties_extractor(self):
372373
aiokafka_instance_mock = mock.Mock()
373-
aiokafka_instance_mock._serialize.return_value = None, None
374+
aiokafka_instance_mock._key_serializer = None
375+
aiokafka_instance_mock._value_serializer = None
374376
aiokafka_instance_mock._partition.return_value = "partition"
375377
aiokafka_instance_mock.client._wait_on_metadata = mock.AsyncMock()
376378
assert (

0 commit comments

Comments
 (0)