🔍 Code Extractor

function health_check

Maturity: 32

A Flask route handler that provides a health check endpoint returning the application's status and current timestamp.

File:
/tf/active/vicechatdev/leexi/app.py
Lines:
411 - 413
Complexity:
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

  • flask
  • datetime

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function health 90.8% similar

    Flask route handler that provides a health check endpoint returning the application's operational status, chat engine availability, and current timestamp.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function health_v1 88.4% similar

    Flask route handler that provides a health check endpoint returning the operational status of the application and its core components (RAG engine and document indexer).

    From: /tf/active/vicechatdev/docchat/app.py
  • function system_status 72.0% similar

    Flask API endpoint that returns comprehensive system status information including database connectivity, authentication state, and feature availability.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function api_task_status 59.4% similar

    Flask API endpoint that retrieves and returns the status of asynchronous tasks (chat or indexing operations) by task ID.

    From: /tf/active/vicechatdev/docchat/app.py
  • function test_flask_routes 59.1% similar

    A test function that validates Flask application routes are properly configured by checking for required endpoints.

    From: /tf/active/vicechatdev/docchat/test_model_selection.py
← Back to Browse