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