8000 Add tags support to GraphiteBridge (#618) · Kevin161111/client_python@ccb8395 · GitHub
[go: up one dir, main page]

Skip to content

Commit ccb8395

Browse files
auth 8000 ored
Add tags support to GraphiteBridge (prometheus#618)
Add tags support to GraphiteBridge Graphite has support for tagged metrics with a syntax very similar to the non-tagged format. Update GraphiteBridge to support it. Signed-off-by: Matt Wilder <me@partcyb.org>
1 parent 83f359b commit ccb8395

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,17 @@ gb.push()
447447
gb.start(10.0)
448448
```
449449

450+
Graphite [tags](https://grafana.com/blog/2018/01/11/graphite-1.1-teaching-an-old-dog-new-tricks/) are also supported.
451+
452+
```python
453+
from prometheus_client.bridge.graphite import GraphiteBridge
454+
455+
gb = GraphiteBridge(('graphite.your.org', 2003), tags=True)
456+
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
457+
c.labels('get', '/').inc()
458+
gb.push()
459+
```
460+
450461
## Custom Collectors
451462

452463
Sometimes it is not possible to directly instrument code, as it is not

prometheus_client/bridge/graphite.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ def run(self):
4646

4747

4848
class GraphiteBridge(object):
49-
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time):
49+
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time, tags=False):
5050
self._address = address
5151
self._registry = registry
52+
self._tags = tags
5253
self._timeout = timeout_seconds
5354
self._timer = _timer
5455

@@ -63,8 +64,14 @@ def push(self, prefix=''):
6364
for metric in self._registry.collect():
6465
for s in metric.samples:
6566
if s.labels:
66-
labelstr = '.' + '.'.join(
67-
['{0}.{1}'.format(
67+
if self._tags:
68+
sep = ';'
69+
fmt = '{0}={1}'
70+
else:
71+
sep = '.'
72+
fmt = '{0}.{1}'
73+
labelstr = sep + sep.join(
74+
[fmt.format(
6875
_sanitize(k), _sanitize(v))
6976
for k, v in sorted(s.labels.items())])
7077
else:

tests/test_graphite_bridge.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ def run(self):
3535
self.t.start()
3636

3737
# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
38-
address = ('localhost', server.server_address[1])
39-
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)
38+
self.address = ('localhost', server.server_address[1])
39+
self.gb = GraphiteBridge(self.address, self.registry, _timer=fake_timer)
40+
41+
def _use_tags(self):
42+
self.gb = GraphiteBridge(self.address, self.registry, tags=True, _timer=fake_timer)
4043

4144
def test_nolabels(self):
4245
gauge = Gauge('g', 'help', registry=self.registry)
@@ -56,6 +59,16 @@ def test_labels(self):
5659

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

62+
def test_labels_tags(self):
63+
self._use_tags()
64+
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
65+
labels.labels('c', 'd').inc()
66+
67+
self.gb.push()
68+
self.t.join()
69+
70+
self.assertEqual(b'labels;a=c;b=d 1.0 1434898897\n', self.data)
71+
5972
def test_prefix(self):
6073
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
6174
labels.labels('c', 'd').inc()
@@ -65,6 +78,16 @@ def test_prefix(self):
6578

6679
self.assertEqual(b'pre.fix.labels.a.c.b.d 1.0 1434898897\n', self.data)
6780

81+
def test_prefix_tags(self):
82+
self._use_tags()
83+
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
84+
labels.labels('c', 'd').inc()
85+
86+
self.gb.push(prefix='pre.fix')
87+
self.t.join()
88+
89+
self.assertEqual(b'pre.fix.labels;a=c;b=d 1.0 1434898897\n', self.data)
90+
6891
def test_sanitizing(self):
6992
labels = Gauge('labels', 'help', ['a'], registry=self.registry)
7093
labels.labels('c.:8').inc()
@@ -73,3 +96,13 @@ def test_sanitizing(self):
7396
self.t.join()
7497

7598
self.assertEqual(b'labels.a.c__8 1.0 1434898897\n', self.data)
99+
100+
def test_sanitizing_tags(self):
101+
self._use_tags()
102+
labels = Gauge('labels', 'help', ['a'], registry=self.registry)
103+
labels.labels('c.:8').inc()
104+
105+
self.gb.push()
106+
self.t.join()
107+
108+
self.assertEqual(b'labels;a=c__8 1.0 1434898897\n', self.data)

0 commit comments

Comments
 (0)
0