Encode prometheus metric names per the prom spec (#26639)

Referencing issue #26418.

Prometheus metric names can only contain chars a-zA-Z0-9, : and _
(https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels).

HA currently generates invalid prometheus names, e.g. if the unit for a
sensor is a non-ASCII character containing  ° or μ. To resolve, we need
to sanitize the name before creating, replacing non-valid characters
with a valid representation. In this case, I've used
"u{unicode-hex-code}".

Also updated the test case to make sure that the ° case is handled.
This commit is contained in:
Andrew Rowson authored and Paulus Schoutsen committed 2019-09-19 14:28:30 -07:00
1 parent 88154074c1
commit 120c8bad50
2 files changed
+26 -1

No files matched your search

+11
View File
@@ -41,6 +41,11 @@ async def prometheus_client(loop, hass, hass_client):
sensor3.entity_id = "sensor.electricity_price"
await sensor3.async_update_ha_state()
sensor4 = DemoSensor("Wind Direction", 25, None, "°", None)
sensor4.hass = hass
sensor4.entity_id = "sensor.wind_direction"
await sensor4.async_update_ha_state()
return await hass_client()
@@ -103,3 +108,9 @@ def test_view(prometheus_client): # pylint: disable=redefined-outer-name
'entity="sensor.electricity_price",'
'friendly_name="Electricity price"} 0.123' in body
)
assert (
'sensor_unit_u0xb0{domain="sensor",'
'entity="sensor.wind_direction",'
'friendly_name="Wind Direction"} 25.0' in body
)