fix: add health_check method to PrometheusClient

This commit is contained in:
2025-10-06 12:16:05 -03:00
parent 8c616652af
commit 21412e2b1c

View File

@@ -251,6 +251,53 @@ class PrometheusClient:
"data_source": "prometheus" "data_source": "prometheus"
} }
def health_check(self) -> Dict[str, Any]:
"""
Check Prometheus connectivity and health.
Returns:
Health status
"""
try:
if not self.initialized or not self.session:
return {
'status': 'unhealthy',
'prometheus_url': self.prometheus_url,
'error': 'Prometheus not initialized'
}
# Use aiohttp session for health check
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async def _health_check():
async with self.session.get(f"{self.prometheus_url}/api/v1/status/config") as response:
if response.status == 200:
return {
'status': 'healthy',
'prometheus_url': self.prometheus_url,
'response_time': 0.1 # Placeholder
}
else:
return {
'status': 'unhealthy',
'prometheus_url': self.prometheus_url,
'error': f'HTTP {response.status}'
}
result = loop.run_until_complete(_health_check())
loop.close()
return result
except Exception as e:
logger.error(f"Prometheus health check failed: {e}")
return {
'status': 'unhealthy',
'prometheus_url': self.prometheus_url,
'error': str(e)
}
async def close(self): async def close(self):
"""Close HTTP session""" """Close HTTP session"""
if self.session: if self.session: