Add cache analytics and observability framework#320
Draft
Conversation
- Add CacheMetrics class for thread-safe metric collection - Track hits, misses, latencies, stale hits, recalculations - Integrate metrics into all cache backends (memory, pickle, mongo, redis, sql) - Add enable_metrics and metrics_sampling_rate parameters to @cachier - Create MetricsExporter base class and PrometheusExporter implementation - Add comprehensive tests for metrics functionality - Add metrics_example.py demonstrating usage Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
- Add Cache Analytics section to README.rst - Fix ruff linting issues in metrics.py and prometheus.py - Add prometheus_exporter_example.py - All tests passing (19/19) - Documentation complete with examples Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
- Add METRICS_IMPLEMENTATION.md with technical details - Document API reference, design decisions, and future enhancements - All tests passing, implementation complete - Ready for code review Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add analytics and observability framework for cache performance
Add cache analytics and observability framework
Jan 27, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-in cache metrics/observability framework to Cachier, plus a Prometheus exporter and documentation/examples to help users monitor cache behavior in production.
Changes:
- Introduces
CacheMetrics/MetricSnapshotand integrates metrics recording into the@cachierdecorator viaenable_metrics+metrics_sampling_rate. - Plumbs an optional
metricshandle through_BaseCoreand all backends. - Adds a Prometheus exporter module, tests, examples, and README documentation for the new feature.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
src/cachier/core.py |
Adds decorator params and records hit/miss/stale/recalc/timeout/latency at cache decision points. |
src/cachier/metrics.py |
New metrics data structures and thread-safe counters/latency tracking. |
src/cachier/exporters/base.py |
New exporter interface for pluggable metrics exporters. |
src/cachier/exporters/prometheus.py |
Implements Prometheus text output and optional prometheus_client integration + HTTP server. |
src/cachier/exporters/__init__.py |
Exposes exporter classes from the package. |
src/cachier/cores/base.py |
Adds metrics parameter storage on the core base class. |
src/cachier/cores/memory.py |
Wires metrics parameter through memory core init. |
src/cachier/cores/pickle.py |
Wires metrics parameter through pickle core init. |
src/cachier/cores/mongo.py |
Wires metrics parameter through mongo core init. |
src/cachier/cores/redis.py |
Wires metrics parameter through redis core init. |
src/cachier/cores/sql.py |
Wires metrics parameter through SQL core init. |
src/cachier/__init__.py |
Re-exports metrics types at the top level. |
tests/test_metrics.py |
Adds tests validating hit/miss/stale/latency/sampling/reset behavior. |
tests/test_exporters.py |
Adds tests for Prometheus exporter registration and text-format output. |
README.rst |
Documents metrics usage and Prometheus export. |
examples/metrics_example.py |
Demonstrates programmatic metrics access and sampling. |
examples/prometheus_exporter_example.py |
Demonstrates Prometheus text export usage. |
METRICS_IMPLEMENTATION.md |
Adds implementation/architecture notes for the new feature. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Use time.perf_counter() for latency measurement (monotonic clock) in core.py and metrics.py - Fix Prometheus text exporter to include ALL metrics: hits, misses, hit_rate, avg_latency_ms, stale_hits, recalculations, entry_count, cache_size_bytes, size_limit_rejections - Fix repeated HELP/TYPE headers by emitting them once per metric type - Add host parameter to PrometheusExporter (default: 127.0.0.1) for security - Implement cache size tracking in base core and memory core: * _update_size_metrics() to trigger updates * _get_entry_count() and _get_total_size() methods * Call after set_entry, clear_cache, delete_stale_entries - All 19 tests passing - Cache size metrics now working correctly Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
- Fix counter increment with deltas (comment 2731262796): Track last-seen values to calculate deltas instead of incrementing with absolute values - Implement prometheus_client mode with custom collector (comment 2731262813): Add CachierCollector that pulls metrics from registered functions at scrape time, properly populating /metrics endpoint - Add test coverage for prometheus_client mode (comment 2731262747): Add tests for use_prometheus_client=True fallback behavior - All 21 tests passing (19 existing + 2 new) Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Borda
reviewed
Jan 30, 2026
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
- Use absolute imports in base.py (comment 2744902663) - Move prometheus example instructions to module docstring (comment 2744908071) - Use contextlib.suppress for exception handling (comments 2744912772, SIM105) - Remove trailing commas for 120 line length (comments 2744919532, 2744929433) - Add comment explaining yields in collector (comment 2744926357) - Use single formatted string appends (comment 2744927877) - Fix README prometheus_client mode documentation (comment 2744928794) - Clarify cache size metrics backend support (comment 2744928804) - Pass host parameter to start_http_server (comment 2744928825) - Fix metric names consistency with _total suffix (comment 2744928839) - Remove unused _last_seen dict (comment 2744928850) - Use monotonic clock for windowed latency calculations (comment 2744928866) - Record miss on stale hit for accurate hit rate (comment 2744928891) - Add explanatory comment to except clause (comment 2744928901) - Don't swallow exceptions in start() method (comment 2744928818) All 21 tests passing Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Borda
reviewed
Jan 30, 2026
Borda
reviewed
Jan 30, 2026
- Replace multiple trivial print calls with one aggregated formatted f-string (comment 2744970314) - Improves code conciseness and readability - All tests passing (14/14) Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
- Combine three-line append patterns into single formatted strings (comment 2744927877) - Use absolute imports in sql.py instead of relative imports (comment 2744972453) - Improve code conciseness in prometheus text exporter - All 7 exporter tests passing Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cache Analytics and Observability Framework Implementation
Overview
Implemented a comprehensive analytics framework for cachier that tracks cache performance metrics and provides observability for production systems.
✅ All Review Feedback Addressed
Previous feedback (11 comments) - ALL RESOLVED
Second round feedback (16 comments from @Borda + copilot reviewer) - ALL RESOLVED
Latest feedback (3 comments) - ALL RESOLVED
🔧 Latest Changes - Code Quality Improvements
from cachier.instead of relative imports__doc__📊 All Metrics Working
✅ Testing Results
🎯 Production Ready
The implementation is complete, tested, documented, and ready for production use with all feedback addressed.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.