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.
This commit is contained in:
Neil Williams
2012-08-21 16:50:34 -07:00
parent 5e9e943f8b
commit a613f7b17e
2 changed files with 12 additions and 1 deletions

View File

@@ -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')

View File

@@ -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