mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
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
This commit is contained in:
parent
3a246df544
commit
879e32f670
@ -19,7 +19,7 @@ from homeassistant import core as hacore
|
|||||||
from homeassistant.helpers import state as state_helper
|
from homeassistant.helpers import state as state_helper
|
||||||
from homeassistant.util.temperature import fahrenheit_to_celsius
|
from homeassistant.util.temperature import fahrenheit_to_celsius
|
||||||
|
|
||||||
REQUIREMENTS = ['prometheus_client==0.0.19']
|
REQUIREMENTS = ['prometheus_client==0.0.21']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -189,6 +189,14 @@ class Metrics(object):
|
|||||||
'electricity_usage_w', self.prometheus_client.Gauge,
|
'electricity_usage_w', self.prometheus_client.Gauge,
|
||||||
'Currently reported electricity draw in Watts',
|
'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')
|
unit = state.attributes.get('unit_of_measurement')
|
||||||
@ -212,12 +220,25 @@ class Metrics(object):
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the switch (0/1)',
|
'State of the switch (0/1)',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
value = state_helper.state_as_number(state)
|
value = state_helper.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
def _handle_zwave(self, state):
|
def _handle_zwave(self, state):
|
||||||
self._battery(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):
|
class PrometheusView(HomeAssistantView):
|
||||||
"""Handle Prometheus requests."""
|
"""Handle Prometheus requests."""
|
||||||
|
@ -560,7 +560,7 @@ pocketcasts==0.1
|
|||||||
proliphix==0.4.1
|
proliphix==0.4.1
|
||||||
|
|
||||||
# homeassistant.components.prometheus
|
# homeassistant.components.prometheus
|
||||||
prometheus_client==0.0.19
|
prometheus_client==0.0.21
|
||||||
|
|
||||||
# homeassistant.components.sensor.systemmonitor
|
# homeassistant.components.sensor.systemmonitor
|
||||||
psutil==5.4.1
|
psutil==5.4.1
|
||||||
|
@ -113,7 +113,7 @@ pilight==0.1.1
|
|||||||
pmsensor==0.4
|
pmsensor==0.4
|
||||||
|
|
||||||
# homeassistant.components.prometheus
|
# homeassistant.components.prometheus
|
||||||
prometheus_client==0.0.19
|
prometheus_client==0.0.21
|
||||||
|
|
||||||
# homeassistant.components.zwave
|
# homeassistant.components.zwave
|
||||||
pydispatcher==2.0.5
|
pydispatcher==2.0.5
|
||||||
|
@ -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
|
assert len(body) > 3 # At least two comment lines and a metric
|
||||||
for line in body:
|
for line in body:
|
||||||
if line:
|
if line:
|
||||||
assert line.startswith('# ') or line.startswith('process_')
|
assert line.startswith('# ') \
|
||||||
|
or line.startswith('process_') \
|
||||||
|
or line.startswith('python_info')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user