Health Check Endpoints
Two health check endpoints are available for monitoring and orchestration tools.Basic Health Check (/healthz)
Lightweight endpoint that returns immediately without external dependencies:
200 OK
Reference: app/__init__.py:61-63
Use
/healthz for load balancer health checks or liveness probes in Kubernetes.Readiness Check (/readyz)
Checks database connectivity to verify the application is ready to serve traffic:
200 OK
Degraded (database unavailable):
503 Service Unavailable
Reference: app/__init__.py:65-72
Use
/readyz for readiness probes in orchestration systems. The application should not receive traffic if this returns 503.Implementation
The readiness check performs a lightweight database query:Logging Configuration
Environment-Based Logging
Logging behavior is controlled by theLOG_TO_STDOUT environment variable:
config.py:16
Stdout Logging (Production)
WhenLOG_TO_STDOUT=true, logs are written to stdout at INFO level:
app/__init__.py:182-185
Stdout logging is recommended for:
- Docker/container deployments
- Cloud platforms (Heroku, AWS, GCP, Azure)
- Kubernetes/orchestrated environments
- Centralized logging systems
File-Based Logging (Development)
WhenLOG_TO_STDOUT=false, logs are written to app.log:
app/__init__.py:187-192
Gunicorn Logging
Gunicorn has separate access and error logs:gunicorn.conf.py:31-34
Control log level via environment:
Access Log Format
Gunicorn access logs include:%(h)s- Remote address%(t)s- Timestamp%(r)s- Request line (method, path, protocol)%(s)s- Status code%(b)s- Response size%(D)s- Request duration (microseconds)
Monitoring Metrics
Worker Process Lifecycle
Gunicorn logs worker lifecycle events:gunicorn.conf.py:60-82
Worker Restart Policy
Workers automatically restart after handling requests to prevent memory leaks:gunicorn.conf.py:27-28
Monitoring Integration
Kubernetes Probes
Example Kubernetes deployment with health checks:Docker Health Check
Add health check to Dockerfile:Uptime Monitoring
Use external monitoring services to ping health endpoints:- UptimeRobot: Monitor
/healthzevery 5 minutes - Pingdom: HTTP check on
/healthz - StatusCake: Monitor
/readyzfor database health - Datadog: Custom check using
/readyz
Error Handling and Logging
Error Handlers
Custom error handlers log exceptions and render templates:app/__init__.py:78-81
Request Logging
Application logs include:- Request method and path
- Response status code
- Request duration
- User IP address (via ProxyFix)
- User agent
Database Query Logging
Enable SQLAlchemy query logging for debugging:Log Aggregation
Centralized Logging
When using stdout logging, integrate with log aggregation services:Kubernetes with Elasticsearch
Logs automatically collected by node-level logging agents (Fluentd, Fluent Bit) and sent to Elasticsearch.
Structured Logging
For better log parsing, consider structured logging with JSON:Performance Monitoring
Application Performance Monitoring (APM)
Integrate APM tools for detailed performance insights:- New Relic: Python agent for Flask
- Datadog APM: Distributed tracing
- Sentry: Error tracking and performance monitoring
- Elastic APM: Open-source APM solution
Custom Metrics
Track custom application metrics:Security Logging
Rate Limiting Events
Flask-Limiter logs rate limit violations:app/__init__.py:21-26
Monitor logs for excessive rate limit hits, which may indicate:
- Malicious activity
- Misconfigured clients
- Need to adjust limits
Authentication Failures
Log failed login attempts for security monitoring:Alerting
Set up alerts based on monitoring data:Health check failures
Health check failures
Alert if
/readyz returns 503 for more than 2 consecutive checks.High error rate
High error rate
Alert if 5xx error rate exceeds 1% of total requests.
Worker restarts
Worker restarts
Alert if Gunicorn workers restart more than 10 times per hour.
Database connection errors
Database connection errors
Alert if database connection pool exhaustion occurs.
Disk space
Disk space
Alert if upload directory (
app/static/uploads/) exceeds 80% capacity.Next Steps
Production Deployment
Review production deployment guide
Database Migrations
Manage database schema changes