Show More
@@ -44,9 +44,9 b' class StatsClientBase(object):' | |||
|
44 | 44 | def pipeline(self): |
|
45 | 45 | raise NotImplementedError() |
|
46 | 46 | |
|
47 | def timer(self, stat, rate=1, tags=None): | |
|
47 | def timer(self, stat, rate=1, tags=None, auto_send=True): | |
|
48 | 48 | """ |
|
49 |
statsd = StatsdClient |
|
|
49 | statsd = StatsdClient.statsd | |
|
50 | 50 | with statsd.timer('bucket_name', auto_send=True) as tmr: |
|
51 | 51 | # This block will be timed. |
|
52 | 52 | for i in xrange(0, 100000): |
@@ -54,7 +54,7 b' class StatsClientBase(object):' | |||
|
54 | 54 | # you can access time here... |
|
55 | 55 | elapsed_ms = tmr.ms |
|
56 | 56 | """ |
|
57 | return Timer(self, stat, rate, tags) | |
|
57 | return Timer(self, stat, rate, tags, auto_send=auto_send) | |
|
58 | 58 | |
|
59 | 59 | def timing(self, stat, delta, rate=1, tags=None, use_decimals=True): |
|
60 | 60 | """ |
@@ -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): | |
|
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 |
@@ -29,6 +29,8 b' class Timer(object):' | |||
|
29 | 29 | self.ms = None |
|
30 | 30 | self._sent = False |
|
31 | 31 | self._start_time = None |
|
32 | self.use_decimals = use_decimals | |
|
33 | self.auto_send = auto_send | |
|
32 | 34 | |
|
33 | 35 | def __call__(self, f): |
|
34 | 36 | """Thread-safe timing function decorator.""" |
@@ -39,14 +41,15 b' class Timer(object):' | |||
|
39 | 41 | return f(*args, **kwargs) |
|
40 | 42 | finally: |
|
41 | 43 | elapsed_time_ms = 1000.0 * (time_now() - start_time) |
|
42 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags) | |
|
44 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals) | |
|
45 | self._sent = True | |
|
43 | 46 | return _wrapped |
|
44 | 47 | |
|
45 | 48 | def __enter__(self): |
|
46 | 49 | return self.start() |
|
47 | 50 | |
|
48 | 51 | def __exit__(self, typ, value, tb): |
|
49 | self.stop() | |
|
52 | self.stop(send=self.auto_send) | |
|
50 | 53 | |
|
51 | 54 | def start(self): |
|
52 | 55 | self.ms = None |
@@ -69,4 +72,4 b' class Timer(object):' | |||
|
69 | 72 | if self._sent: |
|
70 | 73 | raise RuntimeError('Already sent data.') |
|
71 | 74 | self._sent = True |
|
72 | self.client.timing(self.stat, self.ms, self.rate) | |
|
75 | self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals) |
General Comments 0
You need to be logged in to leave comments.
Login now