-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
440 lines (366 loc) · 16.1 KB
/
util.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/python
import abc
import sys
import time
import urllib2
import json
import os
import logging
import re
import mock
import re
MAX_ATTEMPTS = 6
SLEEP_TIME = 10
class CollectdLogHandler(logging.Handler):
"""Log handler to forward statements to collectd
A custom log handler that forwards log messages raised
at level debug, info, notice, warning, and error
to collectd's built in logging. Suppresses extraneous
info and debug statements using a "verbose" boolean
Inherits from logging.Handler
Arguments
collectd -- the collectd object to use
plugin -- name of the plugin (default 'unknown')
verbose -- enable/disable verbose messages (default False)
"""
def __init__(self, collectd, plugin="vitess", verbose=False):
"""Initializes CollectdLogHandler
Arguments
collectd -- the collectd object to use
plugin -- string name of the plugin (default 'unknown')
verbose -- enable/disable verbose messages (default False)
"""
self.collectd = collectd
self.verbose = verbose
self.plugin = plugin
logging.Handler.__init__(self, level=logging.NOTSET)
def register(self):
logger.addHandler(self)
def emit(self, record):
"""
Emits a log record to the appropraite collectd log function
Arguments
record -- str log record to be emitted
"""
try:
if record.msg is not None:
if record.levelname == 'ERROR':
self.collectd.error('%s : %s' % (self.plugin, record.msg))
elif record.levelname == 'WARNING':
self.collectd.warning('%s : %s' % (self.plugin, record.msg))
elif record.levelname == 'NOTICE':
self.collectd.notice('%s : %s' % (self.plugin, record.msg))
elif record.levelname == 'INFO' and self.verbose is True:
self.collectd.info('%s : %s' % (self.plugin, record.msg))
elif record.levelname == 'DEBUG' and self.verbose is True:
self.collectd.debug('%s : %s' % (self.plugin, record.msg))
except Exception as e:
self.collectd.warning(('{p} [ERROR]: Failed to write log statement due '
'to: {e}').format(p=self.plugin,
e=e
))
class CollectdLogger(logging.Logger):
"""Logs all collectd log levels via python's logging library
Custom python logger that forwards log statements at
level: debug, info, notice, warning, error
Inherits from logging.Logger
Arguments
name -- name of the logger
level -- log level to filter by
"""
def __init__(self, name, level=logging.NOTSET):
"""Initializes CollectdLogger
Arguments
name -- name of the logger
level -- log level to filter by
"""
logging.Logger.__init__(self, name, level)
logging.addLevelName(25, 'NOTICE')
def notice(self, msg):
"""Logs a 'NOTICE' level statement at level 25
Arguments
msg - log statement to be logged as 'NOTICE'
"""
self.log(25, msg)
# Set up logging
logging.setLoggerClass(CollectdLogger)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
def log(message):
logger.info(message)
def entry(method_name):
logger.debug("In: " + method_name)
def leave(method_name):
logger.debug("Out: " + method_name)
def get_json_data(url):
response = None
try:
logger.info('Fetching api information from: %s' % url)
request = urllib2.Request(url)
response = urllib2.urlopen(request, timeout=10)
logger.debug('Raw api response: %s' % response)
return json.load(response)
except (urllib2.URLError, urllib2.HTTPError), e:
logger.error('Error connecting to %s - %r : %s' %
(url, e, e))
return None
finally:
if response is not None:
response.close()
def create_metric(epoch_time, metric_name, data, tag_list):
method_name = "create_metric for " + metric_name
entry(method_name)
for key in data.keys():
tags = extract_tags(key, '.', tag_list)
if tags != "-1":
publish_metric(epoch_time, metric_name, data[key], 'gauge', tags)
leave(method_name)
def create_metric_histogram(epoch_time, metric_name, data, tag_list):
method_name = "create_metric_histogram for " + metric_name
entry(method_name)
histogram_data = data['Histograms']
for key in histogram_data.keys():
tags = extract_tags(key, '.', tag_list)
if tags != "-1":
publish_metric(epoch_time, metric_name, histogram_data[key]['Count'], 'gauge', tags)
leave(method_name)
def create_metric_avg(epoch_time, metric_name, data_time, data_count, tag_list):
method_name = "create_metric for " + metric_name
entry(method_name)
for key in data_time.keys():
tags = extract_tags(key, '.', tag_list)
if tags != "-1":
if data_count[key] != 0:
publish_metric(epoch_time, metric_name, (data_time[key]/1000000.0)/data_count[key], 'gauge', tags)
else:
publish_metric(epoch_time, metric_name, 0.0, 'gauge', tags)
leave(method_name)
def extract_tags(key, split_char, tag_list):
tag_data = key.split(split_char)
if len(tag_data) != len(tag_list):
logger.error("extract_tags: Data not as expected for " + key + " tag list: "
+ str(tag_list))
return "-1"
else:
tags = {}
i = 0
for tag in tag_data:
tags[tag_list[i]] = tag.replace(" ", "_")
i += 1
return tags
def extract_tagged_values(data, name, key_split_char='.', tag_list=None):
values = data[name]
if not tag_list:
return [(dict(), value) for value in values.values()]
if type(values) is not dict:
return [(_extract_tags(name, key_split_char, tag_list), values)]
result = []
for key, value in values.items():
result.append((_extract_tags(key, key_split_char, tag_list), value))
return result
def _extract_tags(name, key_split_char='.', tag_list=None):
if not tag_list:
return dict()
tag_data = []
try:
if key_split_char == '.' or key_split_char == '\\':
# names may have escaped periods or slashes, i.e. in usernames, like Foo-web.hs-foo.vitess
# in that case, vitess escapes them with a backslash. We don't want to count those escaped characters
# when splitting. This regex only splits on non-escaped periods/slashes.
tag_data = re.split(r'(?<!\\)%s' % re.escape(key_split_char), name)
# Unescape them.
tag_data[-1] = tag_data[-1].replace(r'\.', '.').replace(r'\\', '\\')
else:
# Otherwise just do a straight split.
tag_data = name.split(key_split_char)
except:
tag_data = name.split(key_split_char)
if len(tag_data) != len(tag_list):
raise Exception("Data not as expected for %s tag list: %s; data: %s" % (name, str(tag_list), str(tag_data)))
return dict(zip(tag_list, [x.replace(" ", "_") for x in tag_data]))
def default_extractor(data, name):
return [(dict(), data[name])]
def nsToMs(ns):
return ns / 1000000.0
def boolval(val):
return val.__str__().lower() == 'true'
def upperSnakeToCamel(val):
if re.match(r'^[A-Z_]+$', val):
return val.title().replace('_', '')
return val
class JsonProvider(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get_json(self):
"""
Return json data for parsing
"""
return
class UrlJsonProvider(JsonProvider):
def __init__(self, host="localhost", port="15101", path="/debug/vars"):
self.host = host
self.port = port
self.path = path
def get_json(self):
url = "http://%s:%s%s" % (self.host, self.port, self.path)
return self._fetch(url)
def _fetch(self, url, attempt=1):
response = None
try:
logger.info('Fetching api information from: %s' % url)
request = urllib2.Request(url)
response = urllib2.urlopen(request, timeout=10)
logger.debug('Raw api response: %s' % response)
return json.load(response)
except (urllib2.URLError, urllib2.HTTPError), e:
if attempt <= MAX_ATTEMPTS:
logger.warning("Failed to get json data. Retrying in %ds" % SLEEP_TIME)
time.sleep(SLEEP_TIME)
return self._fetch(url, attempt + 1)
raise
finally:
if response:
response.close()
class FileJsonProvider(JsonProvider):
def __init__(self, path):
self.path = path
def get_json(self):
with open(self.path, "r") as f:
return json.load(f)
class MetricEmitter(object):
def __init__(self, collectd, plugin_instance, plugin='vitess'):
self.collectd = collectd
self.plugin_instance = plugin_instance
self.plugin = plugin
def emit(self, metric_name, metric_value, type, tags=None):
method_name = "emit_metric for " + metric_name
entry(method_name)
self._emit(metric_name, metric_value, type, tags)
leave(method_name)
def _emit(self, metric_name, metric_value, type, tags):
tag_str = self._generate_tags_str(tags)
val = self.collectd.Values(plugin=self.plugin, plugin_instance=self.plugin_instance)
val.type = type
val.type_instance = "%s.%s.%s%s" % (self.plugin, self.plugin_instance, metric_name, tag_str)
val.values = [metric_value]
val.dispatch()
def _generate_tags_str(self, tags):
if tags:
return "[%s]" % ','.join(["%s=%s" % (x, y) for x, y in tags.items()])
return ""
class BaseCollector(object):
def __init__(self, collectd, name, default_port, json_provider=None, verbose=False, interval=None):
self.collectd = collectd
self.name = name
self.default_port = default_port
self.json_provider = json_provider
self.verbose = verbose
self.interval = interval
self.emitter = MetricEmitter(self.collectd, self.name)
self.include_timing_histograms = True
def configure_callback(self, conf):
json_provider = UrlJsonProvider(port=self.default_port)
for node in conf.children:
if node.key == 'URL':
json_provider.url = node.values[0]
elif node.key == 'Port':
json_provider.port = node.values[0]
elif node.key == 'Path':
json_provider.path = node.values[0]
elif node.key == 'Interval':
self.interval = int(node.values[0])
elif node.key == 'Verbose':
self.verbose = boolval(node.values[0])
elif node.key == 'IncludeTimingHistograms':
self.include_timing_histograms = util.boolval(node.values[0])
handler = CollectdLogHandler(self.collectd, self.name, self.verbose)
handler.register()
self.json_provider = json_provider
def register_read_callback(self):
if self.interval:
self.collectd.register_read(self.read_callback, interval=self.interval)
else:
self.collectd.register_read(self.read_callback)
def read_callback(self):
json = self.json_provider.get_json()
self.process_data(json)
def process_timing_data(self, json_data, timing_name, parse_tags=None):
try:
timing_values = json_data[timing_name]
self.process_metric(timing_values, 'TotalCount', 'counter', prefix=timing_name)
self.process_metric(timing_values, 'TotalTime', 'counter', prefix=timing_name, transformer=nsToMs)
for key, histogram in timing_values['Histograms'].items():
if parse_tags:
tags = _extract_tags(key, tag_list=parse_tags)
prefix = timing_name
else:
tags = None
prefix = "%s%s" % (timing_name, upperSnakeToCamel(key))
self.process_metric(histogram, 'Count', 'counter', prefix=prefix, base_tags=tags)
self.process_metric(histogram, 'Time', 'counter', prefix=prefix, base_tags=tags, transformer=nsToMs)
if self.include_timing_histograms:
def nsKeysToMs(key):
if key.isdigit():
return "%d" % nsToMs(long(key))
return key
self.process_histogram(timing_values['Histograms'], key, prefix=timing_name, alt_name="", suffix="Time", tags=tags, key_transformer=nsKeysToMs)
except KeyError, e:
logger.warning("[KeyError] process_timing_data: Failed to get timing_name '%s' from json data. Skipping." % timing_name)
def process_histogram(self, json_data, metric_name, prefix="", alt_name=None, suffix="", key_transformer=None, tags=None):
try:
histogram = json_data[metric_name]
for key, value in histogram.items():
if key == "Time":
value = nsToMs(value)
if key_transformer:
key = key_transformer(key)
self.emitter.emit("%s%s%sHistogram.%s" % (prefix, alt_name if alt_name is not None else upperSnakeToCamel(metric_name), suffix, key), value, 'gauge', tags)
except KeyError, e:
logger.warning("[KeyError] process_histogram: Failed to get metric_name '%s' from json data. Skipping." % metric_name)
def process_metric(self, json_data, metric_name, type, prefix="", alt_name=None, base_tags=dict(), parse_tags=dict(), transformer=None):
try:
for tags, value in self._extract_values(json_data, metric_name, parse_tags):
all_tags = base_tags.copy() if base_tags else dict()
all_tags.update(tags)
if transformer:
value = transformer(value)
self.emitter.emit("%s%s" % (prefix, alt_name if alt_name else metric_name), value, type, None if not len(all_tags) else all_tags)
except KeyError, e:
logger.warning("[KeyError] process_metric: Failed to get metric_name '%s' from json data. Skipping." % metric_name)
def _extract_values(self, json_data, metric_name, parse_tags):
if parse_tags:
return extract_tagged_values(json_data, metric_name, tag_list=parse_tags)
return default_extractor(json_data, metric_name)
# For running locally, creates some mocks of collectd and allows passing arguments from the command line
def run_local(name, collector):
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument('--host', action='store',
help='Host to get JSON from', default="localhost")
parser.add_argument('--port', action='store',
help='Port on host to get JSON from', default="15101")
parser.add_argument('--host-path', action='store',
help='Path on host to get JSON from', default="/debug/vars")
parser.add_argument('--file-path', action='store',
help='Local file to get JSON from')
parser.add_argument('--interval', action='store',
help='How often (seconds) to output values', default=10)
parser.add_argument('--verbose', '-v', action='store_true',
help='Whether to run in verbose mode')
args = parser.parse_args()
if args.file_path:
json_provider = FileJsonProvider(args.file_path)
else:
json_provider = UrlJsonProvider(host=args.host, port=args.port, path=args.host_path)
collectd = mock.CollectdMock(name)
handler = CollectdLogHandler(collectd, '%s-mock' % name, verbose=args.verbose)
handler.register()
vt = collector(collectd, json_provider, args.verbose)
interval = int(args.interval)
while True:
vt.read_callback()
if interval == 0:
sys.exit(0)
else:
time.sleep(interval)