jomjol / AI-on-the-edge-device

Easy to use device for connecting "old" measuring units (water, power, gas, ...) to the digital world
https://jomjol.github.io/AI-on-the-edge-device-docs/
5.26k stars 580 forks source link

add Prometheus/OpenMetrics exporter #3081

Closed henrythasler closed 1 month ago

henrythasler commented 1 month ago

Adds a new http-endpoint /metrics that provides metrics according to Prometheus (similar to OpenMetrics) text format. Fixes #1345. The metrics mostly follow the design of @hansmi's Prometheus Exporter.

The main metric is probably the current value (ai_on_the_edge_device_flow_value) of the main sequence. Multiple sequences are supported via tags. This metric only provides valid readout (no error). Besides that, a bunch of device properties are exposed.

I'm also creating this PR to get some early feedback and advice from the community on what could be improved.

Simple Prometheus Scrape Config

  - job_name: watermeter
    scrape_interval: 300s
    metrics_path: /metrics
    static_configs:
      - targets: ['192.168.178.23']

Sample response

$ curl -v http://192.168.178.23/metrics
*   Trying 192.168.178.23:80...
* Connected to 192.168.178.23 (192.168.178.23) port 80 (#0)
> GET /metrics HTTP/1.1
> Host: 192.168.178.23
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 1043
< Access-Control-Allow-Origin: *
< 
# HELP ai_on_the_edge_device_flow_value current value of meter readout
# TYPE ai_on_the_edge_device_flow_value gauge
ai_on_the_edge_device_flow_value{sequence="main"} 239.2208
# HELP ai_on_the_edge_device_cpu_temperature_celsius current cpu temperature in celsius
# TYPE ai_on_the_edge_device_cpu_temperature_celsius gauge
ai_on_the_edge_device_cpu_temperature_celsius 41
# HELP ai_on_the_edge_device_rssi_dbm current WiFi signal strength in dBm
# TYPE ai_on_the_edge_device_rssi_dbm gauge
ai_on_the_edge_device_rssi_dbm -63
# HELP ai_on_the_edge_device_memory_heap_free_bytes available heap memory
# TYPE ai_on_the_edge_device_memory_heap_free_bytes gauge
ai_on_the_edge_device_memory_heap_free_bytes 714083
# HELP ai_on_the_edge_device_uptime_seconds device uptime in seconds
# TYPE ai_on_the_edge_device_uptime_seconds gauge
ai_on_the_edge_device_uptime_seconds 6760
# HELP ai_on_the_edge_device_rounds_total data aquisition rounds since device startup
# TYPE ai_on_the_edge_device_rounds_total counter
ai_on_the_edge_device_rounds_total 8
* Connection #0 to host 192.168.178.23 left intact

Resource Impact

Resource total [bytes] current [bytes] this PR [bytes] change [bytes]
RAM 327680 51040 51040 0
Flash 1945600 1496605 1499029 +2424
henrythasler commented 1 month ago

@hansmi, I have refactored the PR quite a bit according to your your suggestions. Feel free to review again.

hansmi commented 1 month ago

@henrythasler, I'll be taking a look at the code changes as a separate review. Generally I'd suggest to break out the "fix homeassistant service discovery" changes to a separate pull request.

henrythasler commented 1 month ago

@hansmi, you are correct. I will create a new PR for the homeassistant change.

caco3 commented 1 month ago

Please also extend the documentation at https://github.com/jomjol/AI-on-the-edge-device-docs/blob/main/docs/REST-API.md

henrythasler commented 1 month ago

Please also extend the documentation at https://github.com/jomjol/AI-on-the-edge-device-docs/blob/main/docs/REST-API.md

Done: https://github.com/jomjol/AI-on-the-edge-device-docs/pull/55