From 2c6be49e13560d7494ddf0f6a25b088342b5e704 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 6 May 2018 22:15:55 +0200 Subject: [PATCH] Add labels support to Promotheus exporter #1255 --- NEWS | 1 + conf/glances.conf | 4 ++++ glances/exports/glances_prometheus.py | 17 ++++++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index c82a4aba..c57506b5 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/conf/glances.conf b/conf/glances.conf index 21cbf027..65057946 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -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 diff --git a/glances/exports/glances_prometheus.py b/glances/exports/glances_prometheus.py index 2f89b476..af3d557b 100644 --- a/glances/exports/glances_prometheus.py +++ b/glances/exports/glances_prometheus.py @@ -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) -- GitLab