Commit 4602401
Add cache analytics and observability framework (#320)
* Initial plan
* Implement cache analytics and observability framework
- 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 metrics documentation and fix linting issues
- 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 comprehensive implementation documentation
- 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>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add `assert` to ensure `start_time` is not `None` before latency recording
* Update README.rst
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update examples/metrics_example.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update src/cachier/metrics.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Address PR review feedback - complete implementation
- 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>
* Address remaining PR review feedback
- 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>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Address PR review feedback - code quality improvements
- 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>
* Refactor metrics example to use single formatted print statement
- 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>
* Consolidate prometheus metric headers and fix imports
- 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>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Align S3 backend with metrics framework
- Add metrics parameter to _S3Core.__init__()
- Pass metrics to S3 core in cachier decorator
- Add metrics import to s3.py
- Update S3 core docstring to document metrics parameter
- Ensures S3 backend supports metrics like all other backends
Addresses comment 4010458432: aligns with latest codebase
Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
* Refactor Prometheus exporter to use `_get_func_metrics` helper for cleaner metrics handling
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update linters' configurations and clean up docstring conventions
* Update linters' configurations and clean up docstring conventions
* Fix metrics framework: async instrumentation, Prometheus consistency, and cleanup
- Instrument _call_async with full cache_metrics coverage matching _call (hits,
misses, stale hits, recalculations, wait timeouts, latency on every code path)
- Fix _calc_entry_async to record size_limit_rejection when entry is not stored
- Fix _generate_text_metrics to snapshot all functions in one lock acquisition,
preventing internally inconsistent Prometheus scrapes
- Replace global REGISTRY with per-instance CollectorRegistry in PrometheusExporter,
eliminating silent double-registration data loss
- Add cachier_wait_timeouts_total to Prometheus text export and custom collector
- Make export_metrics non-abstract in MetricsExporter ABC (concrete no-op default)
- Add type annotations to CachierCollector and MetricsHandler inner classes
- Move random import to module level in metrics.py; remove dead _monotonic_start
and _wall_start attributes
- Document stale-as-miss counting behavior and total_size_bytes backend limitation
in MetricSnapshot docstring
- Remove METRICS_IMPLEMENTATION.md from repository root
- Add 13 new tests: async hit/miss/stale tracking, sampling_rate=0.0 boundary,
empty window_sizes, double-instantiation isolation, text metrics consistency
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Achieve 100% coverage on metrics and exporters modules
- Add # pragma: no cover to unreachable defensive guards (ImportError handler
for optional prometheus_client, dead early-return in _setup_collector)
- Fix stop() to call server_close() and join the server thread, eliminating
ResourceWarning on socket cleanup
- Add 17 new tests to reach 100% branch coverage:
- test_metrics_wait_timeout_direct: exercises record_wait_timeout directly
- test_metrics_sampling_rate_zero_skips_all_methods: covers early-return
branches in record_stale_hit, record_wait_timeout, record_size_limit_rejection,
and record_latency when sampling_rate=0.0
- test_metrics_context_manager / test_metrics_context_manager_none: covers
MetricsContext.__enter__ and __exit__ with and without a metrics object
- test_prometheus_export_metrics_noop: covers the export_metrics no-op path
- test_prometheus_text_metrics_skips_none_metrics: covers the m-is-None branch
in _generate_text_metrics
- test_prometheus_start_stop_simple_server / _prometheus_server: covers start()
and stop() for both server backends
- test_prometheus_simple_server_404 / _prometheus_server_404: covers the 404
response path in both MetricsHandler.do_GET implementations
- test_prometheus_collector_collect / _collect_empty / _collect_skips_none_metrics:
covers CachierCollector.collect() including the m-is-None skip branch
- test_prometheus_client_not_available: covers PrometheusExporter fallback when
PROMETHEUS_CLIENT_AVAILABLE is patched to False
- test_prometheus_stop_when_not_started: covers stop() when _server is None
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Refactor metrics examples: modularize examples into functions and add `main()` entry point
* Refactor Prometheus exporter and cache metrics framework
- Extract `CachierCollector` as a top-level class for cleaner modularity
- Use `MetricsContext` for consistent cache metrics tracking across sync and async paths
- Simplify metric counter updates with a shared `_record_counter` helper method
- Refactor Prometheus text metric generation to eliminate redundancy
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Refactor: compact Prometheus client imports and docstrings in metrics framework
* Refactor: prefix `set_entry` and `aset_entry` with `_` across all cores and centralize size-limit metric recording logic
* Refactor: replace `set_entry` with `_set_entry` in async methods across cores and refine `TYPE_CHECKING` import logic
* Refactor: rename `MetricsContext` variable to `_mctx` for consistent naming across sync and async methods
* Refactor: update monkeypatching to reflect `_set_entry` and `_aset_entry` renaming in tests
* Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Refactor: simplify cutoff calculation in metrics using ternary operator
* Refactor: rename `set_entry` to `_set_entry`, refine size-limit logic, and add metric for size-limit rejections
* Add tests for metrics: validate `entry_count` and `total_size_bytes` for memory and pickle backends
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add tests for `_BaseCore`: metric hooks default values and timeout behavior
* Add tests for metrics: refactor sampling rate tests and add Prometheus exporter mocks
* Remove outdated tests for overwrite/skip cache and Prometheus exporter fallback
* fix(metrics): address review findings before merge
- Replace all hardcoded test ports (9093-19200) with port=0 and read
actual port from server_address[1] to prevent CI port collisions
- Clarify CacheMetrics.window_sizes docstring: windowing is not
automatic; callers must pass window= explicitly to get_stats()
- Add README note that entry_count/total_size_bytes are populated for
the memory backend only; all other backends report 0
- Standardize MetricsContext guards to 'if self._m is not None:'
- Remove dead _init_prometheus_metrics no-op method and its call site
- Replace deprecated typing.Deque/Dict with deque[...]/dict[...] builtins
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Shay Palachy-Affek <shaypal5@users.noreply.github.com>1 parent 4e181ca commit 4602401
22 files changed
Lines changed: 2849 additions & 163 deletions
File tree
- examples
- src/cachier
- cores
- exporters
- tests
- sql_tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
327 | 328 | | |
328 | 329 | | |
329 | 330 | | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
330 | 429 | | |
331 | 430 | | |
332 | 431 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
0 commit comments