From 0b9ddc6dcd89c3a1e19061d3cf25681c328f20cf Mon Sep 17 00:00:00 2001 From: Michael Kralka Date: Fri, 24 Oct 2014 16:58:31 -0700 Subject: [PATCH] Fix connection leak in redis-stats collector --- collectors/0/redis-stats.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/collectors/0/redis-stats.py b/collectors/0/redis-stats.py index 9f86ce5e..10bb2bef 100755 --- a/collectors/0/redis-stats.py +++ b/collectors/0/redis-stats.py @@ -129,21 +129,24 @@ def print_stat(metric, value, tags=""): # connect to the instance and attempt to gather info r = redis.Redis(host="127.0.0.1", port=port) - info = r.info() - for key in KEYS: - if key in info: - print_stat(key, info[key], tags) - - # per database metrics - for db in filter(dbre.match, info.keys()): - for db_metric in info[db].keys(): - print_stat(db_metric, info[db][db_metric], "%s db=%s" % (tags, db)) - - # get some instant latency information - # TODO: might be nice to get 95th, 99th, etc here? - start_time = time.time() - r.ping() - print_stat("latency", time.time() - start_time, tags) + try: + info = r.info() + for key in KEYS: + if key in info: + print_stat(key, info[key], tags) + + # per database metrics + for db in filter(dbre.match, info.keys()): + for db_metric in info[db].keys(): + print_stat(db_metric, info[db][db_metric], "%s db=%s" % (tags, db)) + + # get some instant latency information + # TODO: might be nice to get 95th, 99th, etc here? + start_time = time.time() + r.ping() + print_stat("latency", time.time() - start_time, tags) + finally: + r.connection_pool.disconnect() sys.stdout.flush() time.sleep(interval)