Skip to content

Commit 923e067

Browse files
authored
api: add support for floats to docker logs params since / until (#3031)
Add support for floats to docker logs params `since` / `until` since the Docker Engine APIs support it. This allows using fractional seconds for greater precision. Signed-off-by: Archi Moebius <[email protected]>
1 parent 1c27ec1 commit 923e067

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

docker/api/container.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,12 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
826826
tail (str or int): Output specified number of lines at the end of
827827
logs. Either an integer of number of lines or the string
828828
``all``. Default ``all``
829-
since (datetime or int): Show logs since a given datetime or
830-
integer epoch (in seconds)
829+
since (datetime, int, or float): Show logs since a given datetime,
830+
integer epoch (in seconds) or float (in fractional seconds)
831831
follow (bool): Follow log output. Default ``False``
832-
until (datetime or int): Show logs that occurred before the given
833-
datetime or integer epoch (in seconds)
832+
until (datetime, int, or float): Show logs that occurred before
833+
the given datetime, integer epoch (in seconds), or
834+
float (in fractional seconds)
834835
835836
Returns:
836837
(generator or str)
@@ -855,9 +856,11 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
855856
params['since'] = utils.datetime_to_timestamp(since)
856857
elif (isinstance(since, int) and since > 0):
857858
params['since'] = since
859+
elif (isinstance(since, float) and since > 0.0):
860+
params['since'] = since
858861
else:
859862
raise errors.InvalidArgument(
860-
'since value should be datetime or positive int, '
863+
'since value should be datetime or positive int/float, '
861864
'not {}'.format(type(since))
862865
)
863866

@@ -870,9 +873,11 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
870873
params['until'] = utils.datetime_to_timestamp(until)
871874
elif (isinstance(until, int) and until > 0):
872875
params['until'] = until
876+
elif (isinstance(until, float) and until > 0.0):
877+
params['until'] = until
873878
else:
874879
raise errors.InvalidArgument(
875-
'until value should be datetime or positive int, '
880+
'until value should be datetime or positive int/float, '
876881
'not {}'.format(type(until))
877882
)
878883

docker/models/containers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ def logs(self, **kwargs):
290290
tail (str or int): Output specified number of lines at the end of
291291
logs. Either an integer of number of lines or the string
292292
``all``. Default ``all``
293-
since (datetime or int): Show logs since a given datetime or
294-
integer epoch (in seconds)
293+
since (datetime, int, or float): Show logs since a given datetime,
294+
integer epoch (in seconds) or float (in nanoseconds)
295295
follow (bool): Follow log output. Default ``False``
296-
until (datetime or int): Show logs that occurred before the given
297-
datetime or integer epoch (in seconds)
296+
until (datetime, int, or float): Show logs that occurred before
297+
the given datetime, integer epoch (in seconds), or
298+
float (in nanoseconds)
298299
299300
Returns:
300301
(generator or str): Logs from the container.

tests/unit/api_container_test.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,22 @@ def test_log_since(self):
12791279
stream=False
12801280
)
12811281

1282+
def test_log_since_with_float(self):
1283+
ts = 809222400.000000
1284+
with mock.patch('docker.api.client.APIClient.inspect_container',
1285+
fake_inspect_container):
1286+
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
1287+
follow=False, since=ts)
1288+
1289+
fake_request.assert_called_with(
1290+
'GET',
1291+
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
1292+
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
1293+
'tail': 'all', 'since': ts},
1294+
timeout=DEFAULT_TIMEOUT_SECONDS,
1295+
stream=False
1296+
)
1297+
12821298
def test_log_since_with_datetime(self):
12831299
ts = 809222400
12841300
time = datetime.datetime.utcfromtimestamp(ts)
@@ -1301,7 +1317,7 @@ def test_log_since_with_invalid_value_raises_error(self):
13011317
fake_inspect_container):
13021318
with pytest.raises(docker.errors.InvalidArgument):
13031319
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
1304-
follow=False, since=42.42)
1320+
follow=False, since="42.42")
13051321

13061322
def test_log_tty(self):
13071323
m = mock.Mock()

0 commit comments

Comments
 (0)