Add labels support to Promotheus exporter #1255

pull/1263/head
nicolargo 2018-05-06 22:15:55 +02:00
parent 8b6b0d3bd0
commit 2c6be49e13
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 * Huge refactor of the WebUI packaging thanks to @spike008t #1239
* Add time zone to the current time #1249 * Add time zone to the current time #1249
* Use https URLs for checking external IP #1253 * 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: One more thing ! A new Grafana Dash is available with:
* Network interface variable * Network interface variable

View File

@ -444,6 +444,10 @@ prefix=G
host=localhost host=localhost
port=9091 port=9091
prefix=glances 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] [restful]
# Configuration for the --export RESTful option # Configuration for the --export RESTful option

View File

@ -20,12 +20,11 @@
"""Prometheus interface class.""" """Prometheus interface class."""
import sys import sys
from datetime import datetime
from numbers import Number from numbers import Number
from glances.logger import logger from glances.logger import logger
from glances.exports.glances_export import GlancesExport 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 from prometheus_client import start_http_server, Gauge
@ -42,11 +41,12 @@ class Export(GlancesExport):
# Optionals configuration keys # Optionals configuration keys
self.prefix = 'glances' self.prefix = 'glances'
self.labels = None
# Load the Prometheus configuration file section # Load the Prometheus configuration file section
self.export_enable = self.load_conf('prometheus', self.export_enable = self.load_conf('prometheus',
mandatories=['host', 'port'], mandatories=['host', 'port'],
options=['prefix']) options=['prefix', 'labels'])
if not self.export_enable: if not self.export_enable:
sys.exit(2) sys.exit(2)
@ -82,8 +82,15 @@ class Export(GlancesExport):
# See: https://prometheus.io/docs/practices/naming/ # See: https://prometheus.io/docs/practices/naming/
for c in ['.', '-', '/', ' ']: for c in ['.', '-', '/', ' ']:
metric_name = metric_name.replace(c, self.METRIC_SEPARATOR) 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 # Manage an internal dict between metric name and Gauge
if metric_name not in self._metric_dict: 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 # Write the value
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) self._metric_dict[metric_name].set(v)