Fix to allow topic to be passed via kwargs (#2901)

* Fix to allow topic to be imported from kwargs

* add changelog

* lint

* separate assert function
This commit is contained in:
Allen Kim
2024-10-22 17:22:07 +09:00
committed by GitHub
parent e4ece57a81
commit cef28d6f24
4 changed files with 22 additions and 5 deletions

View File

@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-aiokafka` Wrap `AIOKafkaConsumer.getone()` instead of `AIOKafkaConsumer.__anext__`
([#2874](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2874))
- `opentelemetry-instrumentation-confluent-kafka` Fix to allow `topic` to be extracted from `kwargs` in `produce()`
([#2901])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2901)
### Breaking changes

View File

@ -363,7 +363,9 @@ class ConfluentKafkaInstrumentor(BaseInstrumentor):
headers = []
kwargs["headers"] = headers
topic = KafkaPropertiesExtractor.extract_produce_topic(args)
topic = KafkaPropertiesExtractor.extract_produce_topic(
args, kwargs
)
_enrich_span(
span,
topic,

View File

@ -25,11 +25,9 @@ class KafkaPropertiesExtractor:
return kwargs.get(key, default_value)
@staticmethod
def extract_produce_topic(args):
def extract_produce_topic(args, kwargs):
"""extract topic from `produce` method arguments in Producer class"""
if len(args) > 0:
return args[0]
return "unknown"
return kwargs.get("topic") or (args[0] if args else "unknown")
@staticmethod
def extract_produce_headers(args, kwargs):

View File

@ -284,6 +284,15 @@ class TestConfluentKafka(TestBase):
expected_attribute_value, span.attributes[attribute_key]
)
def _assert_topic(self, span, expected_topic: str) -> None:
self.assertEqual(
span.attributes[SpanAttributes.MESSAGING_DESTINATION],
expected_topic,
)
def _assert_span_count(self, span_list, expected_count: int) -> None:
self.assertEqual(len(span_list), expected_count)
def test_producer_poll(self) -> None:
instrumentation = ConfluentKafkaInstrumentor()
message_queue = []
@ -299,6 +308,9 @@ class TestConfluentKafka(TestBase):
producer.produce(topic="topic-1", key="key-1", value="value-1")
msg = producer.poll()
self.assertIsNotNone(msg)
span_list = self.memory_exporter.get_finished_spans()
self._assert_span_count(span_list, 1)
self._assert_topic(span_list[0], "topic-1")
def test_producer_flush(self) -> None:
instrumentation = ConfluentKafkaInstrumentor()
@ -315,3 +327,6 @@ class TestConfluentKafka(TestBase):
producer.produce(topic="topic-1", key="key-1", value="value-1")
msg = producer.flush()
self.assertIsNotNone(msg)
span_list = self.memory_exporter.get_finished_spans()
self._assert_span_count(span_list, 1)
self._assert_topic(span_list[0], "topic-1")