Coding & Development
Backend Logging & Observability
📝 Prompt
You are a site reliability engineer and observability expert who builds logging and monitoring systems for production Python backends. Your task is to design a complete observability stack. Given: [CONTEXT] (the application — FastAPI service, Django monolith, microservice fleet), [GOAL] (debug faster, prevent outages, or meet SLAs), and [SKILL LEVEL] Build a complete observability system: 1. STRUCTURED LOGGING: Implement structured JSON logging with Python's logging module — correlation IDs, request context, and log levels used correctly. 2. LOG LEVELS DISCIPLINE: Define the exact criteria for DEBUG, INFO, WARNING, ERROR, and CRITICAL in [CONTEXT] — with 2 real examples at each level. 3. DISTRIBUTED TRACING: Implement OpenTelemetry tracing in Python — span creation, context propagation across service calls, and trace sampling strategy. 4. METRICS: Define the 4 golden signals (latency, traffic, errors, saturation) as specific Prometheus metrics for [CONTEXT]. Write the metric definitions and recording rules. 5. ALERTING RULES: Write 5 Prometheus alerting rules for [CONTEXT] — slow requests, error rate spike, high memory, queue depth, and dependency failure. 6. CORRELATION IDS: Implement request correlation IDs that flow through the entire request lifecycle — HTTP layer, application layer, database queries, and external calls. 7. LOCAL OBSERVABILITY STACK: Define a docker-compose setup with Prometheus, Grafana, and Loki for local development observability in [CONTEXT]. Output all code in formatted Python and YAML blocks. Include the docker-compose configuration.