diff --git a/logdna/logdna.py b/logdna/logdna.py index a430a2d..7597f45 100644 --- a/logdna/logdna.py +++ b/logdna/logdna.py @@ -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 @@ -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', @@ -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: diff --git a/tests/test_logger.py b/tests/test_logger.py index bb177e2..6ee1a08 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -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 @@ -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 @@ -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)