Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new kafka consumer client only if needed #19658

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kafka_consumer/changelog.d/19658.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create new kafka consumer client only if needed when fetching highwater offsets
12 changes: 10 additions & 2 deletions kafka_consumer/datadog_checks/kafka_consumer/kafka_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,22 @@ def get_highwater_offsets(self, consumer_offsets):

topic_partition_checked = set()

for consumer_group, _topic, _partition in consumer_offsets:
sorted_consumer_offsets = dict(sorted(consumer_offsets.items(), key=lambda item: item[0][0]))
current_group = None

for consumer_group, _topic, _partition in sorted_consumer_offsets:
self.log.debug('CONSUMER GROUP: %s', consumer_group)
if (_topic, _partition) in topic_partition_checked:
self.log.debug('Highwater offset already collected for topic %s with partition %s', _topic, _partition)
continue

topic_partitions_for_highwater_offsets = set()
if current_group != consumer_group:
if current_group is not None:
self.client.close_consumer()
current_group = consumer_group
self.client.open_consumer(consumer_group)

self.client.open_consumer(consumer_group)
cluster_id, topics = self.client.consumer_get_cluster_id_and_list_topics(consumer_group)

for topic, partitions in topics:
Expand Down Expand Up @@ -363,6 +370,7 @@ def get_highwater_offsets(self, consumer_offsets):
else:
self.log.debug('No new highwater offsets to query for consumer group %s', consumer_group)

if current_group is not None:
self.client.close_consumer()

self.log.debug('Got %s highwater offsets', len(highwater_offsets))
Expand Down