feat: Prometheus Metrics Integration - #37
Conversation
- Add prometheus.rs module with comprehensive metrics recording - Implement InstrumentedBackend wrapper for automatic LLM metrics - Add /metrics endpoint to dashboard server - Record tokens, cost, latency, cache, rate limits, errors, and active requests - Support all major LLM models with pricing (GPT-4, Claude 3/3.5, GPT-3.5) - Add 10 unit tests (8 prometheus + 2 instrumented) - Create comprehensive Prometheus metrics documentation guide Metrics exposed: - llm_tokens_input_total, llm_tokens_output_total, llm_tokens_total - llm_cost_usd_total (stored as cents for precision) - llm_latency_ms (histogram) - llm_cache_hits_total, llm_cache_misses_total - llm_rate_limited_total - llm_errors_total (by error_type) - llm_active_requests (gauge) All tests pass (47 total: 15 monitoring + 32 providers)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Track active requests | ||
| inc_active_requests(&route, &model); | ||
|
|
||
| // Execute the actual streaming inference | ||
| let stream = self |
There was a problem hiding this comment.
Fix active gauge leak on streaming errors
The streaming path increments llm_active_requests before calling infer_stream, but the ? on the await returns immediately on any backend error and the only decrement lives inside the stream wrapper. When a streaming call fails to start (e.g., network failure or provider error), the gauge is left permanently incremented, inflating active-request metrics for the lifetime of the process. The decrement needs to run on the error path as well.
Useful? React with 👍 / 👎.
Overview
Implements comprehensive Prometheus metrics integration for production-grade observability of LLM operations.
What's New
Core Features
Prometheus Module ()
InstrumentedBackend ()
LLMBackendDashboard /metrics Endpoint ()
Documentation
docs/prometheus-metrics.mdMetrics Exposed
Usage
Testing
cargo fmtFiles Changed
crates/monitoring/Cargo.toml- Add metrics dependenciescrates/monitoring/src/prometheus.rs- NEW Prometheus modulecrates/monitoring/src/lib.rs- Export prometheus modulecrates/providers/Cargo.toml- Add monitoring dependencycrates/providers/src/instrumented.rs- NEW Instrumented wrappercrates/providers/src/lib.rs- Export InstrumentedBackendcrates/dashboard/Cargo.toml- Add metrics-exporter dependencycrates/dashboard/src/state.rs- Add PrometheusHandlecrates/dashboard/src/server.rs- Initialize Prometheus + routecrates/dashboard/src/handlers.rs- Add /metrics handlerdocs/prometheus-metrics.md- NEW Complete guideNext Steps (Phase 2)
This PR completes Phase 1: Prometheus Metrics from the roadmap.
Next priorities:
Performance Impact
Breaking Changes
None. This is purely additive.
Closes roadmap Phase 1: Prometheus Metrics Integration