forked from uber-archive/statsrelay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarbonsink.py
286 lines (234 loc) · 9.36 KB
/
carbonsink.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
"""A sink for statsite to send metrics to carbon.
This supports a few interesting features:
* There's support for a "file monitoring" sink which works by
detecting specially formatted metrics, and updating the mtime of a
file on the local filesystem; this facilitates monitoring.
* Support for buffering metrics, which is useful when moving them
with statsrelay.
"""
import argparse
import contextlib
import logging
import os
import socket
import subprocess
import sys
import time
log = logging.getLogger('carbonsink')
def get_hash(hasher, key):
hasher.stdin.write(key + '\n')
line = hasher.stdout.readline()
out_dict = {}
for part in line.split():
k, v = part.split('=', 1)
if k.endswith('_shard'):
v = int(v, 10)
out_dict[k] = v
return out_dict
class CarbonMetric(object):
"""Representation of a metric in carbon."""
__slots__ = ['key', 'value', 'timestamp']
def __init__(self, key, value, timestamp):
self.key = key.replace(' ', '_')
self.value = value
self.timestamp = int(timestamp, 10)
@classmethod
def from_statsite(cls, line):
key, val, timestamp = line.split('|', 2)
return cls(key, val, timestamp)
def as_carbon_line(self):
return '%s %s %d\n' % (self.key, self.value, self.timestamp)
class Sink(object):
"""Abstraction for sinks."""
def write(self, metric):
raise NotImplemented
class CarbonSink(Sink):
"""A sink which writes to a carbon-cache or carbon-relay daemon."""
def __init__(self, host, port, retries=3):
self.host = host
self.port = int(port)
self.retries = retries
self.sock = None
def connect(self):
if self.sock is None:
log.info('Connecting to %s:%s', self.host, self.port)
self.sock = socket.create_connection(
(self.host, self.port), timeout=10)
def write(self, metric):
"""Write the metric, retrying if necessary."""
for attempt in xrange(self.retries):
carbon_line = metric.as_carbon_line()
try:
self.connect()
self.sock.sendall(carbon_line)
return True
except:
log.exception('Failed to send metrics to %s:%s on attempt %d',
self.host, self.port, attempt)
self.close()
# Only sleep if we are going to retry
if self.retries > 1:
time.sleep(0.1)
log.error('Dropping metric after %d retries', self.retries)
return False
def close(self):
"""Ensure the underlying socket is closed."""
if self.sock is not None:
try:
self.sock.close()
except:
pass
self.sock = None
class FileMonitoringSink(Sink):
"""A sink that writes timestamps for specially formatted stats.
The purpose of this sink is to facilitate monitoring (e.g. from
nagios checks).
"""
def __init__(self, filename, monitoring_stat):
self.filename = filename
self.monitoring_stat = monitoring_stat
def write(self, metric):
if metric.key == self.monitoring_stat:
try:
with open(self.filename, 'w') as f:
f.write('%s\n' % (metric.value,))
except IOError:
log.exception('Failed to write to filename %s', self.filename)
return False
return True
class MetricHandler(object):
"""Handler for metrics.
This class encapsulates the logic for knowing when to buffer
stats, how to write to multiple sinks, and so forth.
"""
STATHASHER_PATH = '/usr/bin/stathasher'
def __init__(self, servers, prefix, retries=3):
servers = [x.rsplit(':', 1) for x in servers]
self.sinks = [CarbonSink(*x, retries=retries) for x in servers]
self.prefix = prefix
self.buffer_shards = set()
self.buffer_dir = None
self.buffer_cache = {}
self.stathasher = None
def add_monitoring_sink(self, filename, metric_to_monitor):
metric_to_monitor = '%s.%s' % (self.prefix, metric_to_monitor)
self.sinks.append(FileMonitoringSink(filename, metric_to_monitor))
def detect_buffer_shards(self, fileobj, buffer_dir,
stathasher_config=None):
"""Populate self.buffer_shards from the buffer file."""
self.buffer_dir = buffer_dir
try:
for line in fileobj:
try:
shard_num = int(line, 10)
except ValueError:
pass
self.buffer_shards.add(shard_num)
except IOError:
# if we can't open the file (e.g. it doesn't exist), then
# there are no buffer shards to exclude
pass
if self.buffer_shards:
args = [self.STATHASHER_PATH]
if stathasher_config is not None:
args.extend(['-c', stathasher_config])
self.stathasher = subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def lookup_buffer(self, shard_num):
"""Get the buffer file for a shard num, for appending to."""
try:
return self.buffer_cache[shard_num]
except KeyError:
filename = os.path.join(
self.buffer_dir, 'shard_%s.txt' % (shard_num,))
file_obj = open(filename, 'a')
self.buffer_cache[shard_num] = file_obj
return file_obj
def close(self):
"""Ensure the stathasher process terminates."""
if self.stathasher and self.stathasher.returncode is None:
self.stathasher.kill()
for v in self.buffer_cache.itervalues():
v.close()
def buffer_line(self, line, metric):
"""Buffer the line if buffering is enabled for this metric.
Returns True if the line is buffered, False otherwise.
"""
if not self.buffer_shards:
return False
hashval = get_hash(self.stathasher, metric.key)
shard = hashval['carbon_shard']
if shard in self.buffer_shards:
file_obj = self.lookup_buffer(shard)
file_obj.write(metric.as_carbon_line())
file_obj.flush()
return True
else:
return False
def handle_metric(self, line):
key, val, timestamp = line.split('|', 2)
metric = CarbonMetric(self.prefix + '.' + key, val, timestamp)
if not self.buffer_line(line, metric):
# The above call will buffer the line if buffering is
# enabled and suitable for the metric; in the typical
# case, no buffering will happen, and instead we write the
# metric to all of the sinks.
for sink in self.sinks:
if not sink.write(metric):
log.error(
'Hard failure sending to %r, giving up on it', sink)
continue
@contextlib.contextmanager
def metric_handler(*args, **kwargs):
with contextlib.closing(MetricHandler(*args, **kwargs)) as handler:
yield handler
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--attempts', type=int, default=3)
parser.add_argument('--logfile', default=None)
parser.add_argument('--statsite-instance', default=None)
parser.add_argument('--monitoring-stat', default=None)
parser.add_argument('-c', '--cache-directory',
default='/var/cache/statsite',
help='directory for cache files')
parser.add_argument('-b', '--buffer-shard-file',
default='/etc/statsrelay_buffer_shards.txt',
help='buffer metrics for shards from this file')
parser.add_argument('--statsrelay-config',
default=None,
help='statsrelay config to use when hashing')
parser.add_argument('prefix')
parser.add_argument('servers', nargs='+')
args = parser.parse_args()
logging.basicConfig()
if args.logfile:
fh = logging.FileHandler(args.logfile)
fh.setFormatter(logging.Formatter(
fmt='%(asctime)s\t%(levelname)s\t%(message)s'))
log.addHandler(fh)
with metric_handler(args.servers, args.prefix,
retries=args.attempts) as handler:
if args.statsite_instance and args.monitoring_stat:
filename = os.path.join(
args.cache_directory, args.statsite_instance)
handler.add_monitoring_sink(filename, args.monitoring_stat)
try:
with open(args.buffer_shard_file) as buffer_shard_file:
handler.detect_buffer_shards(
buffer_shard_file,
args.cache_directory,
args.statsrelay_config)
except (OSError, IOError):
pass
# N.B. we want to force stdin to be read iteratively, to avoid
# buffering when statsite is sending a large amount of
# data. This means that we must explicitly get lines using
# stdin.readline(), and we can't use a map or iterator over
# stdin.
while True:
line = sys.stdin.readline().rstrip()
if not line:
break
handler.handle_metric(line.rstrip())
if __name__ == '__main__':
main()