Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docker] catch error when parsing json response #1757

Merged
merged 1 commit into from
Jul 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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