Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
172 lines (138 sloc) 4.938 kb
from __future__ import with_statement
from collections import deque
from functools import wraps
import random
import socket
import time
__all__ = ['StatsClient']
class Timer(object):
"""A context manager/decorator for statsd.timing()."""
def __init__(self, client, stat, rate=1):
self.client = client
self.stat = stat
self.rate = rate
self.ms = None
self._sent = False
self._start_time = None
def __call__(self, f):
"""Thread-safe timing function decorator."""
@wraps(f)
def _wrapped(*args, **kwargs):
start_time = time.time()
try:
return_value = f(*args, **kwargs)
finally:
elapsed_time_ms = int(round(1000 * (time.time() - start_time)))
self.client.timing(self.stat, elapsed_time_ms, self.rate)
return return_value
return _wrapped
def __enter__(self):
return self.start()
def __exit__(self, typ, value, tb):
self.stop()
def start(self):
self.ms = None
self._sent = False
self._start_time = time.time()
return self
def stop(self, send=True):
if self._start_time is None:
raise RuntimeError('Timer has not started.')
dt = time.time() - self._start_time
self.ms = int(round(1000 * 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
self.client.timing(self.stat, self.ms, self.rate)
class StatsClient(object):
"""A client for statsd."""
def __init__(self, host='localhost', port=8125, prefix=None,
maxudpsize=512):
"""Create a new client."""
family, _, _, _, addr = socket.getaddrinfo(
host, port, 0, socket.SOCK_DGRAM
)[0]
self._addr = addr
self._sock = socket.socket(family, socket.SOCK_DGRAM)
self._prefix = prefix
self._maxudpsize = maxudpsize
def pipeline(self):
return Pipeline(self)
def timer(self, stat, rate=1):
return Timer(self, stat, rate)
def timing(self, stat, delta, rate=1):
"""Send new timing information. `delta` is in milliseconds."""
self._send_stat(stat, '%d|ms' % delta, rate)
def incr(self, stat, count=1, rate=1):
"""Increment a stat by `count`."""
self._send_stat(stat, '%s|c' % count, rate)
def decr(self, stat, count=1, rate=1):
"""Decrement a stat by `count`."""
self.incr(stat, -count, rate)
def gauge(self, stat, value, rate=1, delta=False):
"""Set a gauge value."""
if value < 0 and not delta:
if rate < 1:
if random.random() > rate:
return
with self.pipeline() as pipe:
pipe._send_stat(stat, '0|g', 1)
pipe._send_stat(stat, '%s|g' % value, 1)
else:
prefix = '+' if delta and value >= 0 else ''
self._send_stat(stat, '%s%s|g' % (prefix, value), rate)
def set(self, stat, value, rate=1):
"""Set a set value."""
self._send_stat(stat, '%s|s' % value, rate)
def _send_stat(self, stat, value, rate):
self._after(self._prepare(stat, value, rate))
def _prepare(self, stat, value, rate):
if rate < 1:
if random.random() > rate:
return
value = '%s|@%s' % (value, rate)
if self._prefix:
stat = '%s.%s' % (self._prefix, stat)
return '%s:%s' % (stat, value)
def _after(self, data):
if data:
self._send(data)
def _send(self, data):
"""Send data to statsd."""
try:
self._sock.sendto(data.encode('ascii'), self._addr)
except socket.error:
# No time for love, Dr. Jones!
pass
class Pipeline(StatsClient):
def __init__(self, client):
self._client = client
self._prefix = client._prefix
self._maxudpsize = client._maxudpsize
self._stats = deque()
def _after(self, data):
if data is not None:
self._stats.append(data)
def __enter__(self):
return self
def __exit__(self, typ, value, tb):
self.send()
def send(self):
# Use popleft to preserve the order of the stats.
if not self._stats:
return
data = self._stats.popleft()
while self._stats:
stat = self._stats.popleft()
if len(stat) + len(data) + 1 >= self._maxudpsize:
self._client._after(data)
data = stat
else:
data += '\n' + stat
self._client._after(data)
Jump to Line
Something went wrong with that request. Please try again.