提交 2c6be49e 编写于 作者: N nicolargo

Add labels support to Promotheus exporter #1255

上级 8b6b0d3b
......@@ -26,6 +26,7 @@ Enhancements and new features:
* Huge refactor of the WebUI packaging thanks to @spike008t #1239
* Add time zone to the current time #1249
* Use https URLs for checking external IP #1253
* Add labels support to Promotheus exporter #1255
One more thing ! A new Grafana Dash is available with:
* Network interface variable
......
......@@ -444,6 +444,10 @@ prefix=G
host=localhost
port=9091
prefix=glances
# Labels will be added for all measurements
#labels=foo:bar,spam:eggs
# You can also use dynamic values
#labels=system:`uname -s`
[restful]
# Configuration for the --export RESTful option
......
......@@ -20,12 +20,11 @@
"""Prometheus interface class."""
import sys
from datetime import datetime
from numbers import Number
from glances.logger import logger
from glances.exports.glances_export import GlancesExport
from glances.compat import iteritems
from glances.compat import iteritems, listkeys
from prometheus_client import start_http_server, Gauge
......@@ -42,11 +41,12 @@ class Export(GlancesExport):
# Optionals configuration keys
self.prefix = 'glances'
self.labels = None
# Load the Prometheus configuration file section
self.export_enable = self.load_conf('prometheus',
mandatories=['host', 'port'],
options=['prefix'])
options=['prefix', 'labels'])
if not self.export_enable:
sys.exit(2)
......@@ -82,8 +82,15 @@ class Export(GlancesExport):
# See: https://prometheus.io/docs/practices/naming/
for c in ['.', '-', '/', ' ']:
metric_name = metric_name.replace(c, self.METRIC_SEPARATOR)
# Get the labels
labels = self.parse_tags(self.labels)
# Manage an internal dict between metric name and Gauge
if metric_name not in self._metric_dict:
self._metric_dict[metric_name] = Gauge(metric_name, k)
self._metric_dict[metric_name] = Gauge(metric_name, k,
labelnames=listkeys(labels))
# Write the value
self._metric_dict[metric_name].set(v)
if hasattr(self._metric_dict[metric_name], 'labels'):
# Add the labels (see issue #1255)
self._metric_dict[metric_name].labels(**labels).set(v)
else:
self._metric_dict[metric_name].set(v)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册