Skip to content

Commit 0284ead

Browse files
committed
Merge pull request #805 from sourcelair/enhancement/stats-no-stream
Add support for non-stream stats of containers
2 parents 4e441b2 + 7e2ec1a commit 0284ead

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
max_line_length = 80
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

docker/api/container.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,14 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
356356

357357
@utils.minimum_version('1.17')
358358
@utils.check_resource
359-
def stats(self, container, decode=None):
359+
def stats(self, container, decode=None, stream=True):
360360
url = self._url("/containers/{0}/stats", container)
361-
return self._stream_helper(self._get(url, stream=True), decode=decode)
361+
if stream:
362+
return self._stream_helper(self._get(url, stream=True),
363+
decode=decode)
364+
else:
365+
return self._result(self._get(url, params={'stream': False}),
366+
json=True)
362367

363368
@utils.check_resource
364369
def stop(self, container, timeout=10):

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,8 @@ This will stream statistics for a specific container.
874874
* container (str): The container to stream statistics for
875875
* decode (bool): If set to true, stream will be decoded into dicts on the
876876
fly. False by default.
877+
* stream (bool): If set to false, only the current stats will be returned
878+
instead of a stream. True by default.
877879

878880
```python
879881
>>> from docker import Client

tests/integration/container_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,34 @@ def test_pause_unpause(self):
10401040
self.assertEqual(state['Running'], True)
10411041
self.assertIn('Paused', state)
10421042
self.assertEqual(state['Paused'], False)
1043+
1044+
1045+
class GetContainerStatsTest(api_test.BaseTestCase):
1046+
@requires_api_version('1.19')
1047+
def test_get_container_stats_no_stream(self):
1048+
container = self.client.create_container(
1049+
BUSYBOX, ['sleep', '60'],
1050+
)
1051+
self.tmp_containers.append(container)
1052+
self.client.start(container)
1053+
response = self.client.stats(container, stream=0)
1054+
self.client.kill(container)
1055+
1056+
self.assertEqual(type(response), dict)
1057+
for key in ['read', 'network', 'precpu_stats', 'cpu_stats',
1058+
'memory_stats', 'blkio_stats']:
1059+
self.assertIn(key, response)
1060+
1061+
@requires_api_version('1.17')
1062+
def test_get_container_stats_stream(self):
1063+
container = self.client.create_container(
1064+
BUSYBOX, ['sleep', '60'],
1065+
)
1066+
self.tmp_containers.append(container)
1067+
self.client.start(container)
1068+
stream = self.client.stats(container)
1069+
for chunk in stream:
1070+
self.assertEqual(type(chunk), dict)
1071+
for key in ['read', 'network', 'precpu_stats', 'cpu_stats',
1072+
'memory_stats', 'blkio_stats']:
1073+
self.assertIn(key, chunk)

0 commit comments

Comments
 (0)