Skip to content

Commit

Permalink
api: add support for floats to docker logs params since / until (#3031)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ArchiMoebius authored Aug 19, 2022
1 parent 1c27ec1 commit 923e067
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
17 changes: 11 additions & 6 deletions docker/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,12 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
tail (str or int): Output specified number of lines at the end of
logs. Either an integer of number of lines or the string
``all``. Default ``all``
since (datetime or int): Show logs since a given datetime or
integer epoch (in seconds)
since (datetime, int, or float): Show logs since a given datetime,
integer epoch (in seconds) or float (in fractional seconds)
follow (bool): Follow log output. Default ``False``
until (datetime or int): Show logs that occurred before the given
datetime or integer epoch (in seconds)
until (datetime, int, or float): Show logs that occurred before
the given datetime, integer epoch (in seconds), or
float (in fractional seconds)
Returns:
(generator or str)
Expand All @@ -855,9 +856,11 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
params['since'] = utils.datetime_to_timestamp(since)
elif (isinstance(since, int) and since > 0):
params['since'] = since
elif (isinstance(since, float) and since > 0.0):
params['since'] = since
else:
raise errors.InvalidArgument(
'since value should be datetime or positive int, '
'since value should be datetime or positive int/float, '
'not {}'.format(type(since))
)

Expand All @@ -870,9 +873,11 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
params['until'] = utils.datetime_to_timestamp(until)
elif (isinstance(until, int) and until > 0):
params['until'] = until
elif (isinstance(until, float) and until > 0.0):
params['until'] = until
else:
raise errors.InvalidArgument(
'until value should be datetime or positive int, '
'until value should be datetime or positive int/float, '
'not {}'.format(type(until))
)

Expand Down
9 changes: 5 additions & 4 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,12 @@ def logs(self, **kwargs):
tail (str or int): Output specified number of lines at the end of
logs. Either an integer of number of lines or the string
``all``. Default ``all``
since (datetime or int): Show logs since a given datetime or
integer epoch (in seconds)
since (datetime, int, or float): Show logs since a given datetime,
integer epoch (in seconds) or float (in nanoseconds)
follow (bool): Follow log output. Default ``False``
until (datetime or int): Show logs that occurred before the given
datetime or integer epoch (in seconds)
until (datetime, int, or float): Show logs that occurred before
the given datetime, integer epoch (in seconds), or
float (in nanoseconds)
Returns:
(generator or str): Logs from the container.
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/api_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,22 @@ def test_log_since(self):
stream=False
)

def test_log_since_with_float(self):
ts = 809222400.000000
with mock.patch('docker.api.client.APIClient.inspect_container',
fake_inspect_container):
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
follow=False, since=ts)

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all', 'since': ts},
timeout=DEFAULT_TIMEOUT_SECONDS,
stream=False
)

def test_log_since_with_datetime(self):
ts = 809222400
time = datetime.datetime.utcfromtimestamp(ts)
Expand All @@ -1301,7 +1317,7 @@ def test_log_since_with_invalid_value_raises_error(self):
fake_inspect_container):
with pytest.raises(docker.errors.InvalidArgument):
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
follow=False, since=42.42)
follow=False, since="42.42")

def test_log_tty(self):
m = mock.Mock()
Expand Down

0 comments on commit 923e067

Please sign in to comment.