2011-02-16 08:17:48 +03:00
|
|
|
# Steve Ivy <steveivy@gmail.com>
|
|
|
|
# http://monkinetic.com
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2012-12-10 01:03:12 +04:00
|
|
|
from random import random
|
|
|
|
from socket import socket, AF_INET, SOCK_DGRAM
|
|
|
|
|
2012-11-12 20:42:47 +04:00
|
|
|
class StatsdClient(object):
|
2012-12-23 20:09:58 +04:00
|
|
|
SC_TIMING = "ms"
|
|
|
|
SC_COUNT = "c"
|
|
|
|
SC_GAUGE = "g"
|
|
|
|
SC_SET = "s"
|
|
|
|
|
2012-11-12 20:23:56 +04:00
|
|
|
def __init__(self, host='localhost', port=8125):
|
2012-12-23 19:15:43 +04:00
|
|
|
"""
|
|
|
|
Sends statistics to the stats daemon over UDP
|
|
|
|
|
|
|
|
>>> from python_example import StatsdClient
|
|
|
|
"""
|
2012-12-10 01:00:36 +04:00
|
|
|
self.addr = (host, port)
|
2012-11-12 20:20:43 +04:00
|
|
|
|
2012-12-23 21:36:24 +04:00
|
|
|
def timing(self, stats, value):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Log timing information
|
2012-12-23 19:15:43 +04:00
|
|
|
|
2012-12-10 00:57:57 +04:00
|
|
|
>>> client = StatsdClient()
|
2012-12-23 19:15:43 +04:00
|
|
|
>>> client.timing('example.timing', 500)
|
2012-12-23 20:09:58 +04:00
|
|
|
>>> client.timing(('example.timing23', 'example.timing29'), 500)
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2012-12-23 21:41:45 +04:00
|
|
|
self.update_stats(stats, value, self.SC_TIMING)
|
2011-02-16 08:05:42 +03:00
|
|
|
|
2012-12-23 21:16:25 +04:00
|
|
|
def gauge(self, stats, value):
|
|
|
|
"""
|
|
|
|
Log gauges
|
|
|
|
|
|
|
|
>>> client = StatsdClient()
|
|
|
|
>>> client.gauge('example.gauge', 47)
|
|
|
|
>>> client.gauge(('example.gauge41', 'example.gauge43'), 47)
|
|
|
|
"""
|
2012-12-23 21:41:45 +04:00
|
|
|
self.update_stats(stats, value, self.SC_GAUGE)
|
2012-12-23 21:16:25 +04:00
|
|
|
|
2012-12-23 21:33:58 +04:00
|
|
|
def set(self, stats, value):
|
|
|
|
"""
|
|
|
|
Log set
|
|
|
|
|
|
|
|
>>> client = StatsdClient()
|
|
|
|
>>> client.set('example.set', "set")
|
|
|
|
>>> client.set(('example.set61', 'example.set67'), "2701")
|
|
|
|
"""
|
2012-12-23 21:41:45 +04:00
|
|
|
self.update_stats(stats, value, self.SC_SET)
|
2012-12-23 21:33:58 +04:00
|
|
|
|
2012-12-10 00:30:51 +04:00
|
|
|
def increment(self, stats, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Increments one or more stats counters
|
2012-12-23 19:15:43 +04:00
|
|
|
|
2012-12-10 00:57:57 +04:00
|
|
|
>>> client = StatsdClient()
|
2012-12-23 19:15:43 +04:00
|
|
|
>>> client.increment('example.increment')
|
|
|
|
>>> client.increment('example.increment', 0.5)
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2012-12-23 19:22:22 +04:00
|
|
|
self.count(stats, 1, sample_rate)
|
2011-02-16 08:05:42 +03:00
|
|
|
|
2012-12-10 00:30:51 +04:00
|
|
|
def decrement(self, stats, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Decrements one or more stats counters
|
2012-12-23 19:15:43 +04:00
|
|
|
|
2012-12-10 00:57:57 +04:00
|
|
|
>>> client = StatsdClient()
|
2012-12-23 19:15:43 +04:00
|
|
|
>>> client.decrement('example.decrement')
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2012-12-23 19:22:22 +04:00
|
|
|
self.count(stats, -1, sample_rate)
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2012-12-23 21:36:24 +04:00
|
|
|
def count(self, stats, value, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2012-12-23 21:41:45 +04:00
|
|
|
Updates one or more stats counters by arbitrary value
|
2012-12-23 19:15:43 +04:00
|
|
|
|
2012-12-10 00:57:57 +04:00
|
|
|
>>> client = StatsdClient()
|
2012-12-23 19:22:22 +04:00
|
|
|
>>> client.count('example.counter', 17)
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2012-12-23 21:41:45 +04:00
|
|
|
self.update_stats(stats, value, self.SC_COUNT, sample_rate)
|
|
|
|
|
|
|
|
def update_stats(self, stats, value, _type, sample_rate=1):
|
|
|
|
"""
|
|
|
|
Pipeline function that formats data, samples it and passes to send()
|
|
|
|
|
|
|
|
>>> client = StatsdClient()
|
|
|
|
>>> client.update_stats('example.update_stats', 73, "c", 0.9)
|
|
|
|
"""
|
|
|
|
stats = self.format(stats, value, _type)
|
2012-12-23 20:09:58 +04:00
|
|
|
self.send(self.sample(stats, sample_rate), self.addr)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format(keys, value, _type):
|
|
|
|
"""
|
|
|
|
General format function.
|
|
|
|
|
|
|
|
>>> StatsdClient.format("example.format", 2, "T")
|
|
|
|
{'example.format': '2|T'}
|
2013-12-31 12:44:05 +04:00
|
|
|
>>> formatted = StatsdClient.format(("example.format31", "example.format37"), "2", "T")
|
|
|
|
>>> formatted['example.format31'] == '2|T'
|
|
|
|
True
|
|
|
|
>>> formatted['example.format37'] == '2|T'
|
|
|
|
True
|
|
|
|
>>> len(formatted)
|
|
|
|
2
|
2012-12-23 20:09:58 +04:00
|
|
|
"""
|
2011-02-16 08:05:42 +03:00
|
|
|
data = {}
|
2012-12-23 20:09:58 +04:00
|
|
|
value = "{0}|{1}".format(value, _type)
|
|
|
|
# TODO: Allow any iterable except strings
|
|
|
|
if not isinstance(keys, (list, tuple)):
|
|
|
|
keys = [keys]
|
|
|
|
for key in keys:
|
|
|
|
data[key] = value
|
|
|
|
return data
|
2012-12-23 19:01:21 +04:00
|
|
|
|
|
|
|
@staticmethod
|
2012-12-23 19:32:13 +04:00
|
|
|
def sample(data, sample_rate):
|
2012-12-23 19:01:21 +04:00
|
|
|
"""
|
|
|
|
Sample data dict
|
|
|
|
TODO(rbtz@): Convert to generator
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2012-12-23 19:01:21 +04:00
|
|
|
>>> StatsdClient.sample({"example.sample2": "2"}, 1)
|
|
|
|
{'example.sample2': '2'}
|
|
|
|
>>> StatsdClient.sample({"example.sample3": "3"}, 0)
|
|
|
|
{}
|
|
|
|
>>> from random import seed
|
|
|
|
>>> seed(1)
|
2013-12-31 12:44:05 +04:00
|
|
|
>>> sampled = StatsdClient.sample({"example.sample5": "5", "example.sample7": "7"}, 0.99)
|
|
|
|
>>> len(sampled)
|
|
|
|
2
|
|
|
|
>>> sampled['example.sample5']
|
|
|
|
'5|@0.99'
|
|
|
|
>>> sampled['example.sample7']
|
|
|
|
'7|@0.99'
|
2012-12-23 19:01:21 +04:00
|
|
|
>>> StatsdClient.sample({"example.sample5": "5", "example.sample7": "7"}, 0.01)
|
|
|
|
{}
|
|
|
|
"""
|
2012-12-23 21:45:42 +04:00
|
|
|
if sample_rate >= 1:
|
|
|
|
return data
|
|
|
|
elif sample_rate < 1:
|
2012-12-10 01:03:12 +04:00
|
|
|
if random() <= sample_rate:
|
2012-12-23 21:45:42 +04:00
|
|
|
sampled_data = {}
|
2012-12-10 00:49:34 +04:00
|
|
|
for stat, value in data.items():
|
2012-12-23 15:13:19 +04:00
|
|
|
sampled_data[stat] = "{0}|@{1}".format(value, sample_rate)
|
2012-12-23 21:45:42 +04:00
|
|
|
return sampled_data
|
|
|
|
return {}
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2012-12-23 19:01:21 +04:00
|
|
|
@staticmethod
|
|
|
|
def send(_dict, addr):
|
|
|
|
"""
|
|
|
|
Sends key/value pairs via UDP.
|
|
|
|
|
|
|
|
>>> StatsdClient.send({"example.send":"11|c"}, ("127.0.0.1", 8125))
|
|
|
|
"""
|
|
|
|
# TODO(rbtz@): IPv6 support
|
2013-12-31 14:36:10 +04:00
|
|
|
# TODO(rbtz@): Creating socket on each send is a waste of recources
|
2011-02-16 08:05:42 +03:00
|
|
|
udp_sock = socket(AF_INET, SOCK_DGRAM)
|
2012-12-23 19:40:02 +04:00
|
|
|
# TODO(rbtz@): Add batch support
|
|
|
|
for item in _dict.items():
|
|
|
|
udp_sock.sendto(":".join(item).encode('utf-8'), addr)
|