mirror of https://github.com/zulip/zulip.git
Add statsd helpers and wrappers
(imported from commit 9d5b805ae416a65ac49dda8e8e11d9831308116c)
This commit is contained in:
parent
1d25f36554
commit
46415d4984
|
@ -1,6 +1,50 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import hashlib
|
import hashlib
|
||||||
from time import sleep
|
from time import sleep, time
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
def statsd_key(val):
|
||||||
|
if not isinstance(val, str):
|
||||||
|
val = str(val)
|
||||||
|
|
||||||
|
if ':' in val:
|
||||||
|
val = val.split(':')[0]
|
||||||
|
val = val.replace('-', "_")
|
||||||
|
|
||||||
|
return val
|
||||||
|
|
||||||
|
class StatsDWrapper(object):
|
||||||
|
"""Transparently either submit metrics to statsd
|
||||||
|
or do nothing without erroring out"""
|
||||||
|
|
||||||
|
# Backported support for gauge deltas
|
||||||
|
# as our statsd server supports them but supporting
|
||||||
|
# pystatsd is not released yet
|
||||||
|
def _our_gauge(self, stat, value, rate=1, delta=False):
|
||||||
|
"""Set a gauge value."""
|
||||||
|
from django_statsd.clients import statsd
|
||||||
|
if delta:
|
||||||
|
value = '%+g|g' % value
|
||||||
|
else:
|
||||||
|
value = '%g|g' % value
|
||||||
|
statsd._send(stat, value, rate)
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
# Hand off to statsd if we have it enabled
|
||||||
|
# otherwise do nothing
|
||||||
|
if name in ['timer', 'timing', 'incr', 'decr', 'gauge']:
|
||||||
|
if settings.USING_STATSD:
|
||||||
|
from django_statsd.clients import statsd
|
||||||
|
if name == 'gauge':
|
||||||
|
return self._our_gauge
|
||||||
|
else:
|
||||||
|
return getattr(statsd, name)
|
||||||
|
else:
|
||||||
|
return lambda *args, **kwargs: None
|
||||||
|
|
||||||
|
raise AttributeError
|
||||||
|
|
||||||
|
statsd = StatsDWrapper()
|
||||||
|
|
||||||
# Runs the callback with slices of all_list of a given batch_size
|
# Runs the callback with slices of all_list of a given batch_size
|
||||||
def run_in_batches(all_list, batch_size, callback, sleep_time = 0, logger = None):
|
def run_in_batches(all_list, batch_size, callback, sleep_time = 0, logger = None):
|
||||||
|
@ -30,3 +74,18 @@ def make_safe_digest(string, hash_func=hashlib.sha1):
|
||||||
# hashlib.sha1, md5, etc. expect bytes, so non-ASCII strings must
|
# hashlib.sha1, md5, etc. expect bytes, so non-ASCII strings must
|
||||||
# be encoded.
|
# be encoded.
|
||||||
return hash_func(string.encode('utf-8')).hexdigest()
|
return hash_func(string.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def log_statsd_event(name):
|
||||||
|
"""
|
||||||
|
Sends a single event to statsd with the desired name and the current timestamp
|
||||||
|
|
||||||
|
This can be used to provide vertical lines in generated graphs,
|
||||||
|
for example when doing a prod deploy, bankruptcy request, or
|
||||||
|
other one-off events
|
||||||
|
|
||||||
|
Note that to draw this event as a vertical line in graphite
|
||||||
|
you can use the drawAsInfinite() command
|
||||||
|
"""
|
||||||
|
event_name = "events.%s" % (name,)
|
||||||
|
statsd.gauge(event_name, int(time()))
|
||||||
|
|
Loading…
Reference in New Issue