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

base: add check name to Limiter warning message #2391

Merged
merged 1 commit into from
Oct 12, 2018
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
2 changes: 1 addition & 1 deletion datadog_checks_base/datadog_checks/base/checks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, *args, **kwargs):
except Exception:
metric_limit = self.DEFAULT_METRIC_LIMIT
if metric_limit > 0:
self.metric_limiter = Limiter("metrics", metric_limit, self.warning)
self.metric_limiter = Limiter(self.name, "metrics", metric_limit, self.warning)

@property
def in_developer_mode(self):
Expand Down
6 changes: 4 additions & 2 deletions datadog_checks_base/datadog_checks/base/utils/limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class Limiter(object):
It is used by the AgentCheck class to limit the number of sets of tags
that can be set by an instance.
"""
def __init__(self, object_name, object_limit, warning_func=None):
def __init__(self, check_name, object_name, object_limit, warning_func=None):
"""
:param check_name: name of the check using this limiter
:param object_name: (plural) name of counted objects for warning wording
:param object_limit: maximum number of objects to accept before limiting
:param warning_func: callback function, called with a string when limit is exceeded
"""
self.warning = warning_func
self.name = object_name
self.limit = object_limit
self.check_name = check_name

self.reached_limit = False
self.count = 0
Expand Down Expand Up @@ -55,7 +57,7 @@ def is_reached(self, uid=None):

if self.count > self.limit:
if self.warning:
self.warning("Exceeded limit of {} {}, ignoring next ones".format(self.limit, self.name))
self.warning("Check {} exceeded limit of {} {}, ignoring next ones".format(self.check_name, self.limit, self.name))
self.reached_limit = True
return True
return False
Expand Down
10 changes: 5 additions & 5 deletions datadog_checks_base/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ def test_key_function(self):
class TestLimiter():
def test_no_uid(self):
warnings = []
limiter = Limiter("names", 10, warning_func=warnings.append)
limiter = Limiter("my_check", "names", 10, warning_func=warnings.append)
for i in range(0, 10):
assert limiter.is_reached() is False
assert limiter.get_status() == (10, 10, False)

# Reach limit
assert limiter.is_reached() is True
assert limiter.get_status() == (11, 10, True)
assert warnings == ["Exceeded limit of 10 names, ignoring next ones"]
assert warnings == ["Check my_check exceeded limit of 10 names, ignoring next ones"]

# Make sure warning is only sent once
assert limiter.is_reached() is True
assert len(warnings) == 1

def test_with_uid(self):
warnings = []
limiter = Limiter("names", 10, warning_func=warnings.append)
limiter = Limiter("my_check", "names", 10, warning_func=warnings.append)
for i in range(0, 20):
assert limiter.is_reached("dummy1") is False
assert limiter.get_status() == (1, 10, False)
Expand All @@ -84,7 +84,7 @@ def test_with_uid(self):
assert len(warnings) == 0

def test_mixed(self):
limiter = Limiter("names", 10)
limiter = Limiter("my_check", "names", 10)

for i in range(0, 20):
assert limiter.is_reached("dummy1") is False
Expand All @@ -95,7 +95,7 @@ def test_mixed(self):
assert limiter.get_status() == (6, 10, False)

def test_reset(self):
limiter = Limiter("names", 10)
limiter = Limiter("my_check", "names", 10)

for i in range(1, 20):
limiter.is_reached("dummy1")
Expand Down