Skip to content

feat: Add OSS Prometheus metrics module#3411

Open
mgazza wants to merge 3 commits intomainfrom
feature/oss-metrics
Open

feat: Add OSS Prometheus metrics module#3411
mgazza wants to merge 3 commits intomainfrom
feature/oss-metrics

Conversation

@mgazza
Copy link
Collaborator

@mgazza mgazza commented Feb 22, 2026

Summary

  • Adds predbat_metrics.py — a new OSS metrics module where each component emits its own Prometheus metrics at the point of origin
  • prometheus_client is optional: when absent, all metric operations silently no-op via _NoOpMetric stub
  • Instruments all 5 API clients (GivEnergy, Octopus, Solcast/Forecast.Solar, Fox ESS, Solis) with record_api_call() tracking success/failure with reason labels
  • Fixes solar calibration bug: pv_calibration() computed worst_day_scaling, best_day_scaling, total_adjustment as local variables that were never stored on self — now persisted and emitted as metrics
  • Adds plan duration histogram, plan validity gauge, IOG latency histogram, register write counter, energy totals, data age, battery state, cost/savings snapshots
  • Registers /metrics HTTP endpoint on the existing web server (only when prometheus_client is installed)
  • 31 metrics total, all names match the existing SaaS plugin so Grafana dashboards/alerts work unchanged

Test plan

  • python -c "import predbat_metrics" succeeds without prometheus_client installed
  • metrics().api_requests_total.labels(service='test').inc() silently does nothing without prometheus_client
  • With prometheus_client: curl localhost:5052/metrics returns valid Prometheus exposition format
  • predbat_pv_scaling_worst now appears in metrics (was always missing before due to the local variable bug)
  • predbat_api_failures_total{service="givenergy",reason="auth_error"} shows correct classification
  • Existing Grafana dashboards work without modification (metric names unchanged)

🤖 Generated with Claude Code

mgazza and others added 3 commits February 19, 2026 15:58
Add comprehensive Google-style docstrings to all Python source files in
the predbat codebase. Every source file now has a module-level docstring
describing its purpose, and all major classes have class-level docstrings
documenting their role, key attributes, and relationships.

This enables pydoc/pdoc auto-documentation and improves IDE tooltip
support for developers working with the codebase.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move metrics emission into the OSS codebase so each component emits
its own data at the point of origin. prometheus_client is optional —
when absent every operation silently no-ops.

- Add predbat_metrics.py with conditional import, no-op stubs,
  singleton registry (31 metrics), record_api_call() helper,
  and /metrics HTTP handler
- Instrument API clients (gecloud, octopus, solcast, fox, solis)
  with per-call success/failure tracking and reason labels
- Fix solar calibration bug: store worst/best/total scaling on self
  (were local variables, never accessible outside pv_calibration())
- Add plan duration histogram + validity gauge
- Add IOG latency histogram + register write counter
- Add energy totals + data age gauge in fetch
- Emit snapshot metrics (battery, cost, config) in update loop

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants