Health Checks
Simple Health Endpoint
Section titled “Simple Health Endpoint”Python
Section titled “Python”@app.route('/health')def health(): return {'status': 'healthy'}, 200Node.js
Section titled “Node.js”app.get('/health', (req, res) => { res.json({ status: 'healthy' });});http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(`{"status":"healthy"}`))})Docker Health Check
Section titled “Docker Health Check”Dockerfile
Section titled “Dockerfile”HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1docker-compose.yml
Section titled “docker-compose.yml”services: snapcode: healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8000/health"] interval: 30s timeout: 3s retries: 3 start_period: 5s# Check health statusdocker inspect --format='{{.State.Health.Status}}' snapcodeNginx Health Check
Section titled “Nginx Health Check”location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain;}Load Balancer Health Checks
Section titled “Load Balancer Health Checks”HAProxy
Section titled “HAProxy”backend snapcode_backend option httpchk GET /health http-check expect status 200 server app1 localhost:8000 checkNginx Upstream
Section titled “Nginx Upstream”upstream backend { server localhost:8000 max_fails=3 fail_timeout=30s; server localhost:8001 max_fails=3 fail_timeout=30s;}Monitoring Script
Section titled “Monitoring Script”#!/bin/bashURL="http://localhost:8000/health"RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ $RESPONSE -ne 200 ]; then echo "Service unhealthy: $RESPONSE" # Send alert systemctl restart snapcodefiCron:
* * * * * /path/to/health-check.shComprehensive Health Check
Section titled “Comprehensive Health Check”@app.route('/health')def health(): checks = { 'database': check_db(), 'cache': check_redis(), 'disk': check_disk_space(), }
status = 'healthy' if all(checks.values()) else 'unhealthy' code = 200 if status == 'healthy' else 503
return {'status': status, 'checks': checks}, code