From 879e32f670e2354813667173a328dca6a79a8f5c Mon Sep 17 00:00:00 2001 From: Brent Hughes Date: Sun, 3 Dec 2017 16:39:54 -0600 Subject: [PATCH] Add Min and Event Count Metrics To Prometheus (#10530) * Added min and Events sensor types to prometheus * Updated prometheus client and fixed invalid swith state * Added metric to count number of times an automation is triggered * Removed assumption that may not apply to everybody * Fixed tests * Updated requirements_test_all * Fixed unit tests --- homeassistant/components/prometheus.py | 27 +++++++++++++++++++++++--- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/test_prometheus.py | 4 +++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/prometheus.py b/homeassistant/components/prometheus.py index 0396cafd4ff..0195021829b 100644 --- a/homeassistant/components/prometheus.py +++ b/homeassistant/components/prometheus.py @@ -19,7 +19,7 @@ from homeassistant import core as hacore from homeassistant.helpers import state as state_helper from homeassistant.util.temperature import fahrenheit_to_celsius -REQUIREMENTS = ['prometheus_client==0.0.19'] +REQUIREMENTS = ['prometheus_client==0.0.21'] _LOGGER = logging.getLogger(__name__) @@ -189,6 +189,14 @@ class Metrics(object): 'electricity_usage_w', self.prometheus_client.Gauge, 'Currently reported electricity draw in Watts', ), + 'min': ( + 'sensor_min', self.prometheus_client.Gauge, + 'Time in minutes reported by a sensor' + ), + 'Events': ( + 'sensor_event_count', self.prometheus_client.Gauge, + 'Number of events for a sensor' + ), } unit = state.attributes.get('unit_of_measurement') @@ -212,12 +220,25 @@ class Metrics(object): self.prometheus_client.Gauge, 'State of the switch (0/1)', ) - value = state_helper.state_as_number(state) - metric.labels(**self._labels(state)).set(value) + + try: + value = state_helper.state_as_number(state) + metric.labels(**self._labels(state)).set(value) + except ValueError: + pass def _handle_zwave(self, state): self._battery(state) + def _handle_automation(self, state): + metric = self._metric( + 'automation_triggered_count', + self.prometheus_client.Counter, + 'Count of times an automation has been triggered', + ) + + metric.labels(**self._labels(state)).inc() + class PrometheusView(HomeAssistantView): """Handle Prometheus requests.""" diff --git a/requirements_all.txt b/requirements_all.txt index 218f1796efd..309da835c3b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -560,7 +560,7 @@ pocketcasts==0.1 proliphix==0.4.1 # homeassistant.components.prometheus -prometheus_client==0.0.19 +prometheus_client==0.0.21 # homeassistant.components.sensor.systemmonitor psutil==5.4.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b02d80ad0e3..7a33e9b4dd0 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -113,7 +113,7 @@ pilight==0.1.1 pmsensor==0.4 # homeassistant.components.prometheus -prometheus_client==0.0.19 +prometheus_client==0.0.21 # homeassistant.components.zwave pydispatcher==2.0.5 diff --git a/tests/components/test_prometheus.py b/tests/components/test_prometheus.py index dd8cbfe55e0..052292b015d 100644 --- a/tests/components/test_prometheus.py +++ b/tests/components/test_prometheus.py @@ -30,4 +30,6 @@ def test_view(prometheus_client): # pylint: disable=redefined-outer-name assert len(body) > 3 # At least two comment lines and a metric for line in body: if line: - assert line.startswith('# ') or line.startswith('process_') + assert line.startswith('# ') \ + or line.startswith('process_') \ + or line.startswith('python_info')