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

Bugfix using the getMessage method to get a LogRecord msg as a string #10

Merged
merged 1 commit into from
Oct 27, 2021
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
13 changes: 6 additions & 7 deletions ratelimitingfilter/ratelimitingfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def filter(self, record):
if bucket.limited > 0:
# Append a message to the record indicating the number of previously suppressed messages
record.msg = '{msg}{linesep}... {num} additional messages suppressed'.format(
msg=record.msg,
msg=record.getMessage(),
linesep=os.linesep,
num=bucket.limited
)
Expand All @@ -93,26 +93,25 @@ def _bucket_for(self, record):
def _get_substr_bucket(self, record):
# Locate the relevant token bucket by matching the configured substrings against the message
for substr in self._substr_buckets:
if substr in record.msg:
if substr in record.getMessage():
return self._substr_buckets[substr]

return None # None indicates no filtering

def _get_auto_bucket(self, record):
if record.msg in self._auto_buckets:
if record.getMessage() in self._auto_buckets:
# We have an exact match - there is a bucket configured for this message
return self._auto_buckets[record.msg]
return self._auto_buckets[record.getMessage()]

# Check whether we have a partial match - whether part of the message
# matches against a token bucket we previously created for a similar message
for msg, bucket in self._auto_buckets.items():
matcher = difflib.SequenceMatcher(None, msg, record.msg)
matcher = difflib.SequenceMatcher(None, msg, record.getMessage())
if matcher.ratio() >= 0.75: # Might want to make the ratio threshold configurable?
return bucket

# No match, so create a new bucket for this message
return self._auto_buckets[record.msg]

return self._auto_buckets[record.getMessage()]

class TokenBucket(object):
"""An implementation of the Token Bucket algorithm."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
test_suite='nose.collector',
tests_require=['mock==2.0'],
url='https://github.com/wkeeling/ratelimitingfilter',
version='1.2',
version='1.3',
)
8 changes: 7 additions & 1 deletion tests/ratelimitingfilter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def test_append_num_limited_records_to_message(self):
for _ in range(30):
mock_record = Mock()
mock_record.msg = 'test message'
mock_record.getMessage.return_value = 'test message'
if f.filter(mock_record):
filtered.append(mock_record)
self.mock_time.return_value += 0.1
Expand All @@ -114,8 +115,10 @@ def test_rate_limit_messages_matching_substring(self):

mock_matching_record = Mock()
mock_matching_record.msg = 'a rate limited test message'
mock_matching_record.getMessage.return_value = 'a rate limited test message'

mock_non_matching_record = Mock()
mock_non_matching_record.getMessage.return_value = 'a different test message'
mock_non_matching_record.msg = 'a different test message'

result = []
Expand All @@ -139,9 +142,11 @@ def test_rate_limit_messages_automatically(self):
for i in range(20):
mock_varying_record = Mock()
mock_varying_record.msg = 'a rate limited varying message: {varying}'.format(varying=i)
mock_varying_record.getMessage.return_value = 'a rate limited varying message: {varying}'.format(varying=i)

mock_rate_limited_record = Mock()
mock_rate_limited_record.msg = 'a completely different message'
mock_rate_limited_record.getMessage.return_value = 'a completely different message'

if f.filter(mock_varying_record):
# Only 2 of these get logged as they are considered the same message,
Expand All @@ -158,7 +163,8 @@ def test_rate_limit_messages_automatically(self):
def test_log_exception(self):
logger = logging.getLogger('test')
handler = logging.StreamHandler()
throttle = RateLimitingFilter(rate=1, per=1, burst=1)
config = {'match': 'auto'}
throttle = RateLimitingFilter(rate=1, per=1, burst=1, **config)
handler.addFilter(throttle)
logger.addHandler(handler)

Expand Down