metrics: use non decimal version for timer.

- added bucket registry
- some small fixes
This commit is contained in:
RhodeCode Admin 2022-10-25 16:39:23 +02:00
parent e9cba7b782
commit 059911817e
4 changed files with 28 additions and 9 deletions

View file

@ -135,10 +135,10 @@ def make_pyramid_app(global_config, **settings):
log.info('Pyramid app `%s` created and configured in %.2fs',
pyramid_app.func_name, total_time)
if statsd:
elapsed_time_ms = 1000.0 * total_time
elapsed_time_ms = round(1000.0 * total_time) # use ms only rounded time
statsd.timing('rhodecode_app_bootstrap_timing', elapsed_time_ms, tags=[
"pyramid_app:{}".format(pyramid_app.func_name)
])
], use_decimals=False)
return pyramid_app

View file

@ -14,6 +14,12 @@ TAG_INVALID_CHARS_RE = re.compile(
)
TAG_INVALID_CHARS_SUBS = "_"
# we save and expose methods called by statsd for discovery
stat_dict = {
}
@lru_cache(maxsize=500)
def _normalize_tags_with_cache(tag_list):
@ -42,7 +48,7 @@ class StatsClientBase(object):
def timer(self, stat, rate=1, tags=None):
return Timer(self, stat, rate, tags)
def timing(self, stat, delta, rate=1, tags=None):
def timing(self, stat, delta, rate=1, tags=None, use_decimals=True):
"""
Send new timing information.
@ -51,7 +57,11 @@ class StatsClientBase(object):
if isinstance(delta, timedelta):
# Convert timedelta to number of milliseconds.
delta = delta.total_seconds() * 1000.
self._send_stat(stat, '%0.6f|ms' % delta, rate, tags)
if use_decimals:
fmt = '%0.6f|ms'
else:
fmt = '%s|ms'
self._send_stat(stat, fmt % delta, rate, tags)
def incr(self, stat, count=1, rate=1, tags=None):
"""Increment a stat by `count`."""
@ -78,10 +88,17 @@ class StatsClientBase(object):
"""Set a set value."""
self._send_stat(stat, '%s|s' % value, rate)
def histogram(self, stat, value, rate=1, tags=None):
"""Set a histogram"""
self._send_stat(stat, '%s|h' % value, rate, tags)
def _send_stat(self, stat, value, rate, tags=None):
self._after(self._prepare(stat, value, rate, tags))
def _prepare(self, stat, value, rate, tags=None):
global stat_dict
stat_dict[stat] = 1
if rate < 1:
if random.random() > rate:
return

View file

@ -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):
self.client = client
self.stat = stat
self.rate = rate
@ -29,6 +29,7 @@ class Timer(object):
self.ms = None
self._sent = False
self._start_time = None
self.use_decimals = use_decimals
def __call__(self, f):
"""Thread-safe timing function decorator."""
@ -39,7 +40,7 @@ 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)
return _wrapped
def __enter__(self):
@ -69,4 +70,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)

View file

@ -65,14 +65,15 @@ class RequestWrapperTween(object):
if statsd:
match_route = request.matched_route.name if request.matched_route else _path
resp_code = response.status_code
elapsed_time_ms = 1000.0 * total
elapsed_time_ms = round(1000.0 * total) # use ms only
statsd.timing(
'rhodecode_req_timing', elapsed_time_ms,
tags=[
"view_name:{}".format(match_route),
#"user:{}".format(user_id),
"code:{}".format(resp_code)
]
],
use_decimals=False
)
statsd.incr(
'rhodecode_req_total', tags=[