-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit Prometheus/OpenMetrics checks to 2000 metrics per run by default (
#2093) * allow checks to limit the number of metric contexts they submit * set limit for prom checks to 2000 * set limits on all prom children checks to 0 (unlimited) * make the metric limit configurable * do not allow to disable limit if class has set one
- Loading branch information
Showing
17 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# (C) Datadog, Inc. 2018 | ||
# All rights reserved | ||
# Licensed under Simplified BSD License (see LICENSE) | ||
|
||
|
||
class Limiter(object): | ||
""" | ||
Limiter implements a simple cut-off capping logic for object count. | ||
It is used by the AgentCheck class to limit the number of metric contexts | ||
that can be set by an instance. | ||
""" | ||
def __init__(self, object_name, object_limit, warning_func=None): | ||
""" | ||
: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.reached_limit = False | ||
self.count = 0 | ||
self.seen = set() | ||
|
||
def reset(self): | ||
""" | ||
Resets state and uid set. To be called asap to free memory | ||
""" | ||
self.reached_limit = False | ||
self.count = 0 | ||
self.seen.clear() | ||
|
||
def is_reached(self, uid=None): | ||
""" | ||
is_reached is to be called for every object that counts towards the limit. | ||
- When called with no uid, the Limiter assumes this is a new object and | ||
unconditionally increments the counter (less CPU and memory usage). | ||
- When a given object can be passed multiple times, a uid must be provided to | ||
deduplicate calls. Only the first occurrence of a uid will increment the counter. | ||
:param uid: (optional) unique identifier of the object, to deduplicate calls | ||
:returns: boolean, true if limit exceeded | ||
""" | ||
if self.reached_limit: | ||
return True | ||
|
||
if uid: | ||
if uid in self.seen: | ||
return False | ||
self.count += 1 | ||
self.seen.add(uid) | ||
else: | ||
self.count += 1 | ||
|
||
if self.count > self.limit: | ||
if self.warning: | ||
self.warning("Exceeded limit of {} {}, ignoring next ones".format(self.limit, self.name)) | ||
self.reached_limit = True | ||
return True | ||
return False | ||
|
||
def get_status(self): | ||
""" | ||
Returns the internal state of the limiter for unit tests | ||
""" | ||
return (self.count, self.limit, self.reached_limit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.