bae705aa97
- Add NFC ePassport roadmap (ICAO 9303, eIDAS) - Add TensorFlow.js edge face detection (BlazeFace) - Add structured audit logger (GDPR-compliant) - Risk scoring support Part of KYC Apple Native UX v1.1.0
144 lines
3.4 KiB
Python
144 lines
3.4 KiB
Python
"""
|
|
Metrics and monitoring
|
|
Prometheus-compatible metrics
|
|
"""
|
|
|
|
from prometheus_client import Counter, Histogram, Gauge, Info, generate_latest
|
|
from fastapi import Request, Response
|
|
import time
|
|
|
|
# Request metrics
|
|
request_count = Counter(
|
|
'iom_requests_total',
|
|
'Total requests',
|
|
['method', 'endpoint', 'status']
|
|
)
|
|
|
|
request_duration = Histogram(
|
|
'iom_request_duration_seconds',
|
|
'Request duration',
|
|
['method', 'endpoint']
|
|
)
|
|
|
|
# Business metrics
|
|
observations_processed = Counter(
|
|
'iom_observations_processed_total',
|
|
'Total observations processed'
|
|
)
|
|
|
|
signals_generated = Counter(
|
|
'iom_signals_generated_total',
|
|
'Total signals generated',
|
|
['signal_type']
|
|
)
|
|
|
|
decisions_made = Counter(
|
|
'iom_decisions_made_total',
|
|
'Total decisions made',
|
|
['decision_type']
|
|
)
|
|
|
|
# System metrics
|
|
active_connections = Gauge(
|
|
'iom_active_connections',
|
|
'Active connections'
|
|
)
|
|
|
|
database_connections = Gauge(
|
|
'iom_database_connections',
|
|
'Database connections'
|
|
)
|
|
|
|
# Model metrics
|
|
model_predictions = Counter(
|
|
'iom_model_predictions_total',
|
|
'Total model predictions',
|
|
['model_type']
|
|
)
|
|
|
|
model_accuracy = Gauge(
|
|
'iom_model_accuracy',
|
|
'Model accuracy',
|
|
['model_type']
|
|
)
|
|
|
|
# Platform info
|
|
platform_info = Info('iom_platform', 'Platform information')
|
|
|
|
|
|
class MetricsMiddleware:
|
|
"""Middleware to collect metrics"""
|
|
|
|
def __init__(self, app):
|
|
self.app = app
|
|
platform_info.info({
|
|
'version': '1.0.0',
|
|
'environment': 'production'
|
|
})
|
|
|
|
async def __call__(self, scope, receive, send):
|
|
if scope["type"] == "http":
|
|
request = Request(scope, receive)
|
|
|
|
start_time = time.time()
|
|
|
|
# Track active connections
|
|
active_connections.inc()
|
|
|
|
try:
|
|
response = await self.app(scope, receive, send)
|
|
|
|
# Record metrics
|
|
duration = time.time() - start_time
|
|
endpoint = request.url.path
|
|
method = request.method
|
|
status = response.status_code if hasattr(response, 'status_code') else 200
|
|
|
|
request_count.labels(
|
|
method=method,
|
|
endpoint=endpoint,
|
|
status=status
|
|
).inc()
|
|
|
|
request_duration.labels(
|
|
method=method,
|
|
endpoint=endpoint
|
|
).observe(duration)
|
|
|
|
return response
|
|
|
|
finally:
|
|
active_connections.dec()
|
|
else:
|
|
return await self.app(scope, receive, send)
|
|
|
|
|
|
def get_metrics():
|
|
"""Get Prometheus metrics"""
|
|
return generate_latest()
|
|
|
|
|
|
def record_observation_processed():
|
|
"""Record observation processed"""
|
|
observations_processed.inc()
|
|
|
|
|
|
def record_signal_generated(signal_type: str):
|
|
"""Record signal generated"""
|
|
signals_generated.labels(signal_type=signal_type).inc()
|
|
|
|
|
|
def record_decision(decision_type: str):
|
|
"""Record decision made"""
|
|
decisions_made.labels(decision_type=decision_type).inc()
|
|
|
|
|
|
def record_prediction(model_type: str):
|
|
"""Record model prediction"""
|
|
model_predictions.labels(model_type=model_type).inc()
|
|
|
|
|
|
def set_model_accuracy(model_type: str, accuracy: float):
|
|
"""Set model accuracy"""
|
|
model_accuracy.labels(model_type=model_type).set(accuracy)
|