-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsdclient.py
184 lines (152 loc) · 5.46 KB
/
statsdclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Statsd client based on sample code by Steve Ivy <[email protected]>
http://monkinetic.com.
Added support for metric prefix and 'timeit' function.
"""
from random import random
from socket import socket, AF_INET, SOCK_DGRAM
import time
class StatsdClient(object):
"""A client for sending metrics to statsd"""
SC_TIMING = "ms"
SC_COUNT = "c"
SC_GAUGE = "g"
SC_SET = "s"
def __init__(self, host='localhost', port=8125, prefix=""):
"""
Sends statistics to the stats daemon over UDP
>>> client = StatsdClient()
"""
self.addr = (host, port)
self.prefix = prefix + "." if prefix else ""
def timing(self, stats, value):
"""
Log timing information
>>> client = StatsdClient()
>>> client.timing('example.timing', 500)
>>> client.timing(('example.timing23', 'example.timing29'), 500)
"""
self.update_stats(stats, value, self.SC_TIMING)
def gauge(self, stats, value):
"""
Log gauges
>>> client = StatsdClient()
>>> client.gauge('example.gauge', 47)
>>> client.gauge(('example.gauge41', 'example.gauge43'), 47)
"""
self.update_stats(stats, value, self.SC_GAUGE)
def set(self, stats, value):
"""
Log set
>>> client = StatsdClient()
>>> client.set('example.set', "set")
>>> client.set(('example.set61', 'example.set67'), "2701")
"""
self.update_stats(stats, value, self.SC_SET)
def increment(self, stats, sample_rate=1):
"""
Increments one or more stats counters
>>> client = StatsdClient()
>>> client.increment('example.increment')
>>> client.increment('example.increment', 0.5)
"""
self.count(stats, 1, sample_rate)
def decrement(self, stats, sample_rate=1):
"""
Decrements one or more stats counters
>>> client = StatsdClient()
>>> client.decrement('example.decrement')
"""
self.count(stats, -1, sample_rate)
def count(self, stats, value, sample_rate=1):
"""
Updates one or more stats counters by arbitrary value
>>> client = StatsdClient()
>>> client.count('example.counter', 17)
"""
self.update_stats(stats, value, self.SC_COUNT, sample_rate)
def timeit(self, metric, func, *args, **kwargs):
"""
Times given function and log metric in ms for duration of execution.
>>> import time
>>> client = StatsdClient()
>>> client.timeit("latency", time.sleep, 0.5)
"""
(res, seconds) = timeit(func, *args, **kwargs)
self.timing(metric, seconds * 1000.0)
return res
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, self.prefix)
self.send(self.sample(stats, sample_rate), self.addr)
@staticmethod
def format(keys, value, _type, prefix=""):
"""
General format function.
>>> StatsdClient.format("example.format", 2, "T")
{'example.format': '2|T'}
>>> StatsdClient.format(("example.format31", "example.format37"), "2",
... "T")
{'example.format31': '2|T', 'example.format37': '2|T'}
>>> StatsdClient.format("example.format", 2, "T", "prefix.")
{'prefix.example.format': '2|T'}
"""
data = {}
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[prefix + key] = value
return data
@staticmethod
def sample(data, sample_rate):
"""
Sample data dict
TODO(rbtz@): Convert to generator
>>> StatsdClient.sample({"example.sample2": "2"}, 1)
{'example.sample2': '2'}
>>> StatsdClient.sample({"example.sample3": "3"}, 0)
{}
>>> from random import seed
>>> seed(1)
>>> StatsdClient.sample({"example.sample5": "5",
... "example.sample7": "7"}, 0.99)
{'example.sample5': '5|@0.99', 'example.sample7': '7|@0.99'}
>>> StatsdClient.sample({"example.sample5": "5",
... "example.sample7": "7"}, 0.01)
{}
"""
if sample_rate >= 1:
return data
elif sample_rate < 1:
if random() <= sample_rate:
sampled_data = {}
for stat, value in data.items():
sampled_data[stat] = "{0}|@{1}".format(value, sample_rate)
return sampled_data
return {}
@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
udp_sock = socket(AF_INET, SOCK_DGRAM)
# TODO(rbtz@): Add batch support
for item in _dict.items():
udp_sock.sendto(":".join(item).encode('utf-8'), addr)
def timeit(func, *args, **kwargs):
"""
Time execution of function. Returns (res, seconds).
>>> res, timing = timeit(time.sleep, 1)
"""
start_time = time.time()
res = func(*args, **kwargs)
timing = time.time() - start_time
return res, timing