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

Properly cache zookeeper connection strings #3333

Merged
merged 5 commits into from
Mar 21, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from kazoo.exceptions import NoNodeError
from six import iteritems, itervalues, string_types, text_type

from datadog_checks.base.utils.containers import hash_mutable
from datadog_checks.base import AgentCheck, is_affirmative

# Kafka Errors
Expand Down Expand Up @@ -493,13 +494,13 @@ def _get_consumer_offsets(self, client, consumer_group, topic_partitions, coord_
def _should_zk(self, zk_hosts_ports, interval, kafka_collect=False):
if not kafka_collect or not interval:
return True

zk_hosts_ports_hash = hash_mutable(zk_hosts_ports)
now = time()
last = self._zk_last_ts.get(zk_hosts_ports, 0)
last = self._zk_last_ts.get(zk_hosts_ports_hash, 0)

should_zk = False
if now - last >= interval:
self._zk_last_ts[zk_hosts_ports] = last
self._zk_last_ts[zk_hosts_ports_hash] = last
should_zk = True

return should_zk
Expand Down
16 changes: 15 additions & 1 deletion kafka_consumer/tests/test_kafka_consumer_zk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import copy
import time

import pytest
from six import iteritems

from datadog_checks.base.utils.containers import hash_mutable
from datadog_checks.kafka_consumer import KafkaCheck
from .common import HOST, PARTITIONS, TOPICS, is_supported
from .common import HOST, PARTITIONS, TOPICS, ZK_CONNECT_STR, is_supported

pytestmark = pytest.mark.skipif(
not is_supported('zookeeper'),
Expand Down Expand Up @@ -97,3 +99,15 @@ def test_check_nogroups_zk(aggregator, zk_instance):
else:
for mname in BROKER_METRICS + CONSUMER_METRICS:
aggregator.assert_metric(mname, at_least=1)


def test_should_zk():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💜

check = KafkaCheck('kafka_consumer', {}, {})
# Kafka Consumer Offsets set to True and we have a zk_connect_str that hasn't been run yet
assert (check._should_zk([ZK_CONNECT_STR, ZK_CONNECT_STR], 10, True) is True)
# Kafka Consumer Offsets is set to False, should immediately ZK
assert (check._should_zk(ZK_CONNECT_STR, 10, False) is True)
# Last time we checked ZK_CONNECT_STR was less than interval ago, shouldn't ZK
zk_connect_hash = hash_mutable(ZK_CONNECT_STR)
check._zk_last_ts[zk_connect_hash] = time.time()
assert (check._should_zk(ZK_CONNECT_STR, 100, True) is False)