-
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.
allow checks to limit the number of metric contexts they submit
- Loading branch information
Showing
8 changed files
with
183 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# (C) Datadog, Inc. 2010-2016 | ||
# All rights reserved | ||
# Licensed under Simplified BSD License (see LICENSE) | ||
|
||
|
||
class Limiter(object): | ||
def __init__(self, object_name, object_limit, warning_func=None): | ||
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): | ||
self.reached_limit = False | ||
self.count = 0 | ||
self.seen.clear() | ||
|
||
def is_reached(self, uid=None): | ||
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): | ||
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