Skip to content

Commit eead7a7

Browse files
authored
Fix Confluent Kafka Producer Arguments (#699)
* Add confluentkafka test for posargs/kwargs * Fix confluent kafka topic argument bug * More sensible producer arguments
1 parent eb28b52 commit eead7a7

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

newrelic/hooks/messagebroker_confluentkafka.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def wrap_Producer_produce(wrapped, instance, args, kwargs):
5555
topic = args[0]
5656
args = args[1:]
5757
else:
58-
topic = kwargs.get("topic", None)
58+
topic = kwargs.pop("topic", None)
5959

6060
transaction.add_messagebroker_info("Confluent-Kafka", get_package_version("confluent-kafka"))
6161

tests/messagebroker_confluentkafka/test_producer.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import time
1516
import threading
1617

1718
import pytest
@@ -36,34 +37,63 @@
3637
)
3738
@background_task()
3839
def test_produce_arguments(topic, producer, client_type, serialize, headers):
39-
callback_called = threading.Event()
40+
callback1_called = threading.Event()
41+
callback2_called = threading.Event()
42+
ts = int(time.time())
4043

41-
def producer_callback(err, msg):
42-
callback_called.set()
44+
def producer_callback1(err, msg):
45+
callback1_called.set()
46+
47+
def producer_callback2(err, msg):
48+
callback2_called.set()
4349

4450
if client_type == "cimpl":
51+
# Keyword Args
4552
producer.produce(
46-
topic,
53+
topic=topic,
4754
value=serialize({"foo": 1}),
4855
key=serialize("my-key"),
49-
callback=producer_callback,
50-
partition=1,
51-
timestamp=1,
56+
partition=0,
57+
callback=producer_callback2,
58+
timestamp=ts,
5259
headers=headers,
5360
)
54-
else:
61+
# Positional Args
5562
producer.produce(
5663
topic,
64+
serialize({"foo": 1}),
65+
serialize("my-key"),
66+
0,
67+
producer_callback1,
68+
None,
69+
ts,
70+
headers,
71+
)
72+
else:
73+
# Keyword Args
74+
producer.produce(
75+
topic=topic,
5776
value=serialize({"foo": 1}),
5877
key=serialize("my-key"),
59-
partition=1,
60-
on_delivery=producer_callback,
61-
timestamp=1,
78+
partition=0,
79+
on_delivery=producer_callback2,
80+
timestamp=ts,
6281
headers=headers,
6382
)
83+
# Positional Args
84+
producer.produce(
85+
topic,
86+
serialize("my-key"),
87+
serialize({"foo": 1}),
88+
0,
89+
producer_callback1,
90+
ts,
91+
headers,
92+
)
6493
producer.flush()
6594

66-
assert callback_called.wait(5), "Callback never called."
95+
assert callback1_called.wait(5), "Callback never called."
96+
assert callback2_called.wait(5), "Callback never called."
6797

6898

6999
def test_trace_metrics(topic, send_producer_message):

0 commit comments

Comments
 (0)