Skip to content

Commit e9d4ddf

Browse files
api: add one-shot option to container stats (#3089)
Signed-off-by: Andy Roxby <[email protected]>
1 parent aca129d commit e9d4ddf

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

docker/api/container.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ def start(self, container, *args, **kwargs):
11261126
self._raise_for_status(res)
11271127

11281128
@utils.check_resource('container')
1129-
def stats(self, container, decode=None, stream=True):
1129+
def stats(self, container, decode=None, stream=True, one_shot=None):
11301130
"""
11311131
Stream statistics for a specific container. Similar to the
11321132
``docker stats`` command.
@@ -1138,23 +1138,39 @@ def stats(self, container, decode=None, stream=True):
11381138
False by default.
11391139
stream (bool): If set to false, only the current stats will be
11401140
returned instead of a stream. True by default.
1141+
one_shot (bool): If set to true, Only get a single stat instead of
1142+
waiting for 2 cycles. Must be used with stream=false. False by
1143+
default.
11411144
11421145
Raises:
11431146
:py:class:`docker.errors.APIError`
11441147
If the server returns an error.
11451148
11461149
"""
11471150
url = self._url("/containers/{0}/stats", container)
1151+
params = {
1152+
'stream': stream
1153+
}
1154+
if one_shot is not None:
1155+
if utils.version_lt(self._version, '1.41'):
1156+
raise errors.InvalidVersion(
1157+
'one_shot is not supported for API version < 1.41'
1158+
)
1159+
params['one-shot'] = one_shot
11481160
if stream:
1149-
return self._stream_helper(self._get(url, stream=True),
1161+
if one_shot:
1162+
raise errors.InvalidArgument(
1163+
'one_shot is only available in conjunction with '
1164+
'stream=False'
1165+
)
1166+
return self._stream_helper(self._get(url, params=params),
11501167
decode=decode)
11511168
else:
11521169
if decode:
11531170
raise errors.InvalidArgument(
11541171
"decode is only available in conjunction with stream=True"
11551172
)
1156-
return self._result(self._get(url, params={'stream': False}),
1157-
json=True)
1173+
return self._result(self._get(url, params=params), json=True)
11581174

11591175
@utils.check_resource('container')
11601176
def stop(self, container, timeout=None):

tests/unit/api_container_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,18 @@ def test_container_stats(self):
15291529
'GET',
15301530
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
15311531
timeout=60,
1532-
stream=True
1532+
params={'stream': True}
1533+
)
1534+
1535+
def test_container_stats_with_one_shot(self):
1536+
self.client.stats(
1537+
fake_api.FAKE_CONTAINER_ID, stream=False, one_shot=True)
1538+
1539+
fake_request.assert_called_with(
1540+
'GET',
1541+
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
1542+
timeout=60,
1543+
params={'stream': False, 'one-shot': True}
15331544
)
15341545

15351546
def test_container_top(self):

0 commit comments

Comments
 (0)