Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 88 lines (76 sloc) 2.538 kb
b265799b » Steve Ivy
2011-02-15 fix local_settings.py (Sir Not-Appearing-In-This-Repo) and cleanup so…
1 # python_example.py
2
3 # Steve Ivy <steveivy@gmail.com>
4 # http://monkinetic.com
5
6 # this file expects local_settings.py to be in the same dir, with statsd host and port information:
7 #
8 # statsd_host = 'localhost'
9 # statsd_port = 8125
10
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
11 # Sends statistics to the stats daemon over UDP
12 class Statsd(object):
13
14 @staticmethod
15 def timing(stats, time, sample_rate=1):
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
16 """
17 Log timing information
18 >>> Statsd.timing('some.time','500|ms')
19 """
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
20 Statsd.update_stats(stats, time, sample_rate)
21
22 @staticmethod
23 def increment(stats, sample_rate=1):
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
24 """
25 Increments one or more stats counters
26 >>> Statsd.increment('some.int')
9866436e » Steve Ivy
2011-02-15 fix sampling
27 >>> Statsd.increment('some.int',0.5)
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
28 """
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
29 Statsd.update_stats(stats, 1, sample_rate)
30
31 @staticmethod
32 def decrement(stats, sample_rate=1):
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
33 """
34 Decrements one or more stats counters
35 >>> Statsd.decrement('some.int')
36 """
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
37 Statsd.update_stats(stats, -1, sample_rate)
38
39 @staticmethod
40 def update_stats(stats, delta=1, sampleRate=1):
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
41 """
42 Updates one or more stats counters by arbitrary amounts
43 >>> Statsd.update_stats('some.int',10)
44 """
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
45 if (type(stats) is not list):
46 stats = [stats]
47 data = {}
48 for stat in stats:
49 data[stat] = "%s|c" % delta
50
b265799b » Steve Ivy
2011-02-15 fix local_settings.py (Sir Not-Appearing-In-This-Repo) and cleanup so…
51 Statsd.send(data, sampleRate)
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
52
53 @staticmethod
54 def send(data, sample_rate=1):
a469756a » Steve Ivy
2011-02-15 fix doc strings, add shell examples
55 """
56 Squirt the metrics over UDP
57 """
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
58 try:
59 import local_settings as settings
b265799b » Steve Ivy
2011-02-15 fix local_settings.py (Sir Not-Appearing-In-This-Repo) and cleanup so…
60 host = settings.statsd_host
61 port = settings.statsd_port
62 addr=(host, port)
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
63 except Error:
64 exit(1)
65
9866436e » Steve Ivy
2011-02-15 fix sampling
66 sampled_data = {}
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
67
68 if(sample_rate < 1):
9866436e » Steve Ivy
2011-02-15 fix sampling
69 import random
70 if random.random() <= sample_rate:
71 for stat in data.keys():
72 value = data[stat]
73 sampled_data[stat] = "%s|@%s" %(value, sample_rate)
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
74 else:
75 sampled_data=data
76
77 from socket import *
78 udp_sock = socket(AF_INET, SOCK_DGRAM)
79 try:
80 for stat in sampled_data.keys():
81 value = data[stat]
82 send_data = "%s:%s" % (stat, value)
83 udp_sock.sendto(send_data, addr)
b265799b » Steve Ivy
2011-02-15 fix local_settings.py (Sir Not-Appearing-In-This-Repo) and cleanup so…
84 except:
85 import sys
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
86 from pprint import pprint
b265799b » Steve Ivy
2011-02-15 fix local_settings.py (Sir Not-Appearing-In-This-Repo) and cleanup so…
87 print "Unexpected error:", pprint(sys.exc_info())
1014cec2 » Steve Ivy
2011-02-15 first pass at a python version of the Statsd client
88 pass # we don't care
Something went wrong with that request. Please try again.