Skip to content

feat(metrics): expose Prometheus device gauges (#31) - #36

Open
2022117144 wants to merge 1 commit into
PiLab-Indonesia:mainfrom
2022117144:feat-prometheus-metrics-issue31
Open

2022117144 wants to merge 1 commit into
PiLab-Indonesia:mainfrom
2022117144:feat-prometheus-metrics-issue31

Conversation

@2022117144

Copy link
Copy Markdown

What changed

Closes #31.

  • Add an independently testable create_metrics_router(db) endpoint that publishes the current SQLite device totals, alive, and down states in Prometheus text exposition format.
  • Query a fresh database snapshot for each scrape. Use a private Prometheus registry to avoid process-global stale metrics, duplicated gauge registration, and leaking unrelated process metrics.
  • Wire the router into the existing FastAPI app.main API. Remove an unused Device import that prevented loading the module.
  • Add installation dependency prometheus-client, requirements file for the existing Dockerfile, user documentation, and 5 backend tests.
  • Keep all metrics aggregated; no device IP/name labels.

Local verification

On Windows / Python 3.10 (with prometheus-client 0.26.0):

  • python -m pytest -q -p no:cacheprovider tests/test_metrics.py: 5 passed (one third-party TestClient deprecation warning).
  • python -m flake8 app/metrics.py app/main.py tests/test_metrics.py --select=E9,F63,F7,F82 --show-source: exit 0.
  • git diff --check: exit 0.

Tests cover empty database, mixed online/offline counts, state updates reflected on the next scrape, independent repeated scrapes and hidden OpenAPI schema.

Limitations

An actual Prometheus deployment/scrape was not run; the tests use a local FastAPI TestClient and parse official Prometheus metric text. The repository's separate CI/environment-config issues #11 and #13 have separate pending PRs; this PR targets only metrics and introduces no Raspberry Pi hardware dependency.

AI-assisted implementation and verification; no cash bounty, hiring or payment acceptance is claimed.

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.

Add /metrics endpoint for Prometheus scraping

1 participant