function health
Flask route handler that provides a health check endpoint returning the application's operational status, chat engine availability, and current timestamp.
/tf/active/vicechatdev/vice_ai/app.py
1657 - 1663
simple
Purpose
This endpoint serves as a monitoring and diagnostic tool for the application. It allows external systems, load balancers, or monitoring services to verify that the Flask application is running and responsive. It also reports the availability status of the chat_engine component and provides a timestamp for tracking when the health check was performed. This is commonly used in production environments for uptime monitoring, container orchestration health probes (like Kubernetes liveness/readiness probes), and service discovery.
Source Code
def health():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'chat_engine': 'available' if chat_engine else 'unavailable',
'timestamp': datetime.now().isoformat()
})
Return Value
Returns a Flask JSON response object containing a dictionary with three keys: 'status' (string, always 'healthy' if the endpoint responds), 'chat_engine' (string, either 'available' or 'unavailable' depending on whether the chat_engine variable is truthy), and 'timestamp' (string, ISO 8601 formatted datetime of when the check was performed). The HTTP status code is 200 by default.
Dependencies
flask
Required Imports
from flask import jsonify
from datetime import datetime
Usage Example
from flask import Flask, jsonify
from datetime import datetime
app = Flask(__name__)
chat_engine = None # or your actual chat engine instance
@app.route('/health')
def health():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'chat_engine': 'available' if chat_engine else 'unavailable',
'timestamp': datetime.now().isoformat()
})
if __name__ == '__main__':
app.run(debug=True)
# To test the endpoint:
# curl http://localhost:5000/health
# Response: {"chat_engine":"unavailable","status":"healthy","timestamp":"2024-01-15T10:30:45.123456"}
Best Practices
- Ensure the chat_engine variable is properly initialized in the module scope before the Flask app starts
- Consider adding try-catch blocks if chat_engine availability check could raise exceptions
- For production use, consider adding more detailed health metrics (database connectivity, memory usage, etc.)
- This endpoint should not require authentication to allow monitoring systems easy access
- Consider adding HTTP status codes other than 200 if critical components are unavailable (e.g., 503 Service Unavailable)
- The endpoint returns 'healthy' status even when chat_engine is unavailable - consider if this is appropriate for your use case
- For Kubernetes deployments, this can be used as both liveness and readiness probe
- Consider rate limiting if exposed publicly to prevent abuse
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function health_v1 92.4% similar
-
function health_check 90.8% similar
-
function system_status 73.4% similar
-
function api_task_status 62.2% similar
-
function analysis_chat 60.6% similar