Skip to content


Subversion checkout URL

You can clone with
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

89 lines (77 sloc) 2.584 kb
# Steve Ivy <>
# this file expects to be in the same dir, with statsd host and port information:
# statsd_host = 'localhost'
# statsd_port = 8125
# Sends statistics to the stats daemon over UDP
class Statsd(object):
def timing(stats, time, sample_rate=1):
Log timing information
>>> from python_example import Statsd
>>> Statsd.timing('some.time','500|ms')
Statsd.update_stats(stats, time, sample_rate)
def increment(stats, sample_rate=1):
Increments one or more stats counters
>>> Statsd.increment('')
>>> Statsd.increment('',0.5)
Statsd.update_stats(stats, 1, sample_rate)
def decrement(stats, sample_rate=1):
Decrements one or more stats counters
>>> Statsd.decrement('')
Statsd.update_stats(stats, -1, sample_rate)
def update_stats(stats, delta=1, sampleRate=1):
Updates one or more stats counters by arbitrary amounts
>>> Statsd.update_stats('',10)
if (type(stats) is not list):
stats = [stats]
data = {}
for stat in stats:
data[stat] = "%s|c" % delta
Statsd.send(data, sampleRate)
def send(data, sample_rate=1):
Squirt the metrics over UDP
import local_settings as settings
host = settings.statsd_host
port = settings.statsd_port
addr=(host, port)
except Error:
sampled_data = {}
if(sample_rate < 1):
import random
if random.random() <= sample_rate:
for stat in data.keys():
value = data[stat]
sampled_data[stat] = "%s|@%s" %(value, sample_rate)
from socket import *
udp_sock = socket(AF_INET, SOCK_DGRAM)
for stat in sampled_data.keys():
value = data[stat]
send_data = "%s:%s" % (stat, value)
udp_sock.sendto(send_data, addr)
import sys
from pprint import pprint
print "Unexpected error:", pprint(sys.exc_info())
pass # we don't care
Jump to Line
Something went wrong with that request. Please try again.