-
Notifications
You must be signed in to change notification settings - Fork 24
/
statsd.py
84 lines (75 loc) · 2.41 KB
/
statsd.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
'''
Created on Jun 14, 2012
@author: Yangming
'''
import os, sys
_rootpath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(_rootpath, "statsd"))
try:
from afstatsd import Statsd, AFTransport
Statsd.set_transport(AFTransport())
except:
print "Statsd Library is not available, check PYTHON_PATH"
Statsd = None
TIMER_BUCKET_PATTERN = "sys.app.timer.%(appname)s.%(name)s"
COUNTER_BUCKET_PATTERN = "sys.app.counter.%(appname)s.%(name)s"
GAUGE_BUCKET_PATTERN = "sys.app.gauge.%(appname)s.%(name)s"
def set_timer_bucket_pattern(pattern):
global TIMER_BUCKET_PATTERN
TIMER_BUCKET_PATTERN = pattern
def set_counter_bucket_pattern(pattern):
global COUNTER_BUCKET_PATTERN
COUNTER_BUCKET_PATTERN = pattern
def set_gauge_bucket_pattern(pattern):
global GAUGE_BUCKET_PATTERN
GAUGE_BUCKET_PATTERN = pattern
def timer(method):
def send_statsd(*args, **kwargs):
result = method(*args, **kwargs)
bucket = TIMER_BUCKET_PATTERN % result
# TODO: deal with more than one performance data
if len(result.perf_data_list):
value = result.perf_data_list[0]['value']
Statsd.timing(bucket, value)
return result
if Statsd:
return send_statsd
else:
return method
def counter(method):
def send_statsd(*args, **kwargs):
result = method(*args, **kwargs)
bucket = COUNTER_BUCKET_PATTERN % result
# TODO: deal with more than one performance data
if len(result.perf_data_list):
value = result.perf_data_list[0]['value']
Statsd.update_stats(bucket)
return result
if Statsd:
return send_statsd
else:
return method
def gauge(method):
def send_statsd(*args, **kwargs):
result = method(*args, **kwargs)
bucket = GAUGE_BUCKET_PATTERN % result
# TODO: deal with more than one performance data
if len(result.perf_data_list):
value = result.perf_data_list[0]['value']
Statsd.gauge(bucket, value)
return result
if Statsd:
return send_statsd
else:
return method
if __name__ == "__main__":
import nagios
Statsd._transport.verbosity = True
@timer
@gauge
@counter
def test_wrapper():
result = nagios.Result("STATSWRAPPER", 0, appname="nagios")
result.add_performance_data("total", 1)
return result
test_wrapper()