From 8ab018fc17a32f697bad8145d1a05526889dd6b3 Mon Sep 17 00:00:00 2001 From: talwai Date: Thu, 9 Jul 2015 10:40:58 -0400 Subject: [PATCH] [docker] catch error when parsing json response --- checks.d/docker.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/checks.d/docker.py b/checks.d/docker.py index 8b24b9aab5..63bf20fdbb 100644 --- a/checks.d/docker.py +++ b/checks.d/docker.py @@ -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 @@ -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.""" @@ -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