Skip to content

Commit fee6615

Browse files
committed
T-9413 Add configurable timeout
1 parent 9f766e4 commit fee6615

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

logtail/handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
DEFAULT_RAISE_EXCEPTIONS = False
1717
DEFAULT_DROP_EXTRA_EVENTS = True
1818
DEFAULT_INCLUDE_EXTRA_ATTRIBUTES = True
19+
DEFAULT_TIMEOUT = 30
1920

2021

2122
class LogtailHandler(logging.Handler):
@@ -29,6 +30,7 @@ def __init__(self,
2930
drop_extra_events=DEFAULT_DROP_EXTRA_EVENTS,
3031
include_extra_attributes=DEFAULT_INCLUDE_EXTRA_ATTRIBUTES,
3132
context=DEFAULT_CONTEXT,
33+
timeout=DEFAULT_TIMEOUT,
3234
level=logging.NOTSET):
3335
super(LogtailHandler, self).__init__(level=level)
3436
self.source_token = source_token
@@ -38,7 +40,7 @@ def __init__(self,
3840
self.host = "https://" + host
3941
self.context = context
4042
self.pipe = queue.Queue(maxsize=buffer_capacity)
41-
self.uploader = Uploader(self.source_token, self.host)
43+
self.uploader = Uploader(self.source_token, self.host, timeout)
4244
self.drop_extra_events = drop_extra_events
4345
self.include_extra_attributes = include_extra_attributes
4446
self.buffer_capacity = buffer_capacity

logtail/uploader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ def __init__(self, exception):
99
self.exception = exception
1010

1111
class Uploader(object):
12-
def __init__(self, source_token, host):
12+
def __init__(self, source_token, host, timeout):
1313
self.source_token = source_token
1414
self.host = host
15+
self.timeout = timeout
1516
self.session = requests.Session()
1617
self.headers = {
1718
'Authorization': 'Bearer %s' % source_token,
@@ -21,6 +22,6 @@ def __init__(self, source_token, host):
2122
def __call__(self, frame):
2223
data = msgpack.packb(frame, use_bin_type=True)
2324
try:
24-
return self.session.post(self.host, data=data, headers=self.headers)
25+
return self.session.post(self.host, data=data, headers=self.headers, timeout=self.timeout)
2526
except requests.RequestException as e:
26-
return Fake500(e)
27+
return Fake500(e)

tests/test_handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def test_handler_creates_uploader_from_args(self, MockWorker):
2020
handler = LogtailHandler(source_token=self.source_token, host=self.host)
2121
self.assertEqual(handler.uploader.source_token, self.source_token)
2222
self.assertEqual(handler.uploader.host, "https://" + self.host)
23+
24+
@patch('logtail.handler.FlushWorker')
25+
def test_handler_passes_timeout_to_uploader(self, MockWorker):
26+
# Test default timeout
27+
handler = LogtailHandler(source_token=self.source_token, host=self.host)
28+
self.assertEqual(handler.uploader.timeout, 30)
29+
30+
# Test custom timeout
31+
handler = LogtailHandler(source_token=self.source_token, host=self.host, timeout=10)
32+
self.assertEqual(handler.uploader.timeout, 10)
2333

2434
@patch('logtail.handler.FlushWorker')
2535
def test_handler_creates_pipe_from_args(self, MockWorker):

tests/test_uploader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestUploader(unittest.TestCase):
1616

1717
@patch('logtail.uploader.requests.Session.post')
1818
def test_call(self, post):
19-
def mock_post(endpoint, data=None, headers=None):
19+
def mock_post(endpoint, data=None, headers=None, timeout=None):
2020
# Check that the data is sent to ther correct endpoint
2121
self.assertEqual(endpoint, self.host)
2222
# Check the content-type
@@ -25,6 +25,8 @@ def mock_post(endpoint, data=None, headers=None):
2525
self.assertEqual('application/msgpack', headers.get('Content-Type'))
2626
# Check the content was msgpacked correctly
2727
self.assertEqual(msgpack.unpackb(data, raw=False), self.frame)
28+
# Check that timeout is passed to the request
29+
self.assertEqual(timeout, 30)
2830

2931
post.side_effect = mock_post
3032
u = Uploader(self.source_token, self.host)

0 commit comments

Comments
 (0)