feat(api): complete Phase 2 - framework-less asyncio Ingestion API with backpressure and metrics

This commit is contained in:
2026-09-19 13:18:40 +03:30
parent e40af20331
commit 66a50cd3ba
22 changed files with 775 additions and 20 deletions

37
app/api/metrics.py Normal file
View 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()