mirror of https://github.com/nicolargo/glances.git
Add unit test for Prometheus exporter aand refactor the code to not use the API (not useful in this case because stats instance already exist)
This commit is contained in:
parent
d03295a977
commit
2254f899b6
|
|
@ -13,7 +13,6 @@ from numbers import Number
|
|||
|
||||
from prometheus_client import Gauge, start_http_server
|
||||
|
||||
from glances.api import GlancesStats
|
||||
from glances.exports.export import GlancesExport
|
||||
from glances.globals import listkeys
|
||||
from glances.logger import logger
|
||||
|
|
@ -44,7 +43,8 @@ class Export(GlancesExport):
|
|||
# Perhaps a better method is possible...
|
||||
self._metric_dict = {}
|
||||
|
||||
self._stats = GlancesStats()
|
||||
# Keys name (compute in update() method)
|
||||
self.keys_name = {}
|
||||
|
||||
# Init the Prometheus Exporter
|
||||
self.init()
|
||||
|
|
@ -59,6 +59,10 @@ class Export(GlancesExport):
|
|||
else:
|
||||
logger.info(f"Start Prometheus exporter on {self.host}:{self.port}")
|
||||
|
||||
def update(self, stats):
|
||||
self.keys_name = {k: stats.get_plugin(k).get_key() for k in stats.getPluginsList()}
|
||||
super().update(stats)
|
||||
|
||||
def export(self, name, columns, points):
|
||||
"""Write the points to the Prometheus exporter using Gauge."""
|
||||
logger.debug(f"Export {name} stats to Prometheus exporter")
|
||||
|
|
@ -66,8 +70,6 @@ class Export(GlancesExport):
|
|||
# Remove non number stats and convert all to float (for Boolean)
|
||||
data = {str(k): float(v) for k, v in zip(columns, points) if isinstance(v, Number)}
|
||||
|
||||
key_name = self._stats.get_plugin(name).get_key()
|
||||
|
||||
# Write metrics to the Prometheus exporter
|
||||
for metric, value in data.items():
|
||||
labels = self.labels
|
||||
|
|
@ -75,7 +77,7 @@ class Export(GlancesExport):
|
|||
try:
|
||||
obj, stat = metric.split('.')
|
||||
metric_name += stat
|
||||
labels += f",{key_name}:{obj}"
|
||||
labels += f",{self.keys_name.get(name)}:{obj}"
|
||||
except ValueError:
|
||||
metric_name += metric
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
# Run glances with export to Prometheus, stopping after 10 writes
|
||||
# This will run synchronously now since we're using --stop-after
|
||||
echo "Glances to export system stats to Prometheus"
|
||||
./venv/bin/python -m glances --config ./conf/glances.conf --export prometheus --stop-after 10 --quiet &
|
||||
# Get the PID of the last background command
|
||||
GLANCES_PID=$!
|
||||
|
||||
# Wait for a few seconds to let glances start
|
||||
echo "Please wait for a few seconds..."
|
||||
sleep 6
|
||||
|
||||
# Check if we can access the Prometheus metrics endpoint
|
||||
echo "Checking Prometheus metrics endpoint..."
|
||||
curl http://localhost:9091/metrics
|
||||
|
||||
# Kill the glances process if it's still running
|
||||
if ps -p $GLANCES_PID > /dev/null; then
|
||||
kill $GLANCES_PID
|
||||
fi
|
||||
|
||||
echo "Script completed successfully!"
|
||||
Loading…
Reference in New Issue