feat(api): complete Phase 2 - framework-less asyncio Ingestion API with backpressure and metrics
This commit is contained in:
37
app/api/metrics.py
Normal file
37
app/api/metrics.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import threading
|
||||
|
||||
|
||||
class APIMetrics:
|
||||
"""
|
||||
In-memory metrics tracker for the Ingestion API.
|
||||
Thread-safe and async-safe counter for Prometheus scraping.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._lock = threading.Lock()
|
||||
self._received_total: int = 0
|
||||
|
||||
def increment_received(self, count: int = 1) -> None:
|
||||
with self._lock:
|
||||
self._received_total += count
|
||||
|
||||
@property
|
||||
def total_received(self) -> int:
|
||||
with self._lock:
|
||||
return self._received_total
|
||||
|
||||
def get_prometheus_metrics(self) -> str:
|
||||
"""
|
||||
Formats metrics into standard Prometheus text format.
|
||||
"""
|
||||
with self._lock:
|
||||
val = self._received_total
|
||||
|
||||
return (
|
||||
"# HELP cdr_api_received_total Total CDRs received by the API\n"
|
||||
"# TYPE cdr_api_received_total counter\n"
|
||||
f"cdr_api_received_total {val}\n"
|
||||
)
|
||||
|
||||
|
||||
api_metrics = APIMetrics()
|
||||
Reference in New Issue
Block a user