Skip to content

Commit 6176ea1

Browse files
kafka_consumer: reuse AdminClient and Consumer connections across runs (DataDog#24552)
* kafka_consumer: reuse AdminClient and Consumer across runs Recreating the librdkafka AdminClient (close_admin_client defaulted to true) and opening/closing a Consumer on every run tore down and respawned librdkafka's per-broker threads each collection. On high-core hosts that spreads allocations across many glibc arenas whose freed memory is never reused or returned, so agent RSS climbs unbounded. Keep both clients alive (default close_admin_client to false, make open_consumer idempotent, and stop closing the consumer used for cluster-id detection) so the same threads and arenas are reused each run. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Add changelog entry for connection reuse Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Drop inline rationale comments (kept in PR/changelog) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Guard consumer reuse under close_admin_client flag close_admin_client now controls reuse of both clients: when false (default) the AdminClient and Consumer are both reused across runs; when true, both are closed each run (previous behavior). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Drop explanatory comment on consumer close guard Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Keep close_admin_client default unchanged (true) Reuse of both clients is opt-in via close_admin_client: false; the default behavior (closing clients each run) is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Reword changelog: reuse is opt-in via close_admin_client Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 433b81a commit 6176ea1

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow reusing the Kafka AdminClient and Consumer across check runs (via close_admin_client: false) to avoid unbounded agent memory growth from librdkafka thread churn.

kafka_consumer/datadog_checks/kafka_consumer/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def kafka_client(self):
4242
return self._kafka_client
4343

4444
def open_consumer(self, consumer_group):
45+
if self._consumer is not None:
46+
return
47+
4548
config = {
4649
"bootstrap.servers": self.config._kafka_connect_str,
4750
"group.id": consumer_group,
@@ -55,8 +58,11 @@ def open_consumer(self, consumer_group):
5558
self.log.debug("Consumer instance %s created for group %s", self._consumer, consumer_group)
5659

5760
def close_consumer(self):
61+
if self._consumer is None:
62+
return
5863
self.log.debug("Closing consumer instance %s", self._consumer)
5964
self._consumer.close()
65+
self._consumer = None
6066

6167
def __get_authentication_config(self):
6268
config = {

kafka_consumer/datadog_checks/kafka_consumer/kafka_consumer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ def get_highwater_offsets(self, partitions=None):
543543
):
544544
result[(topic, partition)] = offset
545545
finally:
546-
self.client.close_consumer()
546+
if self.config._close_admin_client:
547+
self.client.close_consumer()
547548

548549
self.log.debug('Got %s highwater offsets', len(result))
549550
return result, cluster_id

0 commit comments

Comments
 (0)