Skip to content

Commit

Permalink
Merge pull request #1757 from DataDog/talwai/docker_check_fix
Browse files Browse the repository at this point in the history
[docker] catch error when parsing json response
  • Loading branch information
talwai committed Jul 9, 2015
2 parents 3126468 + 8ab018f commit b53b370
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions checks.d/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

DEFAULT_SOCKET_TIMEOUT = 5

class DockerJSONDecodeError(Exception):
""" Raised when there is trouble parsing the API response sent by Docker Remote API """
pass

class UnixHTTPConnection(httplib.HTTPConnection):
"""Class used in conjuction with UnixSocketHandler to make urllib2
Expand Down Expand Up @@ -358,16 +361,21 @@ def _get_images(self, instance, with_size=True, get_all=False):
def _get_events(self, instance):
"""Get the list of events """
now = int(time.time())
result = self._get_json(
"%s/events" % instance["url"],
params={
"until": now,
"since": self._last_event_collection_ts[instance["url"]] or now - 60,
}, multi=True)
self._last_event_collection_ts[instance["url"]] = now
if type(result) == dict:
result = [result]
return result
try:
result = self._get_json(
"%s/events" % instance["url"],
params={
"until": now,
"since": self._last_event_collection_ts[instance["url"]] or now - 60,
},
multi=True
)
self._last_event_collection_ts[instance["url"]] = now
if type(result) == dict:
result = [result]
return result
except DockerJSONDecodeError:
return []

def _get_json(self, uri, params=None, multi=False):
"""Utility method to get and parse JSON streams."""
Expand Down Expand Up @@ -396,8 +404,7 @@ def _get_json(self, uri, params=None, multi=False):
return json.loads(response)
except Exception as e:
self.log.error('Failed to parse Docker API response: %s', response)
raise

raise DockerJSONDecodeError

# Cgroups

Expand Down

0 comments on commit b53b370

Please sign in to comment.