##// END OF EJS Templates
statsd: added option to use timer without auto-sending data
super-admin -
r4814:bdd096de default
parent child Browse files
Show More
@@ -45,6 +45,15 b' class StatsClientBase(object):'
45 45 raise NotImplementedError()
46 46
47 47 def timer(self, stat, rate=1, tags=None):
48 """
49 statsd = StatsdClient()
50 with statsd.timer('bucket_name', auto_send=True) as tmr:
51 # This block will be timed.
52 for i in xrange(0, 100000):
53 i ** 2
54 # you can access time here...
55 elapsed_ms = tmr.ms
56 """
48 57 return Timer(self, stat, rate, tags)
49 58
50 59 def timing(self, stat, delta, rate=1, tags=None, use_decimals=True):
@@ -21,7 +21,7 b' def safe_wraps(wrapper, *args, **kwargs)'
21 21 class Timer(object):
22 22 """A context manager/decorator for statsd.timing()."""
23 23
24 def __init__(self, client, stat, rate=1, tags=None, use_decimals=True):
24 def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True):
25 25 self.client = client
26 26 self.stat = stat
27 27 self.rate = rate
@@ -30,6 +30,7 b' class Timer(object):'
30 30 self._sent = False
31 31 self._start_time = None
32 32 self.use_decimals = use_decimals
33 self.auto_send = auto_send
33 34
34 35 def __call__(self, f):
35 36 """Thread-safe timing function decorator."""
@@ -41,13 +42,14 b' class Timer(object):'
41 42 finally:
42 43 elapsed_time_ms = 1000.0 * (time_now() - start_time)
43 44 self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals)
45 self._sent = True
44 46 return _wrapped
45 47
46 48 def __enter__(self):
47 49 return self.start()
48 50
49 51 def __exit__(self, typ, value, tb):
50 self.stop()
52 self.stop(send=self.auto_send)
51 53
52 54 def start(self):
53 55 self.ms = None
General Comments 0
You need to be logged in to leave comments. Login now