Skip to content

KafkaPropertiesExtractor not extracting topic from kwargs #2083

@noamfisher

Description

@noamfisher

When using confluent_kafka producer with opentelemetry-instrumentation-confluent-kafka,
Using the confluent_kafka.Producer.produce with topic in kwargs will result in opentelemetry failing to extract the topic.

Steps to reproduce

from opentelemetry.instrumentation.confluent_kafka import ConfluentKafkaInstrumentor
ConfluentKafkaInstrumentor().instrument()

from confluent_kafka import Producer
producer = Producer(...)
producer.produce(topic="my.topic", value=b'data')

What is the expected behavior?
OTEL exports data about a produced message to topic my.topic

What is the actual behavior?
OTEL fails to export - the topic is failed to be extracted in ConfluentKafkaInstrumentor.wrap_produce.

Using the following code will work, passing the topic as arg instead of kwarg

producer.produce("my.topic", value=b'data')

Additional context
I can see the bug clearly. In the beginning of ConfluentKafkaInstrumentor.wrap_produce,
The topic is extracted using this code:

        topic = kwargs.get("topic")
        if not topic:
            topic = args[0]

Later in the method, the topic is extracted again, using

topic = KafkaPropertiesExtractor.extract_produce_topic(args)

This however is looking at args only and not covering cases of the topic being in kwargs.
I suggest moving the code above that looks at both args and kwargs into KafkaPropertiesExtractor.extract_produce_topic

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions