2011-02-16 08:17:48 +03:00
|
|
|
# python_example.py
|
|
|
|
|
|
|
|
# Steve Ivy <steveivy@gmail.com>
|
|
|
|
# http://monkinetic.com
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2011-02-16 08:17:48 +03:00
|
|
|
# this file expects local_settings.py to be in the same dir, with statsd host and port information:
|
2012-08-30 21:04:47 +04:00
|
|
|
#
|
2011-02-16 08:17:48 +03:00
|
|
|
# statsd_host = 'localhost'
|
|
|
|
# statsd_port = 8125
|
|
|
|
|
2011-02-16 08:05:42 +03:00
|
|
|
# Sends statistics to the stats daemon over UDP
|
|
|
|
class Statsd(object):
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2012-11-12 20:20:43 +04:00
|
|
|
def __init__(self, host=localhost, port=8125):
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
try:
|
|
|
|
import local_settings as settings
|
|
|
|
self.host = settings.statsd_host
|
|
|
|
self.port = settings.statsd_port
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
self.addr=(host, port)
|
|
|
|
|
2011-02-16 08:05:42 +03:00
|
|
|
@staticmethod
|
2011-02-17 19:27:47 +03:00
|
|
|
def timing(stat, time, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Log timing information
|
2011-02-16 17:27:50 +03:00
|
|
|
>>> from python_example import Statsd
|
2011-02-17 19:29:45 +03:00
|
|
|
>>> Statsd.timing('some.time', 500)
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2011-02-17 19:27:47 +03:00
|
|
|
stats = {}
|
|
|
|
stats[stat] = "%d|ms" % time
|
|
|
|
Statsd.send(stats, sample_rate)
|
2011-02-16 08:05:42 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def increment(stats, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Increments one or more stats counters
|
|
|
|
>>> Statsd.increment('some.int')
|
2011-02-16 08:50:22 +03:00
|
|
|
>>> Statsd.increment('some.int',0.5)
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
2011-02-16 08:05:42 +03:00
|
|
|
Statsd.update_stats(stats, 1, sample_rate)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decrement(stats, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Decrements one or more stats counters
|
|
|
|
>>> Statsd.decrement('some.int')
|
|
|
|
"""
|
2011-02-16 08:05:42 +03:00
|
|
|
Statsd.update_stats(stats, -1, sample_rate)
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2011-02-16 08:05:42 +03:00
|
|
|
@staticmethod
|
|
|
|
def update_stats(stats, delta=1, sampleRate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Updates one or more stats counters by arbitrary amounts
|
|
|
|
>>> Statsd.update_stats('some.int',10)
|
|
|
|
"""
|
2011-02-16 08:05:42 +03:00
|
|
|
if (type(stats) is not list):
|
|
|
|
stats = [stats]
|
|
|
|
data = {}
|
|
|
|
for stat in stats:
|
|
|
|
data[stat] = "%s|c" % delta
|
|
|
|
|
2011-02-16 08:17:48 +03:00
|
|
|
Statsd.send(data, sampleRate)
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2011-02-16 08:05:42 +03:00
|
|
|
@staticmethod
|
|
|
|
def send(data, sample_rate=1):
|
2011-02-16 08:26:43 +03:00
|
|
|
"""
|
|
|
|
Squirt the metrics over UDP
|
|
|
|
"""
|
2011-02-16 08:05:42 +03:00
|
|
|
try:
|
|
|
|
import local_settings as settings
|
2011-02-16 08:17:48 +03:00
|
|
|
host = settings.statsd_host
|
|
|
|
port = settings.statsd_port
|
|
|
|
addr=(host, port)
|
2012-08-30 21:04:13 +04:00
|
|
|
except:
|
2011-02-16 08:05:42 +03:00
|
|
|
exit(1)
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2011-02-16 08:50:22 +03:00
|
|
|
sampled_data = {}
|
2012-08-30 21:04:47 +04:00
|
|
|
|
2011-02-16 08:05:42 +03:00
|
|
|
if(sample_rate < 1):
|
2011-02-16 08:50:22 +03:00
|
|
|
import random
|
|
|
|
if random.random() <= sample_rate:
|
|
|
|
for stat in data.keys():
|
2012-06-15 23:58:43 +04:00
|
|
|
value = data[stat]
|
2011-02-16 08:50:22 +03:00
|
|
|
sampled_data[stat] = "%s|@%s" %(value, sample_rate)
|
2011-02-16 08:05:42 +03:00
|
|
|
else:
|
|
|
|
sampled_data=data
|
2012-08-30 21:04:47 +04:00
|
|
|
|
|
|
|
from socket import socket, AF_INET, SOCK_DGRAM
|
2011-02-16 08:05:42 +03:00
|
|
|
udp_sock = socket(AF_INET, SOCK_DGRAM)
|
|
|
|
try:
|
|
|
|
for stat in sampled_data.keys():
|
2012-06-15 23:58:43 +04:00
|
|
|
value = sampled_data[stat]
|
2011-02-16 08:05:42 +03:00
|
|
|
send_data = "%s:%s" % (stat, value)
|
2012-11-12 20:20:43 +04:00
|
|
|
udp_sock.sendto(send_data, self.addr)
|
2011-02-16 08:17:48 +03:00
|
|
|
except:
|
|
|
|
import sys
|
2011-02-16 08:05:42 +03:00
|
|
|
from pprint import pprint
|
2011-02-16 08:17:48 +03:00
|
|
|
print "Unexpected error:", pprint(sys.exc_info())
|
2012-08-30 21:04:13 +04:00
|
|
|
pass # we don't care
|
2012-11-12 20:20:43 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
c = StatsdClient()
|
|
|
|
c.increment('example.python')
|