File size: 19,070 Bytes
824bf31 3c90182 824bf31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
"""
Comprehensive monitoring and observability system.
Provides metrics collection, distributed tracing, and health monitoring.
"""
import time
import psutil
import asyncio
from typing import Dict, List, Optional, Any
from datetime import datetime, timedelta
from collections import defaultdict, deque
from contextlib import asynccontextmanager
import logging
from prometheus_client import Counter, Histogram, Gauge, generate_latest, CONTENT_TYPE_LATEST
from opentelemetry import trace, baggage
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from opentelemetry.instrumentation.redis import RedisInstrumentor
from src.core.config import get_settings
from src.core import get_logger
logger = get_logger(__name__)
settings = get_settings()
# Prometheus Metrics
REQUEST_COUNT = Counter(
'cidadao_ai_requests_total',
'Total number of requests',
['method', 'endpoint', 'status_code']
)
REQUEST_DURATION = Histogram(
'cidadao_ai_request_duration_seconds',
'Request duration in seconds',
['method', 'endpoint']
)
AGENT_TASK_COUNT = Counter(
'cidadao_ai_agent_tasks_total',
'Total number of agent tasks',
['agent_type', 'task_type', 'status']
)
AGENT_TASK_DURATION = Histogram(
'cidadao_ai_agent_task_duration_seconds',
'Agent task duration in seconds',
['agent_type', 'task_type']
)
DATABASE_QUERIES = Counter(
'cidadao_ai_database_queries_total',
'Total number of database queries',
['operation', 'table']
)
DATABASE_QUERY_DURATION = Histogram(
'cidadao_ai_database_query_duration_seconds',
'Database query duration in seconds',
['operation', 'table']
)
TRANSPARENCY_API_CALLS = Counter(
'cidadao_ai_transparency_api_calls_total',
'Total calls to transparency API',
['endpoint', 'status']
)
TRANSPARENCY_API_DURATION = Histogram(
'cidadao_ai_transparency_api_duration_seconds',
'Transparency API call duration',
['endpoint']
)
SYSTEM_CPU_USAGE = Gauge(
'cidadao_ai_system_cpu_percent',
'System CPU usage percentage'
)
SYSTEM_MEMORY_USAGE = Gauge(
'cidadao_ai_system_memory_percent',
'System memory usage percentage'
)
REDIS_OPERATIONS = Counter(
'cidadao_ai_redis_operations_total',
'Total Redis operations',
['operation', 'status']
)
ACTIVE_CONNECTIONS = Gauge(
'cidadao_ai_active_connections',
'Number of active connections',
['connection_type']
)
# Investigation and Anomaly Detection Metrics
INVESTIGATIONS_TOTAL = Counter(
'cidadao_ai_investigations_total',
'Total number of investigations started',
['agent_type', 'investigation_type', 'status']
)
ANOMALIES_DETECTED = Counter(
'cidadao_ai_anomalies_detected_total',
'Total number of anomalies detected',
['anomaly_type', 'severity', 'agent']
)
INVESTIGATION_DURATION = Histogram(
'cidadao_ai_investigation_duration_seconds',
'Time taken for investigations',
['agent_type', 'investigation_type']
)
DATA_RECORDS_PROCESSED = Counter(
'cidadao_ai_data_records_processed_total',
'Total number of data records processed',
['data_source', 'agent', 'operation']
)
TRANSPARENCY_API_DATA_FETCHED = Counter(
'cidadao_ai_transparency_data_fetched_total',
'Total data fetched from transparency API',
['endpoint', 'organization', 'status']
)
class PerformanceMetrics:
"""System performance metrics collector."""
def __init__(self):
self.response_times = deque(maxlen=1000)
self.error_rates = defaultdict(int)
self.throughput_counter = 0
self.last_throughput_reset = time.time()
def record_request(self, duration: float, status_code: int, endpoint: str):
"""Record request metrics."""
self.response_times.append(duration)
if status_code >= 400:
self.error_rates[endpoint] += 1
self.throughput_counter += 1
def get_avg_response_time(self) -> float:
"""Get average response time."""
if not self.response_times:
return 0.0
return sum(self.response_times) / len(self.response_times)
def get_p95_response_time(self) -> float:
"""Get 95th percentile response time."""
if not self.response_times:
return 0.0
sorted_times = sorted(self.response_times)
index = int(0.95 * len(sorted_times))
return sorted_times[min(index, len(sorted_times) - 1)]
def get_throughput(self) -> float:
"""Get requests per second."""
elapsed = time.time() - self.last_throughput_reset
if elapsed == 0:
return 0.0
return self.throughput_counter / elapsed
def get_error_rate(self, endpoint: str = None) -> float:
"""Get error rate for endpoint or overall."""
if endpoint:
total_requests = sum(1 for _ in self.response_times) # Approximate
errors = self.error_rates.get(endpoint, 0)
return errors / max(total_requests, 1)
total_errors = sum(self.error_rates.values())
total_requests = sum(1 for _ in self.response_times)
return total_errors / max(total_requests, 1)
def reset_throughput_counter(self):
"""Reset throughput counter."""
self.throughput_counter = 0
self.last_throughput_reset = time.time()
class SystemHealthMonitor:
"""System health monitoring."""
def __init__(self):
self.health_checks = {}
self.last_check = {}
self.check_intervals = {
'database': 30, # seconds
'redis': 30,
'transparency_api': 60,
'disk_space': 300, # 5 minutes
'memory': 60
}
async def check_database_health(self) -> Dict[str, Any]:
"""Check database connectivity and performance."""
try:
from src.core.database import get_db_session
start_time = time.time()
async with get_db_session() as session:
# Simple connectivity test
await session.execute("SELECT 1")
response_time = time.time() - start_time
return {
"status": "healthy",
"response_time": response_time,
"timestamp": datetime.utcnow(),
"details": "Database connection successful"
}
except Exception as e:
logger.error(f"Database health check failed: {e}")
return {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.utcnow()
}
async def check_redis_health(self) -> Dict[str, Any]:
"""Check Redis connectivity and performance."""
try:
from src.core.cache import get_redis_client
start_time = time.time()
redis = await get_redis_client()
# Test Redis connectivity
await redis.ping()
response_time = time.time() - start_time
# Get Redis info
info = await redis.info()
memory_usage = info.get('used_memory', 0)
connected_clients = info.get('connected_clients', 0)
return {
"status": "healthy",
"response_time": response_time,
"memory_usage": memory_usage,
"connected_clients": connected_clients,
"timestamp": datetime.utcnow()
}
except Exception as e:
logger.error(f"Redis health check failed: {e}")
return {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.utcnow()
}
async def check_transparency_api_health(self) -> Dict[str, Any]:
"""Check Portal da Transparência API health."""
try:
import aiohttp
start_time = time.time()
async with aiohttp.ClientSession() as session:
# Test API availability with a simple request
url = "https://api.portaldatransparencia.gov.br/api-de-dados/versao"
headers = {
"chave-api-dados": settings.transparency_api_key.get_secret_value()
}
async with session.get(url, headers=headers, timeout=10) as response:
response_time = time.time() - start_time
if response.status == 200:
return {
"status": "healthy",
"response_time": response_time,
"api_status": response.status,
"timestamp": datetime.utcnow()
}
else:
return {
"status": "degraded",
"response_time": response_time,
"api_status": response.status,
"timestamp": datetime.utcnow()
}
except Exception as e:
logger.error(f"Transparency API health check failed: {e}")
return {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.utcnow()
}
def check_system_resources(self) -> Dict[str, Any]:
"""Check system resource usage."""
try:
# CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
SYSTEM_CPU_USAGE.set(cpu_percent)
# Memory usage
memory = psutil.virtual_memory()
memory_percent = memory.percent
SYSTEM_MEMORY_USAGE.set(memory_percent)
# Disk usage
disk = psutil.disk_usage('/')
disk_percent = (disk.used / disk.total) * 100
# Network stats
network = psutil.net_io_counters()
return {
"status": "healthy" if cpu_percent < 80 and memory_percent < 80 else "warning",
"cpu_percent": cpu_percent,
"memory_percent": memory_percent,
"disk_percent": disk_percent,
"disk_free_gb": disk.free / (1024**3),
"network_bytes_sent": network.bytes_sent,
"network_bytes_recv": network.bytes_recv,
"timestamp": datetime.utcnow()
}
except Exception as e:
logger.error(f"System resource check failed: {e}")
return {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.utcnow()
}
async def get_comprehensive_health(self) -> Dict[str, Any]:
"""Get comprehensive system health status."""
health_status = {
"overall_status": "healthy",
"timestamp": datetime.utcnow(),
"checks": {}
}
# Run all health checks
checks = {
"database": self.check_database_health(),
"redis": self.check_redis_health(),
"transparency_api": self.check_transparency_api_health(),
"system_resources": asyncio.create_task(asyncio.coroutine(self.check_system_resources)())
}
# Execute checks concurrently
for check_name, check_coro in checks.items():
try:
if asyncio.iscoroutine(check_coro):
result = await check_coro
else:
result = check_coro
health_status["checks"][check_name] = result
# Update overall status
if result["status"] != "healthy":
if health_status["overall_status"] == "healthy":
health_status["overall_status"] = "degraded"
if result["status"] == "unhealthy":
health_status["overall_status"] = "unhealthy"
except Exception as e:
logger.error(f"Health check {check_name} failed: {e}")
health_status["checks"][check_name] = {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.utcnow()
}
health_status["overall_status"] = "unhealthy"
return health_status
class DistributedTracing:
"""Distributed tracing configuration and utilities."""
def __init__(self):
self.tracer_provider = None
self.tracer = None
self.setup_tracing()
def setup_tracing(self):
"""Setup OpenTelemetry distributed tracing."""
try:
# Configure tracer provider
self.tracer_provider = TracerProvider()
trace.set_tracer_provider(self.tracer_provider)
# Configure Jaeger exporter
jaeger_exporter = JaegerExporter(
agent_host_name=settings.jaeger_host,
agent_port=settings.jaeger_port,
)
# Add batch span processor
span_processor = BatchSpanProcessor(jaeger_exporter)
self.tracer_provider.add_span_processor(span_processor)
# Get tracer
self.tracer = trace.get_tracer(__name__)
# Instrument frameworks
FastAPIInstrumentor.instrument()
SQLAlchemyInstrumentor.instrument()
RedisInstrumentor.instrument()
logger.info("Distributed tracing configured successfully")
except Exception as e:
logger.error(f"Failed to configure distributed tracing: {e}")
@asynccontextmanager
async def trace_operation(self, operation_name: str, **attributes):
"""Context manager for tracing operations."""
if not self.tracer:
yield None
return
with self.tracer.start_as_current_span(operation_name) as span:
# Add attributes
for key, value in attributes.items():
span.set_attribute(key, str(value))
try:
yield span
except Exception as e:
span.record_exception(e)
span.set_status(trace.Status(trace.StatusCode.ERROR, str(e)))
raise
def add_baggage(self, key: str, value: str):
"""Add baggage to current trace context."""
baggage.set_baggage(key, value)
def get_baggage(self, key: str) -> Optional[str]:
"""Get baggage from current trace context."""
return baggage.get_baggage(key)
class AlertManager:
"""Alert management system."""
def __init__(self):
self.alert_thresholds = {
'response_time_p95': 2.0, # seconds
'error_rate': 0.05, # 5%
'cpu_usage': 80.0, # percent
'memory_usage': 85.0, # percent
'disk_usage': 90.0, # percent
}
self.alert_history = deque(maxlen=1000)
self.active_alerts = {}
def check_thresholds(self, metrics: Dict[str, float]) -> List[Dict[str, Any]]:
"""Check if any metrics exceed thresholds."""
alerts = []
for metric_name, threshold in self.alert_thresholds.items():
value = metrics.get(metric_name, 0)
if value > threshold:
alert = {
"metric": metric_name,
"value": value,
"threshold": threshold,
"severity": self._get_alert_severity(metric_name, value, threshold),
"timestamp": datetime.utcnow(),
"message": f"{metric_name} ({value}) exceeds threshold ({threshold})"
}
alerts.append(alert)
self.active_alerts[metric_name] = alert
self.alert_history.append(alert)
elif metric_name in self.active_alerts:
# Clear resolved alert
resolved_alert = self.active_alerts.pop(metric_name)
resolved_alert["resolved_at"] = datetime.utcnow()
self.alert_history.append(resolved_alert)
return alerts
def _get_alert_severity(self, metric_name: str, value: float, threshold: float) -> str:
"""Determine alert severity based on how much threshold is exceeded."""
ratio = value / threshold
if ratio > 1.5:
return "critical"
elif ratio > 1.2:
return "high"
elif ratio > 1.1:
return "medium"
else:
return "low"
async def send_alert(self, alert: Dict[str, Any]):
"""Send alert notification (implement webhook, email, etc.)."""
# Log alert
logger.warning(f"ALERT: {alert['message']}")
# Here you would implement actual alerting
# e.g., send to Slack, PagerDuty, email, etc.
pass
# Global instances
performance_metrics = PerformanceMetrics()
health_monitor = SystemHealthMonitor()
distributed_tracing = DistributedTracing()
alert_manager = AlertManager()
def get_metrics_data() -> str:
"""Get Prometheus metrics data."""
return generate_latest()
async def collect_system_metrics() -> Dict[str, Any]:
"""Collect comprehensive system metrics."""
# Update system metrics
system_resources = health_monitor.check_system_resources()
# Collect performance metrics
performance_data = {
"avg_response_time": performance_metrics.get_avg_response_time(),
"p95_response_time": performance_metrics.get_p95_response_time(),
"throughput": performance_metrics.get_throughput(),
"error_rate": performance_metrics.get_error_rate()
}
# Check for alerts
alerts = alert_manager.check_thresholds({
"response_time_p95": performance_data["p95_response_time"],
"error_rate": performance_data["error_rate"],
"cpu_usage": system_resources["cpu_percent"],
"memory_usage": system_resources["memory_percent"],
"disk_usage": system_resources["disk_percent"]
})
return {
"performance": performance_data,
"system": system_resources,
"alerts": alerts,
"timestamp": datetime.utcnow()
} |