Spaces:
Paused
Paused
Upload core/cache_monitoring.py with huggingface_hub
Browse files- core/cache_monitoring.py +26 -8
core/cache_monitoring.py
CHANGED
|
@@ -15,13 +15,21 @@ from prometheus_client import Counter, Gauge, Histogram
|
|
| 15 |
from core.logging import logger
|
| 16 |
|
| 17 |
# Prometheus metrics for cache
|
| 18 |
-
cache_hits = Counter(
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
cache_misses = Counter(
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
cache_set_errors = Counter(
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
cache_get_errors = Counter(
|
|
|
|
|
|
|
| 25 |
|
| 26 |
cache_latency = Histogram(
|
| 27 |
"cache_operation_duration_seconds",
|
|
@@ -31,7 +39,9 @@ cache_latency = Histogram(
|
|
| 31 |
|
| 32 |
cache_size = Gauge("cache_size_bytes", "Current cache size in bytes", ["cache_name"])
|
| 33 |
|
| 34 |
-
cache_entry_count = Gauge(
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
class CacheMonitor:
|
|
@@ -135,7 +145,11 @@ class CacheMonitor:
|
|
| 135 |
"hits": self._hit_count,
|
| 136 |
"misses": self._miss_count,
|
| 137 |
"hit_rate": self.get_hit_rate(),
|
| 138 |
-
"avg_latency_ms": (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
|
|
@@ -155,7 +169,9 @@ class _CacheOperationContext:
|
|
| 155 |
duration = time.time() - self.start_time
|
| 156 |
|
| 157 |
# Record latency
|
| 158 |
-
cache_latency.labels(
|
|
|
|
|
|
|
| 159 |
|
| 160 |
self.monitor._total_latency += duration
|
| 161 |
self.monitor._operation_count += 1
|
|
@@ -220,4 +236,6 @@ def get_cache_stats(cache_name: str | None = None) -> dict:
|
|
| 220 |
def log_cache_performance():
|
| 221 |
"""Log cache performance statistics"""
|
| 222 |
for cache_name, stats in _cache_stats.items():
|
| 223 |
-
logger.info(
|
|
|
|
|
|
|
|
|
| 15 |
from core.logging import logger
|
| 16 |
|
| 17 |
# Prometheus metrics for cache
|
| 18 |
+
cache_hits = Counter(
|
| 19 |
+
"cache_hits_total", "Number of cache hits", ["cache_name", "operation"]
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
cache_misses = Counter(
|
| 23 |
+
"cache_misses_total", "Number of cache misses", ["cache_name", "operation"]
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
cache_set_errors = Counter(
|
| 27 |
+
"cache_set_errors_total", "Number of cache set errors", ["cache_name"]
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
cache_get_errors = Counter(
|
| 31 |
+
"cache_get_errors_total", "Number of cache get errors", ["cache_name"]
|
| 32 |
+
)
|
| 33 |
|
| 34 |
cache_latency = Histogram(
|
| 35 |
"cache_operation_duration_seconds",
|
|
|
|
| 39 |
|
| 40 |
cache_size = Gauge("cache_size_bytes", "Current cache size in bytes", ["cache_name"])
|
| 41 |
|
| 42 |
+
cache_entry_count = Gauge(
|
| 43 |
+
"cache_entries_total", "Number of entries in cache", ["cache_name"]
|
| 44 |
+
)
|
| 45 |
|
| 46 |
|
| 47 |
class CacheMonitor:
|
|
|
|
| 145 |
"hits": self._hit_count,
|
| 146 |
"misses": self._miss_count,
|
| 147 |
"hit_rate": self.get_hit_rate(),
|
| 148 |
+
"avg_latency_ms": (
|
| 149 |
+
(self._total_latency / self._operation_count * 1000)
|
| 150 |
+
if self._operation_count > 0
|
| 151 |
+
else 0
|
| 152 |
+
),
|
| 153 |
}
|
| 154 |
|
| 155 |
|
|
|
|
| 169 |
duration = time.time() - self.start_time
|
| 170 |
|
| 171 |
# Record latency
|
| 172 |
+
cache_latency.labels(
|
| 173 |
+
cache_name=self.monitor.cache_name, operation=self.operation
|
| 174 |
+
).observe(duration)
|
| 175 |
|
| 176 |
self.monitor._total_latency += duration
|
| 177 |
self.monitor._operation_count += 1
|
|
|
|
| 236 |
def log_cache_performance():
|
| 237 |
"""Log cache performance statistics"""
|
| 238 |
for cache_name, stats in _cache_stats.items():
|
| 239 |
+
logger.info(
|
| 240 |
+
"Cache performance report", extra={"cache_name": cache_name, **stats}
|
| 241 |
+
)
|