Skip to content

Commit

Permalink
Add Min and Event Count Metrics To Prometheus (#10530)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Brent Hughes authored and MartinHjelmare committed Dec 3, 2017
1 parent 3a246df commit 879e32f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
27 changes: 24 additions & 3 deletions homeassistant/components/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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__)

Expand Down Expand Up @@ -189,6 +189,14 @@ def _handle_sensor(self, state):
'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')
Expand All @@ -212,12 +220,25 @@ def _handle_switch(self, state):
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."""
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/components/test_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 879e32f

Please sign in to comment.