8000 Support for namespace in graphite bridge #49 · samdroid-apps/client_python@9e2b366 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e2b366

Browse files
author
Vikas Kumar
committed
Support for namespace in graphite bridge prometheus#49
1. An optional `prefix` parameter in `bridge.graphite.GraphiteBridge.push()`. 2. `prefix` is prepended to metrics name before sending to graphite server. 3. Unit test.
1 parent 1b88cd0 commit 9e2b366

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

prometheus_client/bridge/graphite.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
4949
self._timeout = timeout_seconds
5050
self._time = _time
5151

52-
def push(self):
52+
def push(self, prefix=''):
5353
now = int(self._time.time())
5454
output = []
55+
56+
prefixstr = ''
57+
if prefix:
58+
prefixstr = prefix + '.'
59+
5560
for metric in self._registry.collect():
5661
for name, labels, value in metric._samples:
5762
if labels:
@@ -61,8 +66,8 @@ def push(self):
6166
for k, v in sorted(labels.items())])
6267
else:
6368
labelstr = ''
64-
output.append('{0}{1} {2} {3}\n'.format(
65-
_sanitize(name), labelstr, float(value), now))
69+
output.append('{0}{1}{2} {3} {4}\n'.format(
70+
prefixstr, _sanitize(name), labelstr, float(value), now))
6671

6772
conn = socket.create_connection(self._address, self._timeout)
6873
conn.sendall(''.join(output).encode('ascii'))

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