diff --git a/prometheus_client/bridge/graphite.py b/prometheus_client/bridge/graphite.py index a01c3122..cf8252bd 100644 --- a/prometheus_client/bridge/graphite.py +++ b/prometheus_client/bridge/graphite.py @@ -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 = [] @@ -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( diff --git a/tests/graphite_bridge.py b/tests/graphite_bridge.py index 0ac24208..615ebf9b 100644 --- a/tests/graphite_bridge.py +++ b/tests/graphite_bridge.py @@ -1,5 +1,6 @@ import unittest import threading +import re try: import SocketServer except ImportError: @@ -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()) @@ -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()