From a613f7b17e3ec2fad650e0e7ec3c43762d24c616 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Tue, 21 Aug 2012 16:50:34 -0700 Subject: [PATCH] stats: Make get_timer return a stub object if Graphite disabled. This saves us from having to do explicit checks for whether or not the timer exists everywhere we use them. --- r2/r2/lib/stats.py | 2 +- r2/r2/lib/utils/utils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/r2/r2/lib/stats.py b/r2/r2/lib/stats.py index e658baf92..eb802926c 100644 --- a/r2/r2/lib/stats.py +++ b/r2/r2/lib/stats.py @@ -85,7 +85,7 @@ class Stats: if self.connection: return self.statsd.timer.Timer(name, self.connection) else: - return None + return utils.SimpleSillyStub() def transact(self, action, service_time_sec): timer = self.get_timer('service_time') diff --git a/r2/r2/lib/utils/utils.py b/r2/r2/lib/utils/utils.py index f47f5b57f..fb61fead4 100644 --- a/r2/r2/lib/utils/utils.py +++ b/r2/r2/lib/utils/utils.py @@ -1310,6 +1310,17 @@ class Bomb(object): def __repr__(cls): raise Hell() +class SimpleSillyStub(object): + """A simple stub object that does nothing when you call its methods.""" + def __nonzero__(self): + return False + + def __getattr__(self, name): + return self.stub + + def stub(self, *args, **kwargs): + pass + def strordict_fullname(item, key='fullname'): """Sometimes we migrate AMQP queues from simple strings to pickled dictionaries. During the migratory period there may be items in