Merge branch 'issue1255' into develop

This commit is contained in:
nicolargo 2018-05-06 22:16:40 +02:00
commit 4d0119ded4
3 changed files with 17 additions and 5 deletions

1
NEWS
View File

@ -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

View File

@ -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

View File

@ -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)