function health_check
A Flask route handler that provides a health check endpoint returning the application's status and current timestamp.
/tf/active/vicechatdev/leexi/app.py
411 - 413
simple
Purpose
This endpoint is used for monitoring and health checking purposes, typically by load balancers, orchestration systems (like Kubernetes), or monitoring tools to verify that the Flask application is running and responsive. It returns a simple JSON response indicating the service is healthy along with the current server time.
Source Code
def health_check():
"""Health check endpoint"""
return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()})
Return Value
Returns a Flask JSON response object containing a dictionary with two keys: 'status' (string value 'healthy') and 'timestamp' (ISO 8601 formatted datetime string representing the current server time). The response has a 200 OK HTTP status code by default.
Dependencies
flaskdatetime
Required Imports
from flask import Flask, jsonify
from datetime import datetime
Usage Example
from flask import Flask, jsonify
from datetime import datetime
app = Flask(__name__)
@app.route('/health')
def health_check():
return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()})
if __name__ == '__main__':
app.run(debug=True)
# Access the endpoint:
# GET http://localhost:5000/health
# Response: {"status": "healthy", "timestamp": "2024-01-15T10:30:45.123456"}
Best Practices
- This endpoint should remain lightweight and fast to respond, as it's typically called frequently by monitoring systems
- Consider adding additional health metrics like database connectivity, external service availability, or memory usage for more comprehensive health checks
- The endpoint should not require authentication to allow monitoring tools easy access
- Use HTTP GET method (default) as health checks should be idempotent and have no side effects
- Consider adding response caching headers to reduce server load if called very frequently
- In production, you may want to add more detailed status information (e.g., 'degraded' status) based on system checks
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function health 90.8% similar
-
function health_v1 88.4% similar
-
function system_status 72.0% similar
-
function api_task_status 59.4% similar
-
function test_flask_routes 59.1% similar