Files
taiji-AI-PAD/services/data-ingestion/app/metrics.py
T
2025-12-24 11:03:53 +00:00

118 lines
3.0 KiB
Python

"""Prometheus metrics definitions and middleware for the data-ingestion service."""
from __future__ import annotations
import time
from fastapi import FastAPI, Request
from prometheus_client import Counter, Histogram, Gauge
# HTTP request metrics
http_requests_total = Counter(
"data_ingestion_http_requests_total",
"Total HTTP requests",
["method", "endpoint", "status"],
)
http_request_duration = Histogram(
"data_ingestion_http_request_duration_seconds",
"HTTP request duration",
["method", "endpoint"],
)
# API processing metrics
rapidapi_sync_total = Counter(
"data_ingestion_rapidapi_sync_total",
"Total RapidAPI sync operations",
["status"],
)
rapidapi_endpoints_synced = Gauge(
"data_ingestion_rapidapi_endpoints_synced",
"Number of RapidAPI endpoints synced",
)
apillama_processing_total = Counter(
"data_ingestion_apillama_processing_total",
"Total APILLAMA processing operations",
["status"],
)
apillama_processing_duration = Histogram(
"data_ingestion_apillama_processing_duration_seconds",
"APILLAMA processing duration",
)
openapi_parse_total = Counter(
"data_ingestion_openapi_parse_total",
"Total OpenAPI parse operations",
["status"],
)
openapi_parse_duration = Histogram(
"data_ingestion_openapi_parse_duration_seconds",
"OpenAPI parse duration",
)
tools_generated_total = Counter(
"data_ingestion_tools_generated_total",
"Total tools generated",
["category"],
)
tools_registry_size = Gauge(
"data_ingestion_tools_registry_size",
"Number of tools in registry",
)
# Cache metrics
cache_hits_total = Counter(
"data_ingestion_cache_hits_total",
"Total cache hits",
["type"],
)
cache_misses_total = Counter(
"data_ingestion_cache_misses_total",
"Total cache misses",
["type"],
)
# System metrics
redis_connections = Gauge(
"data_ingestion_redis_connections",
"Redis connection status (1=connected, 0=disconnected)",
)
nats_connections = Gauge(
"data_ingestion_nats_connections",
"NATS connection status (1=connected, 0=disconnected)",
)
def register_http_metrics(app: FastAPI) -> None:
"""Attach HTTP metrics middleware to the app."""
@app.middleware("http")
async def metrics_middleware(request: Request, call_next): # type: ignore[misc]
start_time = time.time()
method = request.method
endpoint = request.url.path
try:
response = await call_next(request)
status = response.status_code
except Exception:
status = 500
http_requests_total.labels(method=method, endpoint=endpoint, status=status).inc()
http_request_duration.labels(method=method, endpoint=endpoint).observe(
time.time() - start_time
)
raise
http_requests_total.labels(method=method, endpoint=endpoint, status=status).inc()
http_request_duration.labels(method=method, endpoint=endpoint).observe(
time.time() - start_time
)
return response