From 32ef38b96ea89cb98e8126047503b28716d97e5f Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Wed, 21 Aug 2013 00:16:06 +0530 Subject: [PATCH 1/2] Removing self.conn in OpenTSDB Finder. It is recommended that for every request a new connection is used. For more info, refer this [http://stackoverflow.com/questions/5346434/python-cannotsendrequest] and [http://stackoverflow.com/questions/1925639/httplib-cannotsendrequest-error-in-wsgi]. --- webapp/graphite/finders.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/webapp/graphite/finders.py b/webapp/graphite/finders.py index c573c5deb..bb0fa0b93 100644 --- a/webapp/graphite/finders.py +++ b/webapp/graphite/finders.py @@ -41,7 +41,6 @@ class OpenTSDBFinder: def __init__(self, host, port): self.host = host self.port = port - self.conn = httplib.HTTPConnection(self.host, self.port) def find_nodes(self, query): # if (query.drilldown == True): @@ -262,14 +261,21 @@ def drill_recursive(self, patterns, index, current_branch): log.debug("Skipping node [" + patterns[index] + "] on node [" + node.name + "] in branch [" + str(current_branch) + "]") def fetch_branch(self, query): - self.conn.request("GET", "/api/tree/branch?branch=" + query.pattern) - resp = self.conn.getresponse() + conn = httplib.HTTPConnection(self.host, self.port) + conn.request("GET", "/api/tree/branch?branch=" + query.pattern) + resp = conn.getresponse() + #Make sure you grab the data immediately. resp.read() second time gives an empty string + data = resp.read() log.debug("API request has returned") if resp.status != 200: log.warn("Could not find a branch for [" + query.pattern + "]. Status: " + str(resp.status)) else: - log.debug("Positive response from OpenTSDB for the branch at [" + query.pattern + "]") - json_data = json.loads(resp.read()) + #Warn if data is empty. json.loads will throw the error for you + if not data: + log.warn("The JSON returned for the query [" + query.pattern +"] is empty"); + else: + log.debug("Positive response from OpenTSDB for the branch at [" + query.pattern + "]") + json_data = json.loads(data) if (json_data['branches'] and (json_data['branches']) > 0): for i in xrange(len(json_data['branches'])): From 7935ff10ddb288a58017c062faff037d9760533a Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Wed, 21 Aug 2013 11:59:49 +0530 Subject: [PATCH 2/2] Index Error while rendering. This happens when the requested for lower time-span values (say 5 mins, 20 mins etc...). Now using the correct index value. --- webapp/graphite/readers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/graphite/readers.py b/webapp/graphite/readers.py index c3e958db5..d69625daf 100644 --- a/webapp/graphite/readers.py +++ b/webapp/graphite/readers.py @@ -159,7 +159,7 @@ def fetch(self, startTime, endTime): log.debug("Key [" + str(key) + "] was greater than end time [" + str(int(endTime)) + "]") break - valueList[counter] = json_data[0]['dps'][key] + valueList[counter-1] = json_data[0]['dps'][key] counter += 1 log.debug("Retrieved [" + str(counter) + "] datapoints")