From 2494d8850ac4102e0df36614e6a8712e9575edfa Mon Sep 17 00:00:00 2001 From: Andreas Dietrich Date: Fri, 27 May 2016 11:18:54 +0200 Subject: [PATCH] allow changing graphite metric names --- prometheus_client/bridge/graphite.py | 40 +++++++++++++++++----------- setup.py | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/prometheus_client/bridge/graphite.py b/prometheus_client/bridge/graphite.py index a01c3122..b2e3a507 100644 --- a/prometheus_client/bridge/graphite.py +++ b/prometheus_client/bridge/graphite.py @@ -13,6 +13,7 @@ # We also remove periods, so labels can be distinguished. _INVALID_GRAPHITE_CHARS = re.compile(r"[^a-zA-Z0-9_-]") +LOG = logging.getLogger('graphite-bridge') def _sanitize(s): return _INVALID_GRAPHITE_CHARS.sub('_', s) @@ -40,39 +41,46 @@ def run(self): try: self._pusher.push(prefix=self._prefix) except IOError: - logging.exception("Push failed") + LOG.error("Push failed") class GraphiteBridge(object): - def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time): + def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time, label_templates={}): self._address = address self._registry = registry self._timeout = timeout_seconds self._time = _time + self._label_templates = label_templates def push(self, prefix=''): now = int(self._time.time()) output = [] - prefixstr = '' - if prefix: - prefixstr = prefix + '.' + prefixstr = prefix or '' 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())]) + label_dict = dict( + (_sanitize(k), _sanitize(v)) + for k, v in sorted(labels.items())) + label_dict['name'] = name + if name in self._label_templates: + labelstr = self._label_templates[name].format( **label_dict) else: - labelstr = '' - output.append('{0}{1}{2} {3} {4}\n'.format( - prefixstr, _sanitize(name), labelstr, float(value), now)) + labelstr = '.'.join([_sanitize(name)] + + ['{0}.{1}'.format( + _sanitize(k), _sanitize(v)) + for k, v in sorted(labels.items())]) + output.append('{0}.{1} {2} {3}\n'.format( + prefixstr, labelstr, float(value), now)) - conn = socket.create_connection(self._address, self._timeout) - conn.sendall(''.join(output).encode('ascii')) - conn.close() + + try: + conn = socket.create_connection(self._address, self._timeout) + conn.sendall(''.join(output).encode('ascii')) + conn.close() + except Exception as e: + LOG.error('Could not connect to graphite at %s', self._address) def start(self, interval=60.0, prefix=''): t = _RegularPush(self, interval, prefix) diff --git a/setup.py b/setup.py index 976ac0e7..b221a8b7 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name = "prometheus_client", - version = "0.0.13", + version = "0.0.13c", author = "Brian Brazil", author_email = "brian.brazil@robustperception.io", description = ("Python client for the Prometheus monitoring system."),