Skip to content

Commit f5854aa

Browse files
Rahul D. GhosalYun-Kimmabdinur
authored
fix(kafka): pass *args, **kwargs to TracedConsumer.__init__ (#5887)
Pass arbitrary positional and keyword arguments to `TracedConsumer` so as to allow client to configure their instance of `KafkaConsumer`. This is, for example, [required for configuring a logger](https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#kafka-client-configuration) internal to the Consumer, where a logger can only be passed as a kwarg. Currently, passing such a kwarg leads to the following error: ```TypeError: TracedConsumer.__init__() got an unexpected keyword argument 'logger'``` Related to issue [#5837](#5873) --- ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] OPTIONAL: PR description includes explicit acknowledgement of the performance implications of the change as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. --------- Co-authored-by: Yun Kim <[email protected]> Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent bb5dccc commit f5854aa

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

ddtrace/contrib/kafka/patch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def __bool__(self):
3939

4040

4141
class TracedConsumer(confluent_kafka.Consumer):
42-
def __init__(self, config):
43-
super(TracedConsumer, self).__init__(config)
42+
def __init__(self, config, *args, **kwargs):
43+
super(TracedConsumer, self).__init__(config, *args, **kwargs)
4444
self._group_id = config["group.id"]
4545

4646
def poll(self, timeout=1):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
kafka: Fixes ``TypeError` raised when artibrary keyword arguments are passed to ``confluent_kafka.Consumer``

tests/contrib/kafka/test_kafka.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import confluent_kafka
24
import pytest
35
import six
@@ -63,6 +65,21 @@ def consumer(tracer):
6365
_consumer.close()
6466

6567

68+
def test_consumer_created_with_logger_does_not_raise(tracer):
69+
"""Test that adding a logger to a Consumer init does not raise any errors."""
70+
logger = logging.getLogger()
71+
# regression test for DataDog/dd-trace-py/issues/5873
72+
consumer = confluent_kafka.Consumer(
73+
{
74+
"bootstrap.servers": BOOTSTRAP_SERVERS,
75+
"group.id": GROUP_ID,
76+
"auto.offset.reset": "earliest",
77+
},
78+
logger=logger,
79+
)
80+
consumer.close()
81+
82+
6683
@pytest.mark.parametrize("tombstone", [False, True])
6784
@pytest.mark.snapshot(ignores=["metrics.kafka.message_offset"])
6885
def test_message(producer, consumer, tombstone):

0 commit comments

Comments
 (0)