Repository navigation
Expand file tree
/
Copy pathmonitor_server.py
More file actions
98 lines (80 loc) · 3.15 KB
/
Copy pathmonitor_server.py
File metadata and controls
98 lines (80 loc) · 3.15 KB
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
"""Monitor server for vLLM Prefix Cache metrics."""
import time
import json
import threading
from dataclasses import dataclass, field
from http.server import HTTPServer, BaseHTTPRequestHandler
@dataclass
class CacheMetrics:
"""Thread-safe cache metrics collector."""
total_queries: int = 0
cache_hits: int = 0
cache_misses: int = 0
total_tokens_hit: int = 0
total_tokens_missed: int = 0
block_usage: int = 0
total_blocks: int = 0
lock: threading.Lock = field(default_factory=threading.Lock)
def record_hit(self, num_tokens: int):
with self.lock:
self.total_queries += 1
self.cache_hits += 1
self.total_tokens_hit += num_tokens
def record_miss(self, num_tokens: int):
with self.lock:
self.total_queries += 1
self.cache_misses += 1
self.total_tokens_missed += num_tokens
def update_block_usage(self, used: int, total: int):
with self.lock:
self.block_usage = used
self.total_blocks = total
def snapshot(self) -> dict:
with self.lock:
total = max(self.cache_hits + self.cache_misses, 1)
token_hit_rate = 0.0
total_tokens = self.total_tokens_hit + self.total_tokens_missed
if total_tokens > 0:
token_hit_rate = self.total_tokens_hit / total_tokens
return {
"timestamp": time.time(),
"query_hit_rate": self.cache_hits / total,
"token_hit_rate": round(token_hit_rate, 4),
"cache_hits": self.cache_hits,
"cache_misses": self.cache_misses,
"tokens_hit": self.total_tokens_hit,
"tokens_missed": self.total_tokens_missed,
"block_usage": self.block_usage,
"total_blocks": self.total_blocks,
"block_usage_pct": round(self.block_usage / max(self.total_blocks, 1) * 100, 2),
}
# Singleton metrics collector
METRICS = CacheMetrics()
def get_metrics():
return METRICS
class MetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler for /cache_stats endpoint."""
def do_GET(self):
if self.path == "/cache_stats":
metrics = METRICS.snapshot()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(metrics, indent=2).encode())
elif self.path == "/health":
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"ok")
else:
self.send_response(404)
self.end_headers()
def log_message(self, format, *args):
pass # Suppress default logging
def start_monitor_server(host: str = "0.0.0.0", port: int = 9090):
"""Start the metrics HTTP server in a daemon thread."""
server = HTTPServer((host, port), MetricsHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
print(f"Cache monitor running at http://{host}:{port}/cache_stats")
return server