13
13
# We also remove periods, so labels can be distinguished.
14
14
_INVALID_GRAPHITE_CHARS = re .compile (r"[^a-zA-Z0-9_-]" )
15
15
16
+
16
17
def _sanitize (s ):
17
- return _INVALID_GRAPHITE_CHARS .sub ('_' , s )
18
-
18
+ return _INVALID_GRAPHITE_CHARS .sub ('_' , s )
19
19
20
- class _RegularPush (threading .Thread ):
21
20
21
+ class _RegularPush (threading .Thread ):
22
22
def __init__ (self , pusher , interval ):
23
23
super (_RegularPush , self ).__init__ ()
24
24
self ._pusher = pusher
@@ -30,15 +30,15 @@ def run(self):
30
30
while True :
31
31
now = time .time ()
32
32
if now >= wait_until :
33
- # May need to skip some pushes.
34
- while wait_until < now :
35
- wait_until += self ._interval
36
- break
33
+ # May need to skip some pushes.
34
+ while wait_until < now :
35
+ wait_until += self ._interval
36
+ break
37
37
# time.sleep can return early.
38
38
10000
time .sleep (wait_until - now )
39
39
try :
40
40
self ._pusher .push ()
41
- except IOError as e :
41
+ except IOError :
42
42
logging .exception ("Push failed" )
43
43
44
44
@@ -50,27 +50,25 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
50
50
self ._time = _time
51
51
52
52
def push (self ):
53
- now = int (self ._time .time ())
54
- output = []
55
- for metric in self ._registry .collect ():
56
- for name , labels , value in metric ._samples :
57
- if labels :
58
- labelstr = '.' + '.' .join (
59
- ['{0}.{1}' .format (
60
- _sanitize (k ), _sanitize (v ))
61
- for k , v in sorted (labels .items ())])
62
- else :
63
- labelstr = ''
64
- output .append ('{0}{1} {2} {3}\n ' .format (
65
- _sanitize (name ), labelstr , float (value ), now ))
53
+ now = int (self ._time .time ())
54
+ output = []
55
+ for metric in self ._registry .collect ():
56
+ for name , labels , value in metric ._samples :
57
+ if labels :
58
+ labelstr = '.' + '.' .join (
59
+ ['{0}.{1}' .format (
60
+ _sanitize (k ), _sanitize (v ))
61
+ for k , v in sorted (labels .items ())])
62
+ else :
63
+ labelstr = ''
64
+ output .append ('{0}{1} {2} {3}\n ' .format (
65
+ _sanitize (name ), labelstr , float (value ), now ))
66
66
67
- conn = socket .create_connection (self ._address , self ._timeout )
68
- conn .sendall ('' .join (output ).encode ('ascii' ))
69
- conn .close ()
67
+ conn = socket .create_connection (self ._address , self ._timeout )
68
+ conn .sendall ('' .join (output ).encode ('ascii' ))
69
+ conn .close ()
70
70
71
71
def start (self , interval = 60.0 ):
72
72
t = _RegularPush (self , interval )
73
73
t .daemon = True
74
74
t .start ()
75
-
76
-
0 commit comments