Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Non-ASCII bearer tokens trigger a TypeError during comparison, causing /metrics to return 500 instead of the required 401.
2 open issues
What changed in this PR
Adds opt-in Prometheus monitoring on the existing listener, addressing #278 without requiring an admin session. The endpoint also works in --core-only mode.
Changes:
- Adds bearer-token configuration and authentication for
/metrics. - Exports allowlisted service and device state, recovery counts, and local TLS certificate expiry.
- Adds configuration and endpoint tests, setup documentation, and example settings.
| File | Description |
|---|---|
tests/test_metrics.py |
Tests authentication, rendering, core-only operation, and metrics values. |
tests/test_config.py |
Tests metrics defaults, validation, and token-file paths. |
src/roborock_local_server/server.py |
Registers the endpoint and builds metrics snapshots. |
src/roborock_local_server/metrics.py |
Adds bearer-token helpers and Prometheus text rendering. |
src/roborock_local_server/config.py |
Adds metrics settings and token-file path resolution. |
src/roborock_local_server/certs.py |
Adds certificate expiry extraction. |
mkdocs.yml |
Adds metrics documentation to navigation. |
docs/metrics.md |
Documents configuration, scraping, alerts, and limitations. |
docs/index.md |
Links to the metrics guide. |
config.example.toml |
Adds commented metrics configuration examples. |
Review effort: Balanced
Runtime vacuum and service health lived only on admin/UI JSON routes that require the admin credential and expose secrets such as local_key. With --core-only those routes are off, so monitoring could not scrape stack state without storing the admin password. Add an optional GET /metrics on the existing HTTPS listener: disabled by default, bearer-token auth with constant-time comparison, snapshot built from health_snapshot and the merged vacuum list without sensitive fields. Works independent of the admin UI (including core-only). Document scrape setup and alert examples; cover config validation and the endpoint in tests. Refs Python-roborock#278
849b8dc to
0f845b7
Compare
|
Thanks a lot for picking this up, @lucavb! It's great to see #278 implemented. :) I can test this on a Qrevo S5A (roborock.vacuum.a187) with listener_mode = "external_tls" and Prometheus scraping via bearer_token_file. My local setup already uses the same config keys and metric names as this PR, so I can switch to the branch directly and report back. One small thing: for the per-device families, render_prometheus prints all HELP/TYPE lines first and then interleaves the samples per device. The text exposition format expects all lines of a metric as one group (HELP/TYPE, then its samples). Prometheus itself is lenient here, as your live scrape shows, but stricter parsers might not be. Emitting one family at a time (loop over the devices once per metric) would fix it. Optional, feel free to ignore: if bearer_token_file is missing or empty, failing at startup might be clearer than a 401 on every scrape. A |



What does this PR do?
Adds an opt-in
GET /metricsendpoint (Prometheus text format) on the existing HTTPS listener, protected by a bearer token. Metrics are derived from existing in-process state only—no admin session, nolocal_keyor session secrets in the scrape payload. Works with--core-only.Closes #278.
Before you open this PR (optional, but very helpful)
PRs I reviewed:
Proof that it works
Tested on:
roborock.vacuum.a10)1.3.0-rc2+849b8dc(this branch)job=roborock-local-server,instance=roborock-local.direct.sunbury.xyz:555)What I did and what happened:
[metrics]with bearer token in config.Authorization: Bearer …on/metrics.{job="roborock-local-server", __name__=~"roborock_.*"}returned 14 series, includingroborock_build_info{version="1.3.0-rc2"},roborock_device_connected,roborock_service_upfor enabled services, MQTT connection count, and per-device timestamps/labels.uv run pytest— 893 passed locally.Screenshots (required for any UI change)
No UI changes.
Test plan
tests/test_metrics.py(auth, disabled catchall, core-only, TLS expiry, recovery gauge, label escaping)tests/test_config.pymetrics validationuv run pytest