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

Prometheus metrics naming based on device_class and unit_of_measurement #24103

Merged
merged 9 commits into from Aug 10, 2019

Conversation

@perosb
Copy link
Contributor

commented May 25, 2019

Breaking Change:

The naming of metrics has been changed and doesn't use the entity_id anymore.
The user will probably have to change any consumers of the prometheus data to use the new names.
The user can now also use the new component_config_glob to remap the metrics any way the user sees fit.

Description:

Current version of prometheus creates way to many metrics based on entity id name.
This will pollute and fill database and isn't how metrics in promethues should work.

This PR will try to use the device_class first, and secondly the unit of measurement to create the name of the metric.

This means all temperature goes into <namespace>_temperature_c metric.

Also adds support for glob filtering of devices to change metric.
This code is similar to what's in influxdb component.

Updates prometheus_client to 0.7.1

Related issue (if applicable): fixes #

Pull request with documentation for home-assistant.io (if applicable):
home-assistant/home-assistant.io#9518

Example entry for configuration.yaml (if applicable):

prometheus:
  namespace: hass
  component_config_glob:
    # override metric for RFLink devices
    sensor.*_hum:
      override_metric: humidity_percent
    sensor.*_temp:
      override_metric: temperature_c
    sensor.*_bat:
      override_metric: battery_percent
  filter:
    include_domains:
      - sensor
      - lock
# HELP hass_temperature_c Sensor data measured in °C
# TYPE hass_temperature_c gauge
hass_temperature_c{domain="sensor",entity="sensor.temperature1",friendly_name="Brasrummet"} 25.0
hass_temperature_c{domain="sensor",entity="sensor.temperature2",friendly_name="Frysen"} -21.2
hass_temperature_c{domain="sensor",entity="sensor.sensor3966344_temp",friendly_name="Garaget"} 16.4
# HELP hass_humidity_percent Sensor data measured in %
# TYPE hass_humidity_percent gauge
hass_humidity_percent{domain="sensor",entity="sensor.sensor3966344_hum",friendly_name="Garaget"} 63.4
hass_humidity_percent{domain="sensor",entity="sensor.sensor3967017_hum",friendly_name="Vinden"} 52.11
# HELP hass_pressure_hpa Sensor data measured in hPa
# TYPE hass_pressure_hpa gauge
hass_pressure_hpa{domain="sensor",entity="sensor.sensor3967017_pres",friendly_name="Vinden"} 982.66
# HELP hass_sensor_unit_days Sensor data measured in days
# TYPE hass_sensor_unit_days gauge
hass_sensor_unit_days{domain="sensor",entity="sensor.google_wifi_uptime",friendly_name="google_wifi_uptime"} 35.28
# HELP hass_sensor_unit_kwh Sensor data measured in kWh
# TYPE hass_sensor_unit_kwh gauge
hass_sensor_unit_kwh{domain="sensor",entity="sensor.gullvi_energy",friendly_name="Gullvi Energy"} 123.82
hass_sensor_unit_kwh{domain="sensor",entity="sensor.hoverboardladdare_energy",friendly_name="Hoverboardladdare Energy"} 36.86

Based on discussion in #14508

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@perosb perosb force-pushed the perosb:prometheus branch 3 times, most recently from 3af3a33 to 7f98044 May 25, 2019

@perosb perosb referenced this pull request May 25, 2019
2 of 2 tasks complete

@perosb perosb changed the title Prometheus metrics naming and extraction Prometheus metrics naming based on device_class and unit_of_measurement May 25, 2019

@perosb perosb marked this pull request as ready for review May 25, 2019

- Change how we extract the metrics for sensors
- Add component filtering as seen in influxdb
- Add metric override as seen in influxdb
- Add more unit tests with actual device data

@perosb perosb force-pushed the perosb:prometheus branch from a0b3e17 to a35a100 May 28, 2019

@MartinHjelmare MartinHjelmare added this to Review in progress in Dev Jul 23, 2019

@MartinHjelmare

This comment has been minimized.

Copy link
Member

commented Aug 8, 2019

@perosb are you planning to finish here?

@perosb

This comment has been minimized.

Copy link
Contributor Author

commented Aug 8, 2019

Sorry, yes, I just need to find the time to make your suggested changes.

@MartinHjelmare

This comment has been minimized.

Copy link
Member

commented Aug 8, 2019

We're trying to decrease our open PR buffer. Please close this PR and you can reopen it when you're ready to finish. Then we can continue without loosing thread history. Thanks!

perosb added 6 commits Aug 10, 2019
@MartinHjelmare
Copy link
Member

left a comment

Looks good!

homeassistant/components/prometheus/__init__.py Outdated Show resolved Hide resolved
tests/components/prometheus/test_init.py Show resolved Hide resolved

Dev automation moved this from Review in progress to Reviewer approved Aug 10, 2019

@MartinHjelmare
Copy link
Member

left a comment

Great!

@MartinHjelmare

This comment has been minimized.

Copy link
Member

commented Aug 10, 2019

Please update the breaking change paragraph to also say what the user needs to do to cope with the breaking change.

@perosb

This comment has been minimized.

Copy link
Contributor Author

commented Aug 10, 2019

Please update the breaking change paragraph to also say what the user needs to do to cope with the breaking change.

@MartinHjelmare
Done!

Tack ;)

@MartinHjelmare

This comment has been minimized.

Copy link
Member

commented Aug 10, 2019

Thanks! I tweaked the language a bit. We will copy the paragraph directly to the release notes.

@MartinHjelmare MartinHjelmare merged commit e9705af into home-assistant:dev Aug 10, 2019

11 checks passed

CI Build #20190810.21 succeeded
Details
CI (FullCheck Mypy) FullCheck Mypy succeeded
Details
CI (FullCheck Pylint) FullCheck Pylint succeeded
Details
CI (Overview CheckFormat) Overview CheckFormat succeeded
Details
CI (Overview Lint) Overview Lint succeeded
Details
CI (Overview Validate) Overview Validate succeeded
Details
CI (Tests PyTest Python36) Tests PyTest Python36 succeeded
Details
CI (Tests PyTest Python37) Tests PyTest Python37 succeeded
Details
cla-bot Everyone involved has signed the CLA
codecov/patch Coverage not affected when comparing f30e54f...9bb15a1
Details
codecov/project 94.03% (target 90%)
Details

Dev automation moved this from Reviewer approved to Done Aug 10, 2019

@perosb perosb deleted the perosb:prometheus branch Aug 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
4 participants
You can’t perform that action at this time.