##// END OF EJS Templates
chore(release): merged default into stable branch
chore(release): merged default into stable branch

File last commit:

r5431:2e534b16 default
r5558:77c2b736 merge v5.3.0 stable
Show More
timer.py
66 lines | 2.0 KiB | text/x-python | PythonLexer
app: added statsd calls for monitoring.
r4632 import functools
py3: synced _vendor with vcsserver py3 changes
r4909 from time import perf_counter as time_now
app: added statsd calls for monitoring.
r4632
def safe_wraps(wrapper, *args, **kwargs):
"""Safely wraps partial functions."""
while isinstance(wrapper, functools.partial):
wrapper = wrapper.func
return functools.wraps(wrapper, *args, **kwargs)
core: refactor of vendor lib to easier sync with vcsserver
r5431 class Timer:
app: added statsd calls for monitoring.
r4632 """A context manager/decorator for statsd.timing()."""
statsd: added option to use timer without auto-sending data
r4814 def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True):
app: added statsd calls for monitoring.
r4632 self.client = client
self.stat = stat
self.rate = rate
metrics: added new statsd client and enabled new metrics on app
r4792 self.tags = tags
app: added statsd calls for monitoring.
r4632 self.ms = None
self._sent = False
self._start_time = None
metrics: use non decimal version for timer....
r4806 self.use_decimals = use_decimals
statsd: added option to use timer without auto-sending data
r4814 self.auto_send = auto_send
app: added statsd calls for monitoring.
r4632
def __call__(self, f):
"""Thread-safe timing function decorator."""
@safe_wraps(f)
def _wrapped(*args, **kwargs):
start_time = time_now()
try:
return f(*args, **kwargs)
finally:
elapsed_time_ms = 1000.0 * (time_now() - start_time)
metrics: use non decimal version for timer....
r4806 self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals)
statsd: added option to use timer without auto-sending data
r4814 self._sent = True
app: added statsd calls for monitoring.
r4632 return _wrapped
def __enter__(self):
return self.start()
def __exit__(self, typ, value, tb):
statsd: added option to use timer without auto-sending data
r4814 self.stop(send=self.auto_send)
app: added statsd calls for monitoring.
r4632
def start(self):
self.ms = None
self._sent = False
self._start_time = time_now()
return self
def stop(self, send=True):
if self._start_time is None:
raise RuntimeError('Timer has not started.')
dt = time_now() - self._start_time
self.ms = 1000.0 * dt # Convert to milliseconds.
if send:
self.send()
return self
def send(self):
if self.ms is None:
raise RuntimeError('No data recorded.')
if self._sent:
raise RuntimeError('Already sent data.')
self._sent = True
metrics: use non decimal version for timer....
r4806 self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals)