##// 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 raise NotImplementedError()
45 raise NotImplementedError()
46
46
47 def timer(self, stat, rate=1, tags=None):
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 return Timer(self, stat, rate, tags)
57 return Timer(self, stat, rate, tags)
49
58
50 def timing(self, stat, delta, rate=1, tags=None, use_decimals=True):
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 class Timer(object):
21 class Timer(object):
22 """A context manager/decorator for statsd.timing()."""
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 self.client = client
25 self.client = client
26 self.stat = stat
26 self.stat = stat
27 self.rate = rate
27 self.rate = rate
@@ -30,6 +30,7 b' class Timer(object):'
30 self._sent = False
30 self._sent = False
31 self._start_time = None
31 self._start_time = None
32 self.use_decimals = use_decimals
32 self.use_decimals = use_decimals
33 self.auto_send = auto_send
33
34
34 def __call__(self, f):
35 def __call__(self, f):
35 """Thread-safe timing function decorator."""
36 """Thread-safe timing function decorator."""
@@ -41,13 +42,14 b' class Timer(object):'
41 finally:
42 finally:
42 elapsed_time_ms = 1000.0 * (time_now() - start_time)
43 elapsed_time_ms = 1000.0 * (time_now() - start_time)
43 self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals)
44 self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals)
45 self._sent = True
44 return _wrapped
46 return _wrapped
45
47
46 def __enter__(self):
48 def __enter__(self):
47 return self.start()
49 return self.start()
48
50
49 def __exit__(self, typ, value, tb):
51 def __exit__(self, typ, value, tb):
50 self.stop()
52 self.stop(send=self.auto_send)
51
53
52 def start(self):
54 def start(self):
53 self.ms = None
55 self.ms = None
General Comments 0
You need to be logged in to leave comments. Login now