Skip to content

feat(metrics): opt-in Prometheus /metrics endpoint - #319

Open
lucavb wants to merge 1 commit into
Python-roborock:mainfrom
lucavb:feat/prometheus-metrics-endpoint
Open

lucavb wants to merge 1 commit into
Python-roborock:mainfrom
lucavb:feat/prometheus-metrics-endpoint

Conversation

@lucavb

@lucavb lucavb commented Dec 1, 2026

Copy link
Copy Markdown

What does this PR do?

Adds an opt-in GET /metrics endpoint (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, no local_key or session secrets in the scrape payload. Works with --core-only.

Closes #278.

Before you open this PR (optional, but very helpful)

  • I checked for duplicates. No other open PR implements Prometheus metrics.
  • I reviewed 1–2 other open PRs and tested whether they work on my vacuum.

PRs I reviewed:

  • (not done yet)

Proof that it works

Tested on:

  • Vacuum model: Roborock S6 MaxV (roborock.vacuum.a10)
  • Firmware version: (not captured in notes—happy to add if needed)
  • Server version / commit: 1.3.0-rc2 + 849b8dc (this branch)
  • Install method: container on k3s-home, scraped via Thanos/Prometheus (job=roborock-local-server, instance=roborock-local.direct.sunbury.xyz:555)

What I did and what happened:

  1. Enabled [metrics] with bearer token in config.
  2. Configured Prometheus/Thanos scrape with Authorization: Bearer … on /metrics.
  3. Query {job="roborock-local-server", __name__=~"roborock_.*"} returned 14 series, including roborock_build_info{version="1.3.0-rc2"}, roborock_device_connected, roborock_service_up for enabled services, MQTT connection count, and per-device timestamps/labels.
  4. 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.py metrics validation
  • Full uv run pytest
  • Live Prometheus scrape against production stack

Copilot AI balanced review requested due to automatic review settings December 1, 2026 14:07
@lucavb

lucavb commented Dec 1, 2026

Copy link
Copy Markdown
Author
Screenshot 2026-12-01 at 14 48 29

right now it looks like this with it running :)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/roborock_local_server/metrics.py Resolved
Comment thread tests/test_metrics.py Resolved
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
@lucavb
lucavb force-pushed the feat/prometheus-metrics-endpoint branch from 849b8dc to 0f845b7 Compare December 1, 2026 14:44
@lucavb
lucavb requested a balanced review from Copilot December 1, 2026 14:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the opt-in, bearer-token-protected metrics endpoint.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@rottifant

Copy link
Copy Markdown

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 WWW-Authenticate: Bearer header on the 401 would also be nice.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional Prometheus /metrics endpoint

3 participants