Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Min and Event Count Metrics To Prometheus #10530

Merged
merged 7 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -557,7 +557,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')