8000 allow changing graphite metric names · prometheus/client_python@2494d88 · GitHub
[go: up one dir, main page]

Skip to content< 8000 /a>

Commit 2494d88

Browse files
allow changing graphite metric names
1 parent 9a188ac commit 2494d88

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

prometheus_client/bridge/graphite.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# We also remove periods, so labels can be distinguished.
1414
_INVALID_GRAPHITE_CHARS = re.compile(r"[^a-zA-Z0-9_-]")
1515

16+
LOG = logging.getLogger('graphite-bridge')
1617

1718
def _sanitize(s):
1819
return _INVALID_GRAPHITE_CHARS.sub('_', s)
@@ -40,39 +41,46 @@ def run(self):
4041
try:
4142
self._pusher.push(prefix=self._prefix)
4243
except IOError:
43-
logging.exception("Push failed")
44+
LOG.error("Push failed")
4445

4546

4647
class GraphiteBridge(object):
47-
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time):
48+
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time, label_templates={}):
4849
self._address = address
4950
self._registry = registry
5051
self._timeout = timeout_seconds
5152
self._time = _time
53+
self._label_templates = label_templates
5254

5355
def push(self, prefix=''):
5456
now = int(self._time.time())
5557
output = []
5658

57-
prefixstr = ''
58-
if prefix:
59-
prefixstr = prefix + '.'
59+
prefixstr = prefix or ''
6060

6161
for metric in self._registry.collect():
6262
for name, labels, value in metric.samples:
63-
if labels:
64-
labelstr = '.' + '.'.join(
65-
['{0}.{1}'.format(
66-
_sanitize(k), _sanitize(v))
67-
for k, v in sorted(labels.items())])
63+
label_dict = dict(
64+
(_sanitize(k), _sanitize(v))
65+
for k, v in sorted(labels.items()))
66+
label_dict['name'] = name
67+
if name in self._label_templates:
68+
labelstr = self._label_templates[name].format( **label_dict)
6869
else:
69-
labelstr = ''
70-
output.append('{0}{1}{2} {3} {4}\n'.format(
71-
prefixstr, _sanitize(name), labelstr, float(value), now))
70+
labelstr = '.'.join([_sanitize(name)] +
71+
['{0}.{1}'.format(
72+
_sanitize(k), _sanitize(v))
73+
for k, v in sorted(labels.items())])
74+
output.append('{0}.{1} {2} {3}\n'.format(
75+
prefixstr, labelstr, float(value), now))
7276

73-
conn = socket.create_connection(self._address, self._timeout)
74-
conn.sendall(''.join(output).encode('ascii'))
75-
conn.close()
77+
78+
try:
79+
conn = socket.create_connection(self._address, self._timeout)
80+
conn.sendall(''.join(output).encode('ascii'))
81+
conn.close()
82+
except Exception as e:
83+
LOG.error('Could not connect to graphite at %s', self._address)
7684

7785
def start(self, interval=60.0, prefix=''):
7886
t = _RegularPush(self, interval, prefix)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name = "prometheus_client",
6-
version = "0.0.13",
6+
version = "0.0.13c",
77
author = "Brian Brazil",
88
author_email = "brian.brazil@robustperception.io",
99
description = ("Python client for the Prometheus monitoring system."),

0 commit comments

Comments
 (0)
0