8000 Merge pull request #56 from kvikas/master · Syncano/client_python@fc5a6e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc5a6e3

Browse files
committed
Merge pull request prometheus#56 from kvikas/master
Support for namespace in graphite bridge
2 parents 1b88cd0 + c5fff61 commit fc5a6e3

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

prometheus_client/bridge/graphite.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ def _sanitize(s):
1919

2020

2121
class _RegularPush(threading.Thread):
22-
def __init__(self, pusher, interval):
22+
def __init__(self, pusher, interval, prefix):
2323
super(_RegularPush, self).__init__()
2424
self._pusher = pusher
2525
self._interval = interval
26+
self._prefix = prefix
2627

2728
def run(self):
2829
wait_until = time.time()
@@ -37,7 +38,7 @@ def run(self):
3738
# time.sleep can return early.
3839
time.sleep(wait_until - now)
3940
try:
40-
self._pusher.push()
41+
self._pusher.push(prefix=self._prefix)
4142
except IOError:
4243
logging.exception("Push failed")
4344

@@ -49,9 +50,14 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
4950
self._timeout = timeout_seconds
5051
self._time = _time
5152

52-
def push(self):
53+
def push(self, prefix=''):
5354
now = int(self._time.time())
5455
output = []
56+
57+
prefixstr = ''
58+
if prefix:
59+
prefixstr = prefix + '.'
60+
5561
for metric in self._registry.collect():
5662
for name, labels, value in metric._samples:
5763
if labels:
@@ -61,14 +67,14 @@ def push(self):
6167
for k, v in sorted(labels.items())])
6268
else:
6369
labelstr = ''
64-
output.append('{0}{1} {2} {3}\n'.format(
65-
_sanitize(name), labelstr, float(value), now))
70+
output.append('{0}{1}{2} {3} {4}\n'.format(
71+
prefixstr, _sanitize(name), labelstr, float(value), now))
6672

6773
conn = socket.create_connection(self._address, self._timeout)
6874
conn.sendall(''.join(output).encode('ascii'))
6975
conn.close()
7076

71-
def start(self, interval=60.0):
72-
t = _RegularPush(self, interval)
77+
def start(self, interval=60.0, prefix=''):
78+
t = _RegularPush(self, interval, prefix)
7379
t.daemon = True
7480
t.start()

tests/graphite_bridge.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def test_labels(self):
5050

5151
self.assertEqual(b'labels.a.c.b.d 1.0 1434898897\n', self.data)
5252

53+
def test_prefix(self):
54+
labels = Counter('labels', 'help', ['a', 'b'], registry=self.registry)
55+
labels.labels('c', 'd').inc()
56+
57+
self.gb.push(prefix = 'pre.fix')
58+
self.t.join()
59+
60+
self.assertEqual(b'pre.fix.labels.a.c.b.d 1.0 1434898897\n', self.data)
61+
5362
def test_sanitizing(self):
5463
labels = Counter('labels', 'help', ['a'], registry=self.registry)
5564
labels.labels('c.:8').inc()

0 commit comments

Comments
 (0)
0