Skip to content
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
20 changes: 13 additions & 7 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ def _sanitize(s):


class _RegularPush(threading.Thread):
def __init__(self, pusher, interval):
def __init__(self, pusher, interval, prefix):
super(_RegularPush, self).__init__()
self._pusher = pusher
self._interval = interval
self._prefix = prefix

def run(self):
wait_until = time.time()
Expand All @@ -37,7 +38,7 @@ def run(self):
# time.sleep can return early.
time.sleep(wait_until - now)
try:
self._pusher.push()
self._pusher.push(prefix=self._prefix)
except IOError:
logging.exception("Push failed")

Expand All @@ -49,9 +50,14 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
self._timeout = timeout_seconds
self._time = _time

def push(self):
def push(self, prefix=''):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start() needs this option too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

now = int(self._time.time())
output = []

prefixstr = ''
if prefix:
prefixstr = prefix + '.'

for metric in self._registry.collect():
for name, labels, value in metric._samples:
if labels:
Expand All @@ -61,14 +67,14 @@ def push(self):
for k, v in sorted(labels.items())])
else:
labelstr = ''
output.append('{0}{1} {2} {3}\n'.format(
_sanitize(name), labelstr, float(value), now))
output.append('{0}{1}{2} {3} {4}\n'.format(
prefixstr, _sanitize(name), labelstr, float(value), now))

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

def start(self, interval=60.0):
t = _RegularPush(self, interval)
def start(self, interval=60.0, prefix=''):
t = _RegularPush(self, interval, prefix)
t.daemon = True
t.start()
9 changes: 9 additions & 0 deletions tests/graphite_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def test_labels(self):

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

def test_prefix(self):
labels = Counter('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 = Counter('labels', 'help', ['a'], registry=self.registry)
labels.labels('c.:8').inc()
Expand Down