Skip to content

Commit

Permalink
fix: added retries for http 429 504 (#93)
Browse files Browse the repository at this point in the history
- added retries for http 429 504

LOG-11814
  • Loading branch information
dkhokhlov authored Jan 27, 2023
1 parent 6ea7450 commit 712d81d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
14 changes: 12 additions & 2 deletions logdna/logdna.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ def send_request(self, data): # noqa: max-complexity: 13
3XX unexpected status
401, 403 expected client error,
invalid ingestion key
429 expected server error,
"client error", transient
4XX unexpected client error
500 502 503 507 expected server error, transient
500 502 503 504 507 expected server error, transient
5XX unexpected server error
handling:
expected status discard flush buffer
Expand Down Expand Up @@ -256,6 +258,14 @@ def send_request(self, data): # noqa: max-complexity: 13
'Error Response: %s', response.text)
return True # discard

if status_code == 429:
self.internalLogger.debug('Client Error: %s. Retrying...',
reason)
if self.log_error_response:
self.internalLogger.debug(
'Error Response: %s', response.text)
return False # retry

if 400 <= status_code <= 499:
self.internalLogger.debug('Client Error: %s. ' +
'Discarding flush buffer',
Expand All @@ -265,7 +275,7 @@ def send_request(self, data): # noqa: max-complexity: 13
'Error Response: %s', response.text)
return True # discard

if status_code in [500, 502, 503, 507]:
if status_code in [500, 502, 503, 504, 507]:
self.internalLogger.debug('Server Error: %s. Retrying...',
reason)
if self.log_error_response:
Expand Down
48 changes: 45 additions & 3 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,49 @@ def test_try_request_500(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 500
r.reason = 'OK'
r.reason = 'Internal Server Error'
post_mock.return_value = r
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
sample_message['timestamp'] = unittest.mock.ANY
handler.buf = [sample_message]
handler.try_request()
self.assertTrue(handler.exception_flag)
self.assertTrue(post_mock.call_count, 3)

@mock.patch('time.time', unittest.mock.MagicMock(return_value=now))
def test_try_request_502(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 502
r.reason = 'Bad Gateway'
post_mock.return_value = r
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
sample_message['timestamp'] = unittest.mock.ANY
handler.buf = [sample_message]
handler.try_request()
self.assertTrue(handler.exception_flag)
self.assertTrue(post_mock.call_count, 3)

@mock.patch('time.time', unittest.mock.MagicMock(return_value=now))
def test_try_request_504(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 504
r.reason = 'Gateway Timeout'
post_mock.return_value = r
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
sample_message['timestamp'] = unittest.mock.ANY
handler.buf = [sample_message]
handler.try_request()
self.assertTrue(handler.exception_flag)
self.assertTrue(post_mock.call_count, 3)

@mock.patch('time.time', unittest.mock.MagicMock(return_value=now))
def test_try_request_429(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 429
r.reason = 'Too Many Requests'
post_mock.return_value = r
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
sample_message['timestamp'] = unittest.mock.ANY
Expand All @@ -185,7 +227,7 @@ def test_try_request_403(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 403
r.reason = 'OK'
r.reason = 'Forbidden'
post_mock.return_value = r
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
sample_message['timestamp'] = unittest.mock.ANY
Expand All @@ -199,7 +241,7 @@ def test_try_request_403_log_response(self):
with patch('requests.post') as post_mock:
r = requests.Response()
r.status_code = 403
r.reason = 'OK'
r.reason = 'Forbidden'
post_mock.return_value = r
sample_options['log_error_response'] = True
handler = LogDNAHandler(LOGDNA_API_KEY, sample_options)
Expand Down

0 comments on commit 712d81d

Please sign in to comment.