forked from fidlej/sd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dogstatsd.py
executable file
·350 lines (275 loc) · 10.3 KB
/
dogstatsd.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/python
'''
A Python Statsd implementation with some datadog special sauce.
'''
# stdlib
import httplib as http_client
import logging
import optparse
from random import randrange
import re
import socket
import sys
import time
import threading
from urllib import urlencode
# project
from config import get_config
from checks import gethostname
from util import json
logger = logging.getLogger('dogstatsd')
class Metric(object):
"""
A base metric class that accepts points, slices them into time intervals
and performs roll-ups within those intervals.
"""
def sample(self, value, sample_rate):
""" Add a point to the given metric. """
raise NotImplementedError()
def flush(self, timestamp):
""" Flush all metrics up to the given timestamp. """
raise NotImplementedError()
class Gauge(Metric):
""" A metric that tracks a value at particular points in time. """
def __init__(self, name, tags, hostname):
self.name = name
self.value = None
self.tags = tags
self.hostname = hostname
def sample(self, value, sample_rate):
self.value = value
def flush(self, timestamp):
return [{
'metric' : self.name,
'points' : [(timestamp, self.value)],
'tags' : self.tags,
'host' : self.hostname
}]
class Counter(Metric):
""" A metric that tracks a counter value. """
def __init__(self, name, tags, hostname):
self.name = name
self.value = 0
self.tags = tags
self.hostname = hostname
def sample(self, value, sample_rate):
self.value += value * int(1 / sample_rate)
def flush(self, timestamp):
return [{
'metric' : self.name,
'points' : [(timestamp, self.value)],
'tags' : self.tags,
'host' : self.hostname
}]
class Histogram(Metric):
""" A metric to track the distribution of a set of values. """
def __init__(self, name, tags, hostname):
self.name = name
self.max = float("-inf")
self.min = float("inf")
self.sum = 0
self.count = 0
self.sample_size = 1000
self.samples = []
self.percentiles = [0.75, 0.85, 0.95, 0.99]
self.tags = tags
self.hostname = hostname
def sample(self, value, sample_rate):
self.count += int(1 / sample_rate)
self.samples.append(value)
def flush(self, ts):
if not self.count:
return []
self.samples.sort()
length = len(self.samples)
min_ = self.samples[0]
max_ = self.samples[-1]
avg = self.samples[int(round(length/2 - 1))]
metrics = [
{'host':self.hostname, 'tags': self.tags, 'metric' : '%s.min' % self.name, 'points' : [(ts, min_)]},
{'host':self.hostname, 'tags': self.tags, 'metric' : '%s.max' % self.name, 'points' : [(ts, max_)]},
{'host':self.hostname, 'tags': self.tags, 'metric' : '%s.avg' % self.name, 'points' : [(ts, avg)]},
{'host':self.hostname, 'tags': self.tags, 'metric' : '%s.count' % self.name, 'points' : [(ts, self.count)]},
]
for p in self.percentiles:
val = self.samples[int(round(p * length - 1))]
name = '%s.%spercentile' % (self.name, int(p * 100))
metrics.append({'host': self.hostname, 'tags':self.tags, 'metric': name, 'points': [(ts, val)]})
return metrics
class MetricsAggregator(object):
"""
A metric aggregator class.
"""
def __init__(self, hostname, interval):
self.metrics = {}
self.total_count = 0
self.count = 0
self.metric_type_to_class = {
'g': Gauge,
'c': Counter,
'h': Histogram,
'ms' : Histogram
}
self.hostname = hostname
self.interval = interval
def submit(self, packet):
self.count += 1
# We can have colons in tags, so split once.
name_and_metadata = packet.split(':', 1)
if len(name_and_metadata) != 2:
raise Exception('Unparseable packet: %s' % packet)
name = name_and_metadata[0]
metadata = name_and_metadata[1].split('|')
if len(metadata) < 2:
raise Exception('Unparseable packet: %s' % packet)
# Parse the optional values - sample rate & tags.
sample_rate = 1
tags = None
for m in metadata[2:]:
# Parse the sample rate
if m[0] == '@':
sample_rate = float(m[1:])
assert 0 <= sample_rate <= 1
elif m[0] == '#':
tags = tuple(sorted(m[1:].split(',')))
# Bucket metrics by an interval of a few seconds to avoid race
# conditions betwen the threads.
timestamp = time.time()
interval = timestamp - timestamp % self.interval
context = (interval, name, tags)
if context not in self.metrics:
metric_class = self.metric_type_to_class[metadata[1]]
self.metrics[context] = metric_class(name, tags, self.hostname)
self.metrics[context].sample(float(metadata[0]), sample_rate)
def flush(self, include_diagnostic_stats=True):
# Flush all completed intervals bucketed up to this time.
timestamp = time.time()
interval = timestamp - timestamp % self.interval
# Find all intervals that are completed (don't use a generator here)
past_contexts = [c for c in self.metrics if c[0] < interval]
# Flush all completed metrics and remove them.
metrics = []
for context in past_contexts:
metrics += self.metrics[context].flush(timestamp)
del self.metrics[context]
# Track how many points we see.
if include_diagnostic_stats:
metrics.append({
'host':self.hostname,
'tags':None,
'metric': 'dd.dogstatsd.packet.count',
'points': [(timestamp, self.count)]
})
# Save some stats.
logger.info("received %s payloads since last flush" % self.count)
self.total_count += self.count
self.count = 0
return metrics
class Reporter(threading.Thread):
"""
The reporter periodically sends the aggregated metrics to the
server.
"""
def __init__(self, interval, metrics_aggregator, api_host, api_key=None):
threading.Thread.__init__(self)
self.daemon = True
self.interval = int(interval)
self.finished = threading.Event()
self.metrics_aggregator = metrics_aggregator
self.flush_count = 0
self.api_key = api_key
self.api_host = api_host
self.http_conn_cls = http_client.HTTPSConnection
match = re.match('^(https?)://(.*)', api_host)
if match:
self.api_host = match.group(2)
if match.group(1) == 'http':
self.http_conn_cls = http_client.HTTPConnection
def end(self):
self.finished.set()
def run(self):
logger.info("Reporting to %s every %ss" % (self.api_host, self.interval))
while True:
if self.finished.is_set():
break
self.finished.wait(self.interval)
self.flush()
def flush(self):
try:
self.flush_count += 1
metrics = self.metrics_aggregator.flush()
count = len(metrics)
if not count:
logger.info("Flush #{0}: No metrics to flush.".format(self.flush_count))
return
logger.info("Flush #{0}: flushing {1} metrics".format(self.flush_count, count))
self.submit(metrics)
except:
logger.exception("Error flushing metrics")
def submit(self, metrics):
# HACK - Copy and pasted from dogapi, because it's a bit of a pain to distribute python
# dependencies with the agent.
conn = self.http_conn_cls(self.api_host)
body = json.dumps({"series" : metrics})
headers = {'Content-Type':'application/json'}
method = 'POST'
params = {}
if self.api_key:
params['api_key'] = self.api_key
url = '/api/v1/series?%s' % urlencode(params)
start_time = time.time()
conn.request(method, url, body, headers)
#FIXME: add timeout handling code here
response = conn.getresponse()
duration = round((time.time() - start_time) * 1000.0, 4)
logger.info("%s %s %s%s (%sms)" % (
response.status, method, self.api_host, url, duration))
class Server(object):
"""
A statsd udp server.
"""
def __init__(self, metrics_aggregator, host, port):
self.host = host
self.port = int(port)
self.address = (self.host, self.port)
self.metrics_aggregator = metrics_aggregator
self.buffer_size = 1024
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind(self.address)
def start(self):
""" Run the server. """
logger.info('Starting dogstatsd server on %s' % str(self.address))
# Inline variables to speed up look-ups.
buffer_size = self.buffer_size
aggregator_submit = self.metrics_aggregator.submit
socket_recv = self.socket.recv
while True:
try:
aggregator_submit(socket_recv(buffer_size))
except (KeyboardInterrupt, SystemExit):
break
except:
logger.exception('Error receiving datagram')
def main(config_path=None):
c = get_config(parse_args=False, cfg_path=config_path, init_logging=True)
port = c['dogstatsd_port']
target = c['dogstatsd_target']
interval = c['dogstatsd_interval']
api_key = c['api_key']
host = 'localhost'
hostname = gethostname(c)
rollup_interval = 10
# Create the aggregator (which is the point of communication between the
# server and reporting threads.
aggregator = MetricsAggregator(hostname, rollup_interval)
# Start the reporting thread.
reporter = Reporter(interval, aggregator, target, api_key)
reporter.start()
# Start the server.
server_host = ''
server = Server(aggregator, server_host, port)
server.start()
# If we're here, we're done.
logger.info("Shutting down ...")
if __name__ == '__main__':
main()