A production-ready FastAPI-based Python backend template with complete observability stack and modern Python tooling.
- FastAPI for high-performance API development
- UV for fast Python package management
- Pydantic Settings for configuration management
- Production-ready Docker setup with complete observability
- Complete OpenTelemetry integration with Jaeger tracing
- Centralized logging with Fluent Bit → Loki → Grafana
- Pytest for testing with comprehensive test coverage
- Ruff for linting and formatting
- Security hardening and best practices
FastAPI Backend → OTEL Collector → Jaeger → Jaeger UI
↓
Fluent Bit → Loki → Grafana
Complete observability stack with:
- Distributed Tracing: OpenTelemetry → Jaeger
- Centralized Logging: Fluent Bit → Loki → Grafana
- Metrics & Visualization: Grafana dashboards
- Real-time Monitoring: Health checks and alerts
- Python >= 3.11
- UV package manager
- Docker & Docker Compose (for production deployment)
# Setup environment
make setup-env
# Install dependencies
uv venv
uv pip install -e ".[dev]"
# Run locally (recommended for development)
make run
# Run tests
make test
# Lint and format
make lint
make format# Generate secure secret key
export SECRET_KEY="$(make generate-secret)"
# Deploy production environment
make deploy
# Check health
make health
# View logs
make logsmake run # Run locally with Python (hot reload)
make test # Run tests with coverage
make lint # Lint code with ruff
make format # Format code with ruff
make setup-env # Create .env from templatemake deploy # Complete production deployment
make compose # Start production containers
make compose-down # Stop containers
make health # Check application health
make logs # View container logsmake docker-build # Build production Docker image
make docker-run # Run production container locally
make status # Check container status
make urls # Show all service URLsmake generate-secret # Generate secure secret key
make check-security # Check security configuration
make info # Show all available commandsUses .env file for local development settings:
# Setup environment file
make setup-env
# Edit for local development
vim .envKey configuration variables:
# Application
APP_NAME=FastAPI Backend
APP_VERSION=1.0.0
DEBUG=true
ENVIRONMENT=development
# OpenTelemetry
OTEL_SERVICE_NAME=fastapi-backend-dev
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:14317Uses .env.example as template with environment variable overrides:
# Set production secrets
export SECRET_KEY="$(make generate-secret)"
export DATABASE_URL="postgresql://user:pass@db:5432/myapp"
# Deploy
make deployProduction configuration:
# Application
APP_NAME=FastAPI Backend
DEBUG=false
ENVIRONMENT=production
# OpenTelemetry
OTEL_SERVICE_NAME=fastapi-backend
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317- FastAPI backend with complete observability
- Health checks and monitoring endpoints
- Security hardening with non-root user
- Resource limits and restart policies
- JSON structured logging
- Version: 1.57.0 (stable)
- Distributed tracing visualization
- Performance monitoring and debugging
- Request flow analysis
- Trace correlation with logs
- Version: 0.100.0 (stable contrib)
- OpenTelemetry data collection and processing
- Trace aggregation and forwarding
- Memory limiting and batch processing
- Version: 10.4.2 (LTS)
- Log visualization and dashboards
- Integrated with Loki for log queries
- Connected to Jaeger for trace correlation
- Pre-built "FastAPI Backend Logs" dashboard
- Default credentials: admin/admin
- Version: 3.0.0 (stable)
- Log aggregation and storage
- Efficient log indexing and querying
- Integration with Grafana for visualization
- Service-specific log filtering
- Version: 3.0.7 (stable)
- Log collection from Docker containers
- JSON log parsing and enrichment
- Forwarding logs to Loki with labels
GET /- Root endpoint with application infoGET /health- Health check endpointGET /settings- Configuration endpoint (non-sensitive data)GET /docs- Interactive API documentation (development only)
Example responses:
// GET /
{
"message": "Hello World",
"app_name": "FastAPI Backend",
"version": "1.0.0",
"environment": "production"
}
// GET /health
{
"status": "healthy",
"app_name": "FastAPI Backend",
"version": "1.0.0",
"environment": "production"
}Complete distributed tracing with OpenTelemetry:
# Access Jaeger UI
open http://localhost:16686
# Generate traces
curl http://localhost:8000/
curl http://localhost:8000/healthStructured logging with Fluent Bit → Loki → Grafana:
# Access Grafana for log visualization
open http://localhost:3000
# Default credentials: admin/admin
# View pre-built dashboard
# Navigate to: Dashboards → FastAPI Backend Logs
# Query logs directly from Loki
curl "http://localhost:3100/loki/api/v1/query_range?query={job=\"fluentbit\"}"Ready-to-use queries for log analysis:
# All backend logs
{job="fluentbit", service_name="fastapi-backend"} | json
# Error logs only
{job="fluentbit", service_name="fastapi-backend"} | json | level="ERROR"
# Request logs with formatting
{job="fluentbit", service_name="fastapi-backend"} | json | line_format "{{.method}} {{.url}} → {{.status_code}} ({{.duration_ms}}ms)"
# Request rate metrics
sum(rate({job="fluentbit", service_name="fastapi-backend"} | json [5m]))
# Check application health
make health
# Detailed health check
curl http://localhost:8000/health
# Check all container status
make status
# Show all service URLs
make urls- Non-root containers: All containers run as non-root users
- Resource limits: CPU and memory limits for stability
- Secret management: Secure secret key generation
- Environment validation: Production settings validation
- Network isolation: Custom Docker network (
fastapi-backend-network) - Health checks: Built-in health monitoring
- Structured logging: JSON logs with request correlation
# Daily development
make run # Start local development server
# Make code changes (automatic reload enabled)
make test # Run tests with coverage
make lint # Check code quality
# Test production build
make deploy # Test production deployment
make health # Verify deployment
make logs # Monitor logs
make compose-down # Stop containers# Generate secure secrets
export SECRET_KEY="$(make generate-secret)"
# Set production database (optional)
export DATABASE_URL="postgresql://user:pass@prod-db:5432/myapp"
# Set log level (optional)
export LOG_LEVEL="WARNING"# Complete production deployment
make deploy
# Verify deployment
make health
make status
# Access services
open http://localhost:8000 # API
open http://localhost:16686 # Jaeger
open http://localhost:3000 # Grafana
# Monitor logs
make logs# Check security configuration
make check-security
# Ensure:
# ✅ SECRET_KEY is set securely
# ✅ DEBUG is disabled in production
# ✅ Production database is configured
# ✅ Log level is appropriate
# ✅ All containers run as non-root├── app/ # Application code
│ ├── main.py # FastAPI application
│ ├── core/ # Core functionality
│ │ ├── settings.py # Configuration management
│ │ ├── middleware.py # Custom middleware
│ │ └── logging.py # Logging configuration
│ └── api/ # API routes
├── tests/ # Test files
├── scripts/ # Configuration scripts
│ ├── opentelemetry/ # OTEL configuration
│ │ └── otel-collector-config.yaml # OTEL Collector config
│ └── logging/ # Logging configuration
│ ├── fluent-bit.conf # Fluent Bit config
│ ├── grafana-datasources.yaml # Grafana datasources
│ └── dashboards/ # Pre-built dashboards
├── Dockerfile # Production-ready Docker image
├── docker-compose.yml # Production services
├── .env.example # Production template
├── run.py # Application entry point
├── Makefile # Development commands
├── pyproject.toml # Python project configuration
└── README.md # This file
# Run all tests with coverage
make test
# Run specific test file
uv run pytest tests/test_main.py -v
# Run with detailed coverage report
uv run pytest --cov=app --cov-report=html tests/
# Run tests in watch mode (development)
uv run pytest-watch# Check container status
make status
# View all logs
make logs
# Restart services
make compose-down && make deploy# Test health endpoints
make health
curl http://localhost:13133 # OTEL Collector health
# Check health configuration
docker compose config | grep -A 5 healthcheck# Check log pipeline
curl "http://localhost:3100/ready" # Loki readiness
curl "http://localhost:3100/loki/api/v1/label/job/values" # Available jobs
# Test log queries
curl "http://localhost:3100/loki/api/v1/query_range?query={job=\"fluentbit\"}"
# Check Fluent Bit status
docker compose logs fluent-bit# Check application logs
make logs
# Test configuration
curl http://localhost:8000/settings
# Debug environment
docker compose exec api env | grep -E "(APP_|OTEL_|DEBUG)"- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
make test - Lint code:
make lint - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Submit a pull request
- API Documentation - Interactive API docs (when running)
- Package Name:
py-fastapi-backend - Service Name:
fastapi-backend - Network:
fastapi-backend-network - Python Version: 3.11+
- FastAPI Version: 0.116.1+
This project is licensed under the MIT License - see the LICENSE file for details.
Production-ready FastAPI backend with complete observability! 🚀
Ready for deployment with monitoring, logging, tracing, and security best practices built-in.