diff --git a/HISTORY.md b/HISTORY.md index c730b6d..613a38c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,12 @@ +# 2.3.5 / 2025-11-18 +- Fix for Github Issue #516 +- Fix for Github Issue #515 +- Fix for Github Issue #493 +- Updated the version to 2.3.5 +- Enhanced retry logic in segment/analytics/consumer.py by adding detailed debug logs for each retry attempt and logging an error when all retries are exhausted. +- Moved the return success out of the finally block in segment/analytics/consumer.py at Line 84. +- Removed redundant error logging in segment/analytics/request.py to avoid duplicate logs when exceptions are raised. + # 2.3.4 / 2025-2-24 - Fix for proxy values not being used diff --git a/segment/analytics/consumer.py b/segment/analytics/consumer.py index 1c47157..157e3c9 100644 --- a/segment/analytics/consumer.py +++ b/segment/analytics/consumer.py @@ -14,9 +14,11 @@ # lower to leave space for extra data that will be added later, eg. "sentAt". BATCH_SIZE_LIMIT = 475000 + class FatalError(Exception): def __init__(self, message): self.message = message + def __str__(self): msg = "[Segment] {0})" return msg.format(self.message) @@ -81,7 +83,7 @@ def upload(self): # mark items as acknowledged from queue for _ in batch: self.queue.task_done() - return success + return success def next(self): """Return the next batch of items to upload.""" @@ -132,14 +134,26 @@ def fatal_exception(exc): # retry on all other errors (eg. network) return False + attempt_count = 0 + @backoff.on_exception( backoff.expo, Exception, max_tries=self.retries + 1, - giveup=fatal_exception) + giveup=fatal_exception, + on_backoff=lambda details: self.log.debug( + f"Retry attempt {details['tries']}/{self.retries + 1} after {details['elapsed']:.2f}s" + )) def send_request(): - post(self.write_key, self.host, gzip=self.gzip, - timeout=self.timeout, batch=batch, proxies=self.proxies, - oauth_manager=self.oauth_manager) + nonlocal attempt_count + attempt_count += 1 + try: + return post(self.write_key, self.host, gzip=self.gzip, + timeout=self.timeout, batch=batch, proxies=self.proxies, + oauth_manager=self.oauth_manager) + except Exception as e: + if attempt_count >= self.retries + 1: + self.log.error(f"All {self.retries} retries exhausted. Final error: {e}") + raise send_request() diff --git a/segment/analytics/request.py b/segment/analytics/request.py index 4118e2c..ab92b80 100644 --- a/segment/analytics/request.py +++ b/segment/analytics/request.py @@ -54,9 +54,8 @@ def post(write_key, host=None, gzip=False, timeout=15, proxies=None, oauth_manag try: res = _session.post(url, **kwargs) except Exception as e: - log.error(e) raise e - + if res.status_code == 200: log.debug('data uploaded successfully') return res diff --git a/segment/analytics/version.py b/segment/analytics/version.py index f82ed75..b4ab188 100644 --- a/segment/analytics/version.py +++ b/segment/analytics/version.py @@ -1 +1 @@ -VERSION = '2.3.4' +VERSION = '2.3.5'