# HG changeset patch # User RhodeCode Admin # Date 2023-01-05 08:33:33 # Node ID 6541a7d5f3cb0d4998d5a67a85e8a0d1f79add8e # Parent 4f9554192a30f9a372673768c1df41e861344da0 statsd: synced client with rhodecode diff --git a/vcsserver/lib/_vendor/statsd/base.py b/vcsserver/lib/_vendor/statsd/base.py --- a/vcsserver/lib/_vendor/statsd/base.py +++ b/vcsserver/lib/_vendor/statsd/base.py @@ -44,9 +44,9 @@ class StatsClientBase(object): def pipeline(self): raise NotImplementedError() - def timer(self, stat, rate=1, tags=None): + def timer(self, stat, rate=1, tags=None, auto_send=True): """ - statsd = StatsdClient() + statsd = StatsdClient.statsd with statsd.timer('bucket_name', auto_send=True) as tmr: # This block will be timed. for i in xrange(0, 100000): @@ -54,7 +54,7 @@ class StatsClientBase(object): # you can access time here... elapsed_ms = tmr.ms """ - return Timer(self, stat, rate, tags) + return Timer(self, stat, rate, tags, auto_send=auto_send) def timing(self, stat, delta, rate=1, tags=None, use_decimals=True): """ diff --git a/vcsserver/lib/_vendor/statsd/timer.py b/vcsserver/lib/_vendor/statsd/timer.py --- a/vcsserver/lib/_vendor/statsd/timer.py +++ b/vcsserver/lib/_vendor/statsd/timer.py @@ -21,7 +21,7 @@ def safe_wraps(wrapper, *args, **kwargs) class Timer(object): """A context manager/decorator for statsd.timing().""" - def __init__(self, client, stat, rate=1, tags=None): + def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True): self.client = client self.stat = stat self.rate = rate @@ -29,6 +29,8 @@ class Timer(object): self.ms = None self._sent = False self._start_time = None + self.use_decimals = use_decimals + self.auto_send = auto_send def __call__(self, f): """Thread-safe timing function decorator.""" @@ -39,14 +41,15 @@ class Timer(object): return f(*args, **kwargs) finally: elapsed_time_ms = 1000.0 * (time_now() - start_time) - self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags) + self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals) + self._sent = True return _wrapped def __enter__(self): return self.start() def __exit__(self, typ, value, tb): - self.stop() + self.stop(send=self.auto_send) def start(self): self.ms = None @@ -69,4 +72,4 @@ class Timer(object): if self._sent: raise RuntimeError('Already sent data.') self._sent = True - self.client.timing(self.stat, self.ms, self.rate) + self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals)