Skip to content
Closed
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
5 changes: 4 additions & 1 deletion prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
self._timeout = timeout_seconds
self._time = _time

def push(self, prefix=''):
def push(self, prefix='', filter_re=None):
now = int(self._time.time())
output = []

Expand All @@ -60,6 +60,9 @@ def push(self, prefix=''):

for metric in self._registry.collect():
for name, labels, value in metric.samples:
if filter_re and not filter_re.search(name):
continue

if labels:
labelstr = '.' + '.'.join(
['{0}.{1}'.format(
Expand Down
15 changes: 14 additions & 1 deletion tests/graphite_bridge.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import threading
import re
try:
import SocketServer
except ImportError:
Expand Down Expand Up @@ -27,7 +28,7 @@ def run(self):
server.socket.close()
self.t = ServingThread()
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, _time=FakeTime())
Expand Down Expand Up @@ -59,6 +60,18 @@ def test_prefix(self):

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

def test_filter_re(self):
labels = Counter('labels', 'help', ['a', 'b'], registry=self.registry)
labels.labels('c', 'd').inc()

other = Counter('other', 'help', ['a', 'b'], registry=self.registry)
other.labels('c', 'd').inc()

self.gb.push(filter_re = re.compile(r'.*bel.*'))
self.t.join()

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

def test_sanitizing(self):
labels = Counter('labels', 'help', ['a'], registry=self.registry)
labels.labels('c.:8').inc()
Expand Down