Skip to content

Commit ff78c51

Browse files
committed
Catch and retry request exceptions
1 parent ddceff1 commit ff78c51

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

logtail/flusher.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ def step(self):
5656
# request fails in a way that can be retried, it is retried with an
5757
# exponential backoff in between attempts.
5858
if frame:
59+
response = None
5960
for delay in RETRY_SCHEDULE + (None, ):
6061
response = self.upload(frame)
6162
if not _should_retry(response.status_code):
6263
break
6364
if delay is not None:
6465
time.sleep(delay)
6566

67+
if response.status_code == 500 and getattr(response, "exception") != None:
68+
print('Failed to send logs to Better Stack after {} retries: {}'.format(len(RETRY_SCHEDULE), response.exception))
69+
6670
if shutdown:
6771
sys.exit(0)
6872

logtail/uploader.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import msgpack
44
import requests
55

6+
class Fake500(object):
7+
def __init__(self, exception):
8+
self.status_code = 500
9+
self.exception = exception
610

711
class Uploader(object):
812
def __init__(self, source_token, host):
@@ -16,4 +20,7 @@ def __init__(self, source_token, host):
1620

1721
def __call__(self, frame):
1822
data = msgpack.packb(frame, use_bin_type=True)
19-
return self.session.post(self.host, data=data, headers=self.headers)
23+
try:
24+
return self.session.post(self.host, data=data, headers=self.headers)
25+
except requests.RequestException as e:
26+
return Fake500(e)

0 commit comments

Comments
 (0)