diff --git a/app.py b/app.py index 21829ad..def6d09 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import os import requests import time +import re from flask import render_template, Flask, make_response, Response @@ -70,13 +71,20 @@ def metrics(): else: attrib["currentValue"] = attrib["currentValue"] - # Sanitise the device name as it will appear in the label - device_name = device_details['label'].lower().replace(' ','_').replace('-','_') - # Sanitise the metric name - metric_name = attrib['name'].lower().replace(' ','_').replace('-','_') + # Sanitize to allow Prometheus Ingestion + device_name = sanitize(device_details['name']) + device_label = sanitize(device_details['label']) + device_human_label = device_details['label'] + device_type = sanitize(device_details['type']) + device_id = sanitize(device_details['id']) + metric_name = sanitize(attrib['name']) # Create the dict that holds the data device_attributes.append({ "device_name": f"{device_name}", + "device_label": f"{device_label}", + "device_human_label": f"{device_human_label}", + "device_type": f"{device_type}", + "device_id": f"{device_id}", "metric_name": f"{metric_name}", "metric_value": f"{attrib['currentValue']}", "metric_timestamp": time.time()}) @@ -88,5 +96,5 @@ def metrics(): response.mimetype = "text/plain" return response - - +def sanitize(inputValue): + return re.sub('[^a-z0-9]+', '_', inputValue.lower()) diff --git a/templates/base.txt b/templates/base.txt index 58a99f9..5876661 100644 --- a/templates/base.txt +++ b/templates/base.txt @@ -1,3 +1,3 @@ {% for device in device_details %} -{{ device["metric_name"] }}{device_name="{{device['device_name']}}"} {{ device["metric_value"] }} +{{ device["metric_name"] }}{device_name="{{device['device_name']}}",device_human_label="{{device['device_human_label']}}",device_label="{{device['device_label']}}",device_type="{{device['device_type']}}",device_id="{{device['device_id']}}"} {{ device["metric_value"] }} {% endfor %}