Created
September 17, 2012 19:49
-
-
Save bartek/3739373 to your computer and use it in GitHub Desktop.
statsd client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import random | |
import logging | |
from socket import socket, AF_INET, SOCK_DGRAM | |
from django.conf import settings | |
logger = logging.getLogger(__name__) | |
class Statsd(object): | |
""" | |
Send statstics to the stats daemon over UDP. | |
Expects a STATSD setting in django.conf.settings | |
STATSD = { | |
'HOST': '127.0.0.1', | |
'PORT': 8125, | |
} | |
Additional parameters in STATSD: | |
`PREFIX`, Prefix each stat key with value. | |
""" | |
@staticmethod | |
def timing(stat, time, sample_rate=1): | |
""" | |
Log timing information | |
>>> from statsd import Statsd | |
>>> Statsd.timing('some.time', 500) | |
""" | |
stats = {} | |
stats[stat] = "%d|ms" % time | |
Statsd.send(stats, sample_rate) | |
@staticmethod | |
def increment(stats, sample_rate=1): | |
""" | |
Increments one or more stats counters | |
>>> Statsd.increment('some.int') | |
>>> Statsd.increment('some.int',0.5) | |
""" | |
Statsd.update_stats(stats, 1, sample_rate) | |
@staticmethod | |
def decrement(stats, sample_rate=1): | |
""" | |
Decrements one or more stats counters | |
>>> Statsd.decrement('some.int') | |
""" | |
Statsd.update_stats(stats, -1, sample_rate) | |
@staticmethod | |
def update_stats(stats, delta=1, sampleRate=1): | |
""" | |
Updates one or more stats counters by arbitrary amounts | |
>>> Statsd.update_stats('some.int',10) | |
""" | |
if (type(stats) is not list): | |
stats = [stats] | |
data = {} | |
for stat in stats: | |
data[stat] = "%s|c" % delta | |
Statsd.send(data, sampleRate) | |
@staticmethod | |
def send(data, sample_rate=1): | |
""" | |
Squirt the metrics over UDP | |
""" | |
if not settings.STATSD or len(set(settings.STATSD.keys()).intersection(['HOST', 'PORT'])) < 2: | |
logger.warning("You must set a HOST and PORT key in the STATSD \ | |
dictionary within the project settings.") | |
return | |
prefix = settings.STATSD.get('PREFIX', '') | |
# Adjust the keys within the data | |
addr = (settings.STATSD['HOST'], settings.STATSD['PORT']) | |
sampled_data = {} | |
if(sample_rate < 1): | |
if random.random() <= sample_rate: | |
for stat in data.keys(): | |
value = data[stat] | |
sampled_data[stat] = "%s|@%s" %(value, sample_rate) | |
else: | |
sampled_data=data | |
udp_sock = socket(AF_INET, SOCK_DGRAM) | |
try: | |
for stat in sampled_data.keys(): | |
value = sampled_data[stat] | |
if prefix: | |
stat = '.'.join([prefix, stat]) | |
send_data = "%s:%s" % (stat, value) | |
logger.debug("Statsd - %s" % send_data) | |
udp_sock.sendto(send_data, addr) | |
except: | |
# If anythin happens, log it. | |
logger.error("Unexpected error sending to statsd", exc_info=sys.exc_info()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment