asgi-correlation-id
asgi-correlation-id is a Python middleware that automatically tracks HTTP requests across your application logs by assigning each request a unique ID. It reads or generates correlation IDs from request headers and injects them into log output, making it simple to trace all logs belonging to a single user request.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | snok/asgi-correlation-id |
| Owner | snok |
| Primary language | Python |
| License | MIT — OSI-approved |
| Stars | 637 |
| Forks | 36 |
| Open issues | 2 |
| Latest release | v5.0.1 (2026-06-09) |
| Last updated | 2026-06-09 |
| Source | https://github.com/snok/asgi-correlation-id |
What asgi-correlation-id is
ASGI middleware that implements request ID propagation by reading X-Request-ID (or custom header) from incoming requests or generating UUIDs, storing them in context-local storage, and providing a log filter for injection into Python logging records. Supports header validation, custom generators, and transformers; integrates with Starlette/FastAPI frameworks.
Get the asgi-correlation-id source
Clone the repository and explore it locally.
git clone https://github.com/snok/asgi-correlation-id.gitcd asgi-correlation-id# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
Implementation considerations
- Middleware ordering matters: place CorrelationIdMiddleware early in the stack so correlation IDs are available to downstream middleware and handlers.
- Logging configuration must include the CorrelationIdFilter and %(correlation_id)s format string; without this, IDs are generated but not emitted to logs.
- For CORS requests, explicitly allow the correlation ID header in allow_headers and expose_headers; browser security policies will block cross-origin access otherwise.
- If using custom validators or generators, ensure they are thread-safe and performant; validator is called on every incoming request and generator on every cache miss.
- Exception handlers must explicitly include the correlation ID in 5xx error responses if you need traceability for unhandled exceptions.
When to avoid it — and what to weigh
- Non-ASGI frameworks — This middleware is ASGI-specific; if your application uses WSGI (Django, Flask in traditional mode) or other protocols, you will need alternative solutions or adapters.
- Minimal logging footprint required — If you are constrained by log volume or storage, adding correlation IDs increases log size; evaluate cost-benefit trade-offs in high-volume environments.
- Full distributed tracing needs — Correlation IDs alone do not capture timing, span hierarchies, or resource metrics; if you need comprehensive tracing, pair this with OpenTelemetry or similar instrumentation.
- Strict context isolation required — If your application runs hundreds of concurrent requests in a single thread (rare) or requires guarantees that correlation IDs never leak between requests, test context-local storage behavior under your load profile.
License & commercial use
MIT License: permissive open-source license allowing commercial use, modification, and distribution with proper attribution and no warranty.
MIT is a permissive OSI-approved license that explicitly permits commercial use without royalties or restrictions. No license review required for standard commercial deployment. Review internal policies for any project-specific compliance requirements.
DEV.co evaluation signals
Editorial assessment — not user reviews. Directional, with an explicit confidence level.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Strong |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Strong |
| Assessment confidence | High |
Correlation IDs are non-sensitive identifiers (typically UUIDs) not suitable for authentication or authorization. Ensure X-Request-ID response header is not exposed to untrusted clients if internal tracing details must remain hidden; CORS expose_headers configuration controls this. Default UUIDs are cryptographically generated; custom generators should maintain similar entropy.
Alternatives to consider
OpenTelemetry SDK + auto-instrumentation
Provides distributed tracing with full span hierarchies, timing, and resource metrics across services; more heavyweight but gives richer observability than correlation IDs alone.
Structlog with contextvars
General-purpose structured logging library with built-in context support; requires manual correlation ID propagation but offers more flexible log formatting and filtering.
Starlette middleware + custom context manager
Manual implementation avoids a dependency; suitable if you need minimal features or want full control, but requires maintenance and testing.
Build on asgi-correlation-id with DEV.co software developers
Implement asgi-correlation-id to unify logs across your ASGI application. Devco experts can help integrate this middleware into your logging stack and connect it to your observability tools.
Talk to DEV.coRelated open-source tools
Surfaced by semantic similarity across the DEV.co open-source index.
Related on DEV.co
Explore the category and the services that help you build with it.
asgi-correlation-id FAQ
Can I use this with Django?
What if a request doesn't include a correlation ID header?
How do I propagate correlation IDs to background jobs or external API calls?
Does this add significant overhead?
Custom software development services
DEV.co is a software development agency delivering custom software development services to companies building on open source. Our software developers and web developers design, integrate, and ship production systems — spanning web development, APIs, AI, data, and cloud. If asgi-correlation-id is part of your open-source observability roadmap, our team can implement, customize, migrate, and maintain it.
Ready to streamline your request tracing?
Implement asgi-correlation-id to unify logs across your ASGI application. Devco experts can help integrate this middleware into your logging stack and connect it to your observability tools.