Files
telecom-cdr-pipeline-ai/app/api/metrics.py

38 lines
944 B
Python

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()