Skip to content

Commit ea89041

Browse files
BordaCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c6aef7e commit ea89041

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

README.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ The metrics system tracks:
351351
* **Recalculations**: Count of cache recalculations triggered
352352
* **Wait timeouts**: Timeouts during concurrent calculation waits
353353
* **Size limit rejections**: Entries rejected due to ``entry_size_limit``
354-
* **Cache size**: Number of entries and total size in bytes
354+
* **Cache size (memory backend only)**: Number of entries and total size in bytes for the in-memory cache core
355355

356356
Sampling Rate
357357
-------------
@@ -379,14 +379,16 @@ Export metrics to Prometheus for monitoring and alerting:
379379
return x ** 2
380380
381381
# Set up Prometheus exporter
382-
# Note: use_prometheus_client=False ensures live metrics are exposed for registered functions.
383-
exporter = PrometheusExporter(port=9090, use_prometheus_client=False)
382+
# use_prometheus_client controls whether metrics are exposed via the prometheus_client
383+
# registry (True) or via Cachier's own HTTP handler (False). In both modes, metrics for
384+
# registered functions are collected live at scrape time.
385+
exporter = PrometheusExporter(port=9090, use_prometheus_client=True)
384386
exporter.register_function(my_operation)
385387
exporter.start()
386388
387389
# Metrics available at http://localhost:9090/metrics
388390
389-
The exporter provides metrics in Prometheus text format, compatible with standard Prometheus scraping, when used with ``use_prometheus_client=False`` as shown above. A ``prometheus_client``-based mode is also available via ``use_prometheus_client=True``, but in the current release it may not expose live values for registered functions.
391+
The exporter provides metrics in Prometheus text format, compatible with standard Prometheus scraping, in both ``use_prometheus_client=True`` and ``use_prometheus_client=False`` modes. When ``use_prometheus_client=True``, Cachier registers a custom collector with ``prometheus_client`` that pulls live statistics from registered functions at scrape time, so scraped values reflect the current state of the cache. When ``use_prometheus_client=False``, Cachier serves the same metrics directly without requiring the ``prometheus_client`` dependency.
390392

391393
Programmatic Access
392394
-------------------

src/cachier/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
432432
_print("But it is stale... :(")
433433
if cache_metrics:
434434
cache_metrics.record_stale_hit()
435+
cache_metrics.record_miss()
435436
if entry._processing:
436437
if _next_time:
437438
_print("Returning stale.")

src/cachier/cores/memory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,7 @@ def _get_total_size(self) -> int:
138138
try:
139139
total += self._estimate_size(entry.value)
140140
except Exception:
141-
pass
141+
# Size estimation is best-effort; skip entries that cannot be sized
142+
# to avoid breaking cache functionality or metrics collection.
143+
continue
142144
return total

src/cachier/exporters/prometheus.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ def __init__(
7575
self._server: Optional[Any] = None
7676
self._server_thread: Optional[threading.Thread] = None
7777

78-
# Track last-seen values for delta calculation
79-
self._last_seen: Dict[str, Dict[str, int]] = {}
8078

8179
# Try to import prometheus_client if requested
8280
self._prom_client = None
@@ -111,14 +109,14 @@ def collect(self):
111109
with self.exporter._lock:
112110
# Collect hits
113111
hits = CounterMetricFamily(
114-
"cachier_cache_hits",
112+
"cachier_cache_hits_total",
115113
"Total cache hits",
116114
labels=["function"],
117115
)
118116

119117
# Collect misses
120118
misses = CounterMetricFamily(
121-
"cachier_cache_misses",
119+
"cachier_cache_misses_total",
122120
"Total cache misses",
123121
labels=["function"],
124122
)

0 commit comments

Comments
 (0)