Skip to content

Commit

Permalink
docker-py 1.6 to use non stream stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Praloran committed Nov 27, 2015
1 parent 4f9dd77 commit bf398b5
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions dockerplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import re


STREAM_DOCKER_PY_VERSION=(1, 6, 0)

def _c(c):
"""A helper method for representing a container in messages. If the given
argument is a string, it is assumed to be the container's ID and only the
Expand Down Expand Up @@ -180,7 +182,7 @@ class ContainerStats(threading.Thread):
second), and make the most recently read data available in a variable.
"""

def __init__(self, container, client):
def __init__(self, container, client, stream):
threading.Thread.__init__(self)
self.daemon = True
self.stop = False
Expand All @@ -189,6 +191,7 @@ def __init__(self, container, client):
self._client = client
self._feed = None
self._stats = None
self._stream = stream

# Automatically start stats reading thread
self.start()
Expand All @@ -200,10 +203,15 @@ def run(self):
failures = 0
while not self.stop:
try:
if not self._feed:
self._feed = self._client.stats(self._container,
decode=True)
self._stats = self._feed.next()

if not self._stream:
if not self._feed:
self._feed = self._client.stats(self._container,
decode=True)
self._stats = self._feed.next()
else:
self._stats = self._client.stats(self._container,
decode=True,stream=False)
# Reset failure count on successfull read from the stats API.
failures = 0
except Exception, e:
Expand Down Expand Up @@ -255,6 +263,12 @@ def __init__(self, docker_url=None):
self.timeout = DockerPlugin.DEFAULT_DOCKER_TIMEOUT
self.capture = False
self.stats = {}
self.stream = False
s_version = re.match('([\d.]+)', docker.__version__)
version = tuple([int(x) for x in s_version.group(1).split('.')])
if version >= STREAM_DOCKER_PY_VERSION:
self.stream = True
collectd.info('Docker stats use stream')

def configure_callback(self, conf):
for node in conf.children:
Expand Down Expand Up @@ -310,7 +324,8 @@ def read_callback(self):
# Start a stats gathering thread if the container is new.
if container['Id'] not in self.stats:
self.stats[container['Id']] = ContainerStats(container,
self.client)
self.client,
self.stream)

# Get and process stats from the container.
stats = self.stats[container['Id']].stats
Expand Down

0 comments on commit bf398b5

Please sign in to comment.