FastAPI Example Application
The Greenfield Cluster includes a fully instrumented FastAPI application demonstrating best practices for cloud-native applications.
Overview
The FastAPI example application showcases:
- OpenTelemetry Integration: Automatic distributed tracing
- Prometheus Metrics: Custom and automatic metrics
- Database Connectivity: Examples for Redis, PostgreSQL, MySQL, MongoDB
- Kafka Integration: Event publishing and consuming
- Health Checks: Kubernetes-ready health endpoints
- Istio Integration: Service mesh sidecar injection
Architecture
Features
| Feature | Implementation |
|---|---|
| Framework | FastAPI (async Python) |
| Tracing | OpenTelemetry automatic instrumentation |
| Metrics | Prometheus client library |
| Health Checks | /health endpoint |
| API Documentation | Auto-generated OpenAPI/Swagger |
Endpoints
Health Check
Returns application health status.
Database Examples
# Redis
GET /redis
POST /redis
# PostgreSQL
GET /postgres
POST /postgres
# MySQL
GET /mysql
# MongoDB
GET /mongodb
POST /mongodb
Kafka Example
Publishes a message to Kafka.
Metrics
Prometheus metrics endpoint.
Usage
Accessing the Application
# Port forward
kubectl port-forward -n greenfield svc/fastapi-app 8000:8000
# Test endpoints
curl http://localhost:8000/health
curl http://localhost:8000/docs # Swagger UI
API Documentation
FastAPI provides automatic interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
Implementation Examples
Basic Endpoint with Tracing
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
app = FastAPI()
FastAPIInstrumentor.instrument_app(app)
@app.get("/")
async def root():
# Automatically traced
return {"message": "Hello World"}
Database Connection
import redis
from fastapi import FastAPI
app = FastAPI()
@app.get("/redis")
async def test_redis():
r = redis.Redis(host='redis-master.greenfield.svc.cluster.local')
r.set('key', 'value')
value = r.get('key')
return {"value": value.decode()}
Custom Metrics
from prometheus_client import Counter, Histogram
from fastapi import FastAPI
app = FastAPI()
REQUEST_COUNT = Counter('http_requests_total', 'Total HTTP requests')
REQUEST_DURATION = Histogram('http_request_duration_seconds', 'HTTP request duration')
@app.get("/api/users")
async def get_users():
REQUEST_COUNT.inc()
with REQUEST_DURATION.time():
# Your logic here
return {"users": []}
Building and Deploying
Build Image
cd apps/fastapi-example
# Build image
docker build -t fastapi-example:latest .
# For Minikube
minikube image load fastapi-example:latest
# For cloud, push to registry
docker tag fastapi-example:latest your-registry/fastapi-example:latest
docker push your-registry/fastapi-example:latest
Deploy
# Update image in manifests
# kustomize/base/fastapi-app/deployment.yaml
# Deploy
kubectl apply -k kustomize/base/
Monitoring
View Traces
- Access Jaeger UI
- Select "fastapi-app" service
- View traces for requests
View Metrics
- Access Grafana
- Explore metrics starting with
http_orfastapi_ - Create dashboards
View Logs
# View application logs
kubectl logs -n greenfield deployment/fastapi-app
# Follow logs
kubectl logs -f -n greenfield deployment/fastapi-app
Development
Local Development
cd apps/fastapi-example
# Create virtual environment
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install dependencies
pip install -r requirements.txt
# Run locally
uvicorn main:app --reload
# Access at http://localhost:8000
Testing
Best Practices Demonstrated
- Async/Await: Proper async implementation
- Dependency Injection: FastAPI dependencies for shared resources
- Error Handling: Proper exception handling and logging
- Connection Pooling: Efficient database connections
- Configuration: Environment-based configuration
- Observability: Comprehensive tracing and metrics
- Health Checks: Kubernetes-ready probes
Customization
Use this as a template for your own applications:
- Copy the application directory
- Modify endpoints for your use case
- Update database models
- Add business logic
- Update Kubernetes manifests