Skip to content
Merged
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
50 changes: 24 additions & 26 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# We also remove periods, so labels can be distinguished.
_INVALID_GRAPHITE_CHARS = re.compile(r"[^a-zA-Z0-9_-]")


def _sanitize(s):
return _INVALID_GRAPHITE_CHARS.sub('_', s)

return _INVALID_GRAPHITE_CHARS.sub('_', s)

class _RegularPush(threading.Thread):

class _RegularPush(threading.Thread):
def __init__(self, pusher, interval):
super(_RegularPush, self).__init__()
self._pusher = pusher
Expand All @@ -30,15 +30,15 @@ def run(self):
while True:
now = time.time()
if now >= wait_until:
# May need to skip some pushes.
while wait_until < now:
wait_until += self._interval
break
# May need to skip some pushes.
while wait_until < now:
wait_until += self._interval
break
# time.sleep can return early.
time.sleep(wait_until - now)
try:
self._pusher.push()
except IOError as e:
except IOError:
logging.exception("Push failed")


Expand All @@ -50,27 +50,25 @@ def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=ti
self._time = _time

def push(self):
now = int(self._time.time())
output = []
for metric in self._registry.collect():
for name, labels, value in metric._samples:
if labels:
labelstr = '.' + '.'.join(
['{0}.{1}'.format(
_sanitize(k), _sanitize(v))
for k, v in sorted(labels.items())])
else:
labelstr = ''
output.append('{0}{1} {2} {3}\n'.format(
_sanitize(name), labelstr, float(value), now))
now = int(self._time.time())
output = []
for metric in self._registry.collect():
for name, labels, value in metric._samples:
if labels:
labelstr = '.' + '.'.join(
['{0}.{1}'.format(
_sanitize(k), _sanitize(v))
for k, v in sorted(labels.items())])
else:
labelstr = ''
output.append('{0}{1} {2} {3}\n'.format(
_sanitize(name), labelstr, float(value), now))

conn = socket.create_connection(self._address, self._timeout)
conn.sendall(''.join(output).encode('ascii'))
conn.close()
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)
t.daemon = True
t.start()