Skip to content

Commit 5e0787d

Browse files
authored
Fix typo when saving broker timestamp (#18307)
* test * test * fix typo in the key * add logging * add persistent_cache_key * changelog
1 parent 276d38c commit 5e0787d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a typo when writing to persistent cache to calculate the estimated consumer lag.

kafka_consumer/datadog_checks/kafka_consumer/kafka_consumer.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ def check(self, _):
4949
highwater_offsets = {}
5050
broker_timestamps = defaultdict(dict)
5151
cluster_id = ""
52+
persistent_cache_key = "broker_timestamps_"
5253
try:
5354
if len(consumer_offsets) < self._context_limit:
5455
# Fetch highwater offsets
5556
# Expected format: ({(topic, partition): offset}, cluster_id)
5657
highwater_offsets, cluster_id = self.client.get_highwater_offsets(consumer_offsets)
5758
if self._data_streams_enabled:
58-
broker_timestamps = self._load_broker_timestamps()
59+
broker_timestamps = self._load_broker_timestamps(persistent_cache_key)
5960
self._add_broker_timestamps(broker_timestamps, highwater_offsets)
60-
self._save_broker_timestamps(broker_timestamps)
61+
self._save_broker_timestamps(broker_timestamps, persistent_cache_key)
6162
else:
6263
self.warning("Context limit reached. Skipping highwater offset collection.")
6364
except Exception:
@@ -94,11 +95,11 @@ def check(self, _):
9495
if self.config._close_admin_client:
9596
self.client.close_admin_client()
9697

97-
def _load_broker_timestamps(self):
98+
def _load_broker_timestamps(self, persistent_cache_key):
9899
"""Loads broker timestamps from persistent cache."""
99100
broker_timestamps = defaultdict(dict)
100101
try:
101-
for topic_partition, content in json.loads(self.read_persistent_cache("broker_timestamps_")).items():
102+
for topic_partition, content in json.loads(self.read_persistent_cache(persistent_cache_key)).items():
102103
for offset, timestamp in content.items():
103104
broker_timestamps[topic_partition][int(offset)] = timestamp
104105
except Exception as e:
@@ -113,9 +114,9 @@ def _add_broker_timestamps(self, broker_timestamps, highwater_offsets):
113114
if len(timestamps) > self._max_timestamps:
114115
del timestamps[min(timestamps)]
115116

116-
def _save_broker_timestamps(self, broker_timestamps):
117+
def _save_broker_timestamps(self, broker_timestamps, persistent_cache_key):
117118
"""Saves broker timestamps to persistent cache."""
118-
self.write_persistent_cache("broker_timestamps", json.dumps(broker_timestamps))
119+
self.write_persistent_cache(persistent_cache_key, json.dumps(broker_timestamps))
119120

120121
def report_highwater_offsets(self, highwater_offsets, contexts_limit, cluster_id):
121122
"""Report the broker highwater offsets."""

0 commit comments

Comments
 (0)