Spaces:
Sleeping
Sleeping
File size: 12,149 Bytes
72fc0ee | 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 | """
Health Check and Readiness Probes for Production Deployment
Provides comprehensive health monitoring endpoints.
"""
import time
import psutil
import requests
from typing import Dict, Any, List
from datetime import datetime
from pathlib import Path
class HealthChecker:
"""Comprehensive health checking system."""
def __init__(self):
"""Initialize health checker."""
self.start_time = time.time()
self.last_check_time = None
self.check_history: List[Dict[str, Any]] = []
def check_health(self) -> Dict[str, Any]:
"""
Perform comprehensive health check.
Returns:
Health status dictionary
"""
self.last_check_time = datetime.now()
health_checks = {
'status': 'healthy',
'timestamp': self.last_check_time.isoformat(),
'uptime_seconds': int(time.time() - self.start_time),
'checks': {}
}
# System resource checks
health_checks['checks']['system'] = self._check_system_resources()
# Application checks
health_checks['checks']['application'] = self._check_application()
# Dependencies checks
health_checks['checks']['dependencies'] = self._check_dependencies()
# Data checks
health_checks['checks']['data'] = self._check_data_directories()
# Determine overall status
failed_checks = [
check_name for check_name, check_result in health_checks['checks'].items()
if check_result.get('status') != 'healthy'
]
if failed_checks:
health_checks['status'] = 'unhealthy'
health_checks['failed_checks'] = failed_checks
# Store in history (keep last 100)
self.check_history.append(health_checks)
if len(self.check_history) > 100:
self.check_history.pop(0)
return health_checks
def _check_system_resources(self) -> Dict[str, Any]:
"""Check system resources."""
try:
cpu_percent = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
disk = psutil.disk_usage('/')
# Thresholds
cpu_critical = 90
memory_critical = 90
disk_critical = 90
status = 'healthy'
warnings = []
if cpu_percent > cpu_critical:
status = 'unhealthy'
warnings.append(f"CPU usage critical: {cpu_percent:.1f}%")
elif cpu_percent > 75:
warnings.append(f"CPU usage high: {cpu_percent:.1f}%")
if memory.percent > memory_critical:
status = 'unhealthy'
warnings.append(f"Memory usage critical: {memory.percent:.1f}%")
elif memory.percent > 75:
warnings.append(f"Memory usage high: {memory.percent:.1f}%")
if disk.percent > disk_critical:
status = 'unhealthy'
warnings.append(f"Disk usage critical: {disk.percent:.1f}%")
elif disk.percent > 80:
warnings.append(f"Disk usage high: {disk.percent:.1f}%")
return {
'status': status,
'cpu_percent': cpu_percent,
'memory_percent': memory.percent,
'disk_percent': disk.percent,
'warnings': warnings,
}
except Exception as e:
return {
'status': 'unhealthy',
'error': str(e)
}
def _check_application(self) -> Dict[str, Any]:
"""Check application health."""
try:
# Check if critical modules can be imported
import quantlib_pro
from quantlib_pro.portfolio import PortfolioOptimizer
from quantlib_pro.risk import RiskCalculator
return {
'status': 'healthy',
'modules_loaded': True,
}
except ImportError as e:
return {
'status': 'unhealthy',
'error': f"Module import failed: {str(e)}"
}
except Exception as e:
return {
'status': 'unhealthy',
'error': str(e)
}
def _check_dependencies(self) -> Dict[str, Any]:
"""Check external dependencies."""
dependencies_status = {
'status': 'healthy',
'checks': {}
}
# Check Redis (if configured)
try:
import os
redis_host = os.getenv('REDIS_HOST')
if redis_host:
import redis
r = redis.Redis(host=redis_host, port=6379, socket_connect_timeout=2)
r.ping()
dependencies_status['checks']['redis'] = 'connected'
except Exception as e:
dependencies_status['checks']['redis'] = f'error: {str(e)}'
# Redis is optional, don't fail health check
# Check internet connectivity (for market data)
try:
response = requests.get('https://www.google.com', timeout=5)
dependencies_status['checks']['internet'] = 'connected'
except Exception as e:
dependencies_status['checks']['internet'] = f'error: {str(e)}'
dependencies_status['status'] = 'degraded'
return dependencies_status
def _check_data_directories(self) -> Dict[str, Any]:
"""Check data directories."""
try:
required_dirs = ['data', 'data/cache', 'data/logs']
missing_dirs = []
for dir_path in required_dirs:
if not Path(dir_path).exists():
missing_dirs.append(dir_path)
if missing_dirs:
return {
'status': 'unhealthy',
'missing_directories': missing_dirs
}
# Check write permissions
test_file = Path('data/cache/.health_check_test')
try:
test_file.write_text('test')
test_file.unlink()
writeable = True
except Exception:
writeable = False
return {
'status': 'healthy' if writeable else 'unhealthy',
'writeable': writeable,
}
except Exception as e:
return {
'status': 'unhealthy',
'error': str(e)
}
def check_readiness(self) -> Dict[str, Any]:
"""
Check if application is ready to serve traffic.
Returns:
Readiness status
"""
# Application is ready if:
# 1. System resources are not critical
# 2. Application modules loaded
# 3. Required directories exist
health = self.check_health()
ready = (
health['status'] == 'healthy' and
health['checks']['application']['status'] == 'healthy'
)
return {
'ready': ready,
'status': 'ready' if ready else 'not_ready',
'timestamp': datetime.now().isoformat(),
}
def check_liveness(self) -> Dict[str, Any]:
"""
Check if application is alive (simpler than health check).
Returns:
Liveness status
"""
# Application is alive if it can respond
return {
'alive': True,
'status': 'alive',
'timestamp': datetime.now().isoformat(),
'uptime_seconds': int(time.time() - self.start_time),
}
def get_metrics(self) -> Dict[str, Any]:
"""
Get health metrics for monitoring.
Returns:
Metrics dictionary
"""
if not self.check_history:
return {}
# Calculate success rate
total_checks = len(self.check_history)
healthy_checks = sum(
1 for check in self.check_history
if check['status'] == 'healthy'
)
success_rate = (healthy_checks / total_checks * 100) if total_checks > 0 else 0
# Get latest resource usage
latest = self.check_history[-1]
system_check = latest['checks'].get('system', {})
return {
'total_checks': total_checks,
'success_rate': success_rate,
'uptime_seconds': int(time.time() - self.start_time),
'last_check': self.last_check_time.isoformat() if self.last_check_time else None,
'current_cpu_percent': system_check.get('cpu_percent'),
'current_memory_percent': system_check.get('memory_percent'),
'current_disk_percent': system_check.get('disk_percent'),
}
# Global health checker instance
_health_checker = HealthChecker()
def get_health_checker() -> HealthChecker:
"""Get global health checker instance."""
return _health_checker
# Flask endpoints (if using REST API)
def create_health_endpoints(app):
"""
Create health check endpoints for Flask app.
Args:
app: Flask application
"""
@app.route('/health')
def health():
"""Full health check endpoint."""
result = _health_checker.check_health()
status_code = 200 if result['status'] == 'healthy' else 503
return result, status_code
@app.route('/health/ready')
def ready():
"""Readiness probe endpoint."""
result = _health_checker.check_readiness()
status_code = 200 if result['ready'] else 503
return result, status_code
@app.route('/health/live')
def live():
"""Liveness probe endpoint."""
result = _health_checker.check_liveness()
return result, 200
@app.route('/metrics')
def metrics():
"""Metrics endpoint for Prometheus."""
metrics_data = _health_checker.get_metrics()
# Convert to Prometheus format
lines = [
'# HELP quantlib_health_checks_total Total number of health checks',
'# TYPE quantlib_health_checks_total counter',
f'quantlib_health_checks_total {metrics_data.get("total_checks", 0)}',
'',
'# HELP quantlib_health_success_rate Health check success rate',
'# TYPE quantlib_health_success_rate gauge',
f'quantlib_health_success_rate {metrics_data.get("success_rate", 0)}',
'',
'# HELP quantlib_uptime_seconds Application uptime in seconds',
'# TYPE quantlib_uptime_seconds counter',
f'quantlib_uptime_seconds {metrics_data.get("uptime_seconds", 0)}',
'',
'# HELP quantlib_cpu_percent CPU usage percentage',
'# TYPE quantlib_cpu_percent gauge',
f'quantlib_cpu_percent {metrics_data.get("current_cpu_percent", 0)}',
'',
'# HELP quantlib_memory_percent Memory usage percentage',
'# TYPE quantlib_memory_percent gauge',
f'quantlib_memory_percent {metrics_data.get("current_memory_percent", 0)}',
'',
'# HELP quantlib_disk_percent Disk usage percentage',
'# TYPE quantlib_disk_percent gauge',
f'quantlib_disk_percent {metrics_data.get("current_disk_percent", 0)}',
]
return '\n'.join(lines), 200, {'Content-Type': 'text/plain; charset=utf-8'}
# Streamlit health check (simpler version)
def streamlit_health_check() -> bool:
"""
Simple health check for Streamlit.
Returns:
True if healthy, False otherwise
"""
try:
checker = get_health_checker()
result = checker.check_health()
return result['status'] == 'healthy'
except Exception:
return False
|