Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 11 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,17 @@ gb.push()
gb.start(10.0)
```

Graphite [tags](https://grafana.com/blog/2018/01/11/graphite-1.1-teaching-an-old-dog-new-tricks/) are also supported.

```python
from prometheus_client.bridge.graphite import GraphiteBridge

gb = GraphiteBridge(('graphite.your.org', 2003), tags=True)
c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels('get', '/').inc()
gb.push()
```

## Custom Collectors

Sometimes it is not possible to directly instrument code, as it is not
Expand Down
13 changes: 10 additions & 3 deletions 13 prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def run(self):


class GraphiteBridge(object):
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time):
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time, tags=False):
self._address = address
self._registry = registry
self._tags = tags
self._timeout = timeout_seconds
self._timer = _timer

Expand All @@ -63,8 +64,14 @@ def push(self, prefix=''):
for metric in self._registry.collect():
for s in metric.samples:
if s.labels:
labelstr = '.' + '.'.join(
['{0}.{1}'.format(
if self._tags:
sep = ';'
fmt = '{0}={1}'
else:
sep = '.'
fmt = '{0}.{1}'
labelstr = sep + sep.join(
[fmt.format(
_sanitize(k), _sanitize(v))
for k, v in sorted(s.labels.items())])
else:
Expand Down
37 changes: 35 additions & 2 deletions 37 tests/test_graphite_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def run(self):
self.t.start()

# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
address = ('localhost', server.server_address[1])
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)
self.address = ('localhost', server.server_address[1])
self.gb = GraphiteBridge(self.address, self.registry, _timer=fake_timer)

def _use_tags(self):
self.gb = GraphiteBridge(self.address, self.registry, tags=True, _timer=fake_timer)

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

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

def test_labels_tags(self):
self._use_tags()
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
labels.labels('c', 'd').inc()

self.gb.push()
self.t.join()

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

def test_prefix(self):
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
labels.labels('c', 'd').inc()
Expand All @@ -65,6 +78,16 @@ def test_prefix(self):

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

def test_prefix_tags(self):
self._use_tags()
labels = Gauge('labels', 'help', ['a', 'b'], registry=self.registry)
labels.labels('c', 'd').inc()

self.gb.push(prefix='pre.fix')
self.t.join()

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

def test_sanitizing(self):
labels = Gauge('labels', 'help', ['a'], registry=self.registry)
labels.labels('c.:8').inc()
Expand All @@ -73,3 +96,13 @@ def test_sanitizing(self):
self.t.join()

self.assertEqual(b'labels.a.c__8 1.0 1434898897\n', self.data)

def test_sanitizing_tags(self):
self._use_tags()
labels = Gauge('labels', 'help', ['a'], registry=self.registry)
labels.labels('c.:8').inc()

self.gb.push()
self.t.join()

self.assertEqual(b'labels;a=c__8 1.0 1434898897\n', self.data)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.