consolidate: HQ Prometheus metrics endpoint
Browse files- hq/metrics.py +32 -0
hq/metrics.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent Q3 [HQ] — Metrics
|
| 3 |
+
Prometheus-compatible /metrics endpoint + internal tracking.
|
| 4 |
+
"""
|
| 5 |
+
import time
|
| 6 |
+
from collections import defaultdict
|
| 7 |
+
|
| 8 |
+
_counters = defaultdict(int)
|
| 9 |
+
_latencies = defaultdict(list)
|
| 10 |
+
_start_time = time.time()
|
| 11 |
+
|
| 12 |
+
def track_request(target: str, latency_s: float):
|
| 13 |
+
_counters[f"requests_total_{target}"] += 1
|
| 14 |
+
_latencies[target].append(latency_s)
|
| 15 |
+
|
| 16 |
+
def get_metrics() -> dict:
|
| 17 |
+
uptime = time.time() - _start_time
|
| 18 |
+
avg_latency = {
|
| 19 |
+
t: round(sum(v)/len(v), 3) if v else 0
|
| 20 |
+
for t, v in _latencies.items()
|
| 21 |
+
}
|
| 22 |
+
return {
|
| 23 |
+
"uptime_seconds": round(uptime, 1),
|
| 24 |
+
"request_counts": dict(_counters),
|
| 25 |
+
"avg_latency_seconds": avg_latency,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def prometheus_format() -> str:
|
| 29 |
+
lines = [f"# Agent Q3 [HQ] Metrics\nagentq3_uptime_seconds {round(time.time()-_start_time,1)}"]
|
| 30 |
+
for k, v in _counters.items():
|
| 31 |
+
lines.append(f"agentq3_{k} {v}")
|
| 32 |
+
return "\n".join(lines)
|