Python Observability
Unverified●30/40Claude Code◐PartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
Cursor◐PartialPlain prose you can paste in — but no Cursor rules file
Codex◐PartialPlain prose you can paste in — but no AGENTS.md
Gemini CLI◐PartialPlain prose you can paste in
Copilot◐PartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add python-observabilityWho is stuck, and on what
Python observability patterns including structured logging, metrics, and distributed tracing. Use when adding logging, implementing metrics collection, setting up tracing, or debugging production systems.
The whole source
Frontmatter — 2 properties
| name | python-observability |
|---|---|
| description | Python observability patterns including structured logging, metrics, and distributed tracing. Use when adding logging, implementing metrics collection, setting up tracing, or debugging production systems. |
| 1 | --- |
| 2 | name: python-observability |
| 3 | description: Python observability patterns including structured logging, metrics, and distributed tracing. Use when adding logging, implementing metrics collection, setting up tracing, or debugging production systems. |
| 4 | ---A5 — No allowed-tools declared — no way to tell what this skill may touch |
| 5 | |
| 6 | # Python Observability |
| 7 | |
| 8 | Instrument Python applications with structured logs, metrics, and traces. When something breaks in production, you need to answer "what, where, and why" without deploying new code. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | - Adding structured logging to applications |
| 13 | - Implementing metrics collection with Prometheus |
| 14 | - Setting up distributed tracing across services |
| 15 | - Propagating correlation IDs through request chains |
| 16 | - Debugging production issues |
| 17 | - Building observability dashboards |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### 1. Structured Logging |
| 22 | |
| 23 | Emit logs as JSON with consistent fields for production environments. Machine-readable logs enable powerful queries and alerts. For local development, consider human-readable formats. |
| 24 | |
| 25 | ### 2. The Four Golden Signals |
| 26 | |
| 27 | Track latency, traffic, errors, and saturation for every service boundary. |
| 28 | |
| 29 | ### 3. Correlation IDs |
| 30 | |
| 31 | Thread a unique ID through all logs and spans for a single request, enabling end-to-end tracing. |
| 32 | |
| 33 | ### 4. Bounded Cardinality |
| 34 | |
| 35 | Keep metric label values bounded. Unbounded labels (like user IDs) explode storage costs. |
| 36 | |
| 37 | ## Quick Start |
| 38 | |
| 39 | ```python |
| 40 | import structlog |
| 41 | |
| 42 | structlog.configure( |
| 43 | processors=[ |
| 44 | structlog.processors.TimeStamper(fmt="iso"), |
| 45 | structlog.processors.JSONRenderer(), |
| 46 | ], |
| 47 | ) |
| 48 | |
| 49 | logger = structlog.get_logger() |
| 50 | logger.info("Request processed", user_id="123", duration_ms=45) |
| 51 | ``` |
| 52 | |
| 53 | ## Fundamental Patterns |
| 54 | |
| 55 | ### Pattern 1: Structured Logging with Structlog |
| 56 | |
| 57 | Configure structlog for JSON output with consistent fields. |
| 58 | |
| 59 | ```python |
| 60 | import logging |
| 61 | import structlog |
| 62 | |
| 63 | def configure_logging(log_level: str = "INFO") -> None: |
| 64 | """Configure structured logging for the application.""" |
| 65 | structlog.configure( |
| 66 | processors=[ |
| 67 | structlog.contextvars.merge_contextvars, |
| 68 | structlog.processors.add_log_level, |
| 69 | structlog.processors.TimeStamper(fmt="iso"), |
| 70 | structlog.processors.StackInfoRenderer(), |
| 71 | structlog.processors.format_exc_info, |
| 72 | structlog.processors.JSONRenderer(), |
| 73 | ], |
| 74 | wrapper_class=structlog.make_filtering_bound_logger( |
| 75 | getattr(logging, log_level.upper()) |
| 76 | ), |
| 77 | context_class=dict, |
| 78 | logger_factory=structlog.PrintLoggerFactory(), |
| 79 | cache_logger_on_first_use=True, |
| 80 | ) |
| 81 | |
| 82 | # Initialize at application startup |
| 83 | configure_logging("INFO") |
| 84 | logger = structlog.get_logger() |
| 85 | ``` |
| 86 | |
| 87 | ### Pattern 2: Consistent Log Fields |
| 88 | |
| 89 | Every log entry should include standard fields for filtering and correlation. |
| 90 | |
| 91 | ```python |
| 92 | import structlog |
| 93 | from contextvars import ContextVar |
| 94 | |
| 95 | # Store correlation ID in context |
| 96 | correlation_id: ContextVar[str] = ContextVar("correlation_id", default="") |
| 97 | |
| 98 | logger = structlog.get_logger() |
| 99 | |
| 100 | def process_request(request: Request) -> Response: |
| 101 | """Process request with structured logging.""" |
| 102 | logger.info( |
| 103 | "Request received", |
| 104 | correlation_id=correlation_id.get(), |
| 105 | method=request.method, |
| 106 | path=request.path, |
| 107 | user_id=request.user_id, |
| 108 | ) |
| 109 | |
| 110 | try: |
| 111 | result = handle_request(request) |
| 112 | logger.info( |
| 113 | "Request completed", |
| 114 | correlation_id=correlation_id.get(), |
| 115 | status_code=200, |
| 116 | duration_ms=elapsed, |
| 117 | ) |
| 118 | return result |
| 119 | except Exception as e: |
| 120 | logger.error( |
| 121 | "Request failed", |
| 122 | correlation_id=correlation_id.get(), |
| 123 | error_type=type(e).__name__, |
| 124 | error_message=str(e), |
| 125 | ) |
| 126 | raise |
| 127 | ``` |
| 128 | |
| 129 | ### Pattern 3: Semantic Log Levels |
| 130 | |
| 131 | Use log levels consistently across the application. |
| 132 | |
| 133 | | Level | Purpose | Examples | |
| 134 | |-------|---------|----------| |
| 135 | | `DEBUG` | Development diagnostics | Variable values, internal state | |
| 136 | | `INFO` | Request lifecycle, operations | Request start/end, job completion | |
| 137 | | `WARNING` | Recoverable anomalies | Retry attempts, fallback used | |
| 138 | | `ERROR` | Failures needing attention | Exceptions, service unavailable | |
| 139 | |
| 140 | ```python |
| 141 | # DEBUG: Detailed internal information |
| 142 | logger.debug("Cache lookup", key=cache_key, hit=cache_hit) |
| 143 | |
| 144 | # INFO: Normal operational events |
| 145 | logger.info("Order created", order_id=order.id, total=order.total) |
| 146 | |
| 147 | # WARNING: Abnormal but handled situations |
| 148 | logger.warning( |
| 149 | "Rate limit approaching", |
| 150 | current_rate=950, |
| 151 | limit=1000, |
| 152 | reset_seconds=30, |
| 153 | ) |
| 154 | |
| 155 | # ERROR: Failures requiring investigation |
| 156 | logger.error( |
| 157 | "Payment processing failed", |
| 158 | order_id=order.id, |
| 159 | error=str(e), |
| 160 | payment_provider="stripe", |
| 161 | ) |
| 162 | ``` |
| 163 | |
| 164 | Never log expected behavior at `ERROR`. A user entering a wrong password is `INFO`, not `ERROR`. |
| 165 | |
| 166 | ### Pattern 4: Correlation ID Propagation |
| 167 | |
| 168 | Generate a unique ID at ingress and thread it through all operations. |
| 169 | |
| 170 | ```python |
| 171 | from contextvars import ContextVar |
| 172 | import uuid |
| 173 | import structlog |
| 174 | |
| 175 | correlation_id: ContextVar[str] = ContextVar("correlation_id", default="") |
| 176 | |
| 177 | def set_correlation_id(cid: str | None = None) -> str: |
| 178 | """Set correlation ID for current context.""" |
| 179 | cid = cid or str(uuid.uuid4()) |
| 180 | correlation_id.set(cid) |
| 181 | structlog.contextvars.bind_contextvars(correlation_id=cid) |
| 182 | return cid |
| 183 | |
| 184 | # FastAPI middleware example |
| 185 | from fastapi import Request |
| 186 | |
| 187 | async def correlation_middleware(request: Request, call_next): |
| 188 | """Middleware to set and propagate correlation ID.""" |
| 189 | # Use incoming header or generate new |
| 190 | cid = request.headers.get("X-Correlation-ID") or str(uuid.uuid4()) |
| 191 | set_correlation_id(cid) |
| 192 | |
| 193 | response = await call_next(request) |
| 194 | response.headers["X-Correlation-ID"] = cid |
| 195 | return response |
| 196 | ``` |
| 197 | |
| 198 | Propagate to outbound requests: |
| 199 | |
| 200 | ```python |
| 201 | import httpx |
| 202 | |
| 203 | async def call_downstream_service(endpoint: str, data: dict) -> dict: |
| 204 | """Call downstream service with correlation ID.""" |
| 205 | async with httpx.AsyncClient() as client: |
| 206 | response = await client.post( |
| 207 | endpoint, |
| 208 | json=data, |
| 209 | headers={"X-Correlation-ID": correlation_id.get()}, |
| 210 | ) |
| 211 | return response.json() |
| 212 | ``` |
| 213 | |
| 214 | ## Detailed worked examples and patterns |
| 215 | |
| 216 | Detailed sections (starting with `## Advanced Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient. |
| 217 | |
| 218 | ## Best Practices Summary |
| 219 | |
| 220 | 1. **Use structured logging** - JSON logs with consistent fields |
| 221 | 2. **Propagate correlation IDs** - Thread through all requests and logs |
| 222 | 3. **Track the four golden signals** - Latency, traffic, errors, saturation |
| 223 | 4. **Bound label cardinality** - Never use unbounded values as metric labels |
| 224 | 5. **Log at appropriate levels** - Don't cry wolf with ERROR |
| 225 | 6. **Include context** - User ID, request ID, operation name in logs |
| 226 | 7. **Use context managers** - Consistent timing and error handling |
| 227 | 8. **Separate concerns** - Observability code shouldn't pollute business logic |
| 228 | 9. **Test your observability** - Verify logs and metrics in integration tests |
| 229 | 10. **Set up alerts** - Metrics are useless without alerting |
| 230 |
Reviews
Installed this one?Write the first review and take the Trailblazer badge.
Alternatives
Subagent Driven DevelopmentUse when executing implementation plans with independent tasks in the current session◐◐◐◐◐●36/40Python Code Style & DocumentationPython code style, linting, formatting, naming conventions, and documentation standards. Use when writing new code, reviewing style, configuring linters, writing docstrings, or establishing project standards.◐····●35/40Competitor Price Analysis 💲Competitor pricing strategy analysis and market positioning. Price mapping, pricing gaps identification, elasticity signals evaluation, and strategic pricing optimization. Use when the user asks about competitor pricing, price analysis, pricing strategy, or co◐····●34/40Competitor Price Tracker 📊Set up competitor price tracking and monitoring workflows. Track price changes, detect promotions, analyze pricing patterns, and get alerts for competitive price movements.◐····●34/40