Skip to main content

fastapi-correlation

📢 Hobby Project Notice: This is a research and learning project exploring FastAPI middleware and structured logging best practices. Feel free to use it as a reference, report issues, or suggest improvements! Contributions and feedback are always welcome.

Correlation ID middleware and structured logging for FastAPI — zero project-specific dependencies (only Starlette).

Features

  • CorrelationIDMiddleware — injects a unique X-Correlation-ID header per request (reads the incoming header if present, generates a UUID4 otherwise)
  • get_correlation_id() — context-var accessor usable anywhere in the request lifecycle
  • LoggingContextMiddleware — injects endpoint, method, status_code, and user_id into every log record for the duration of the request
  • set_log_context / get_log_context / clear_log_context — helpers for enriching per-request structured log fields
  • StructuredJSONFormatter — RFC 3339 UTC JSON output ready for Loki / Grafana Alloy
  • HumanReadableFormatter — compact, coloured output for local development

Installation

pip install fastapi-correlation

Quick start

from fastapi import FastAPI
from fastapi_correlation import (
    CorrelationIDMiddleware,
    LoggingContextMiddleware,
    StructuredJSONFormatter,
    set_log_context,
    get_correlation_id,
)
import logging, sys

# Wire up structured JSON logging
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(StructuredJSONFormatter())
logging.getLogger().addHandler(handler)

app = FastAPI()
app.add_middleware(LoggingContextMiddleware)
app.add_middleware(CorrelationIDMiddleware)

@app.get("/ping")
async def ping():
    set_log_context(custom_field="hello")
    return {"correlation_id": get_correlation_id()}

License

MIT

Metadata

Release files for fastapi-correlation 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-correlation 0.0.2
File Size Uploaded Provenance Actions
fastapi_correlation-0.0.2.tar.gz 47.5 kB Without provenance Details for fastapi_correlation-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-correlation 0.0.2
File Interpreter ABI Platform Actions
fastapi_correlation-0.0.2-py3-none-any.whl Python 3 none any Details for fastapi_correlation-0.0.2-py3-none-any.whl

Total release size: 55.5 kB

Release files / fastapi_correlation-0.0.2.tar.gz

Download URL fastapi_correlation-0.0.2.tar.gz
Size 47.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ed90e8c6a9e1e4b6c12cc403da3dac85ad3caa0fd569bba27ae92c4cf38a01f2
BLAKE2b-256 checksum
How to use checksums
acbc1e41c3ec434a6c46f2f3ea4739021c462589e06e9a7a1e9d9e7aa9cfb772
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / fastapi_correlation-0.0.2-py3-none-any.whl

Download URL fastapi_correlation-0.0.2-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0a069b0c1f6ffa7b51e55f730fc4d90321bd05068c3a7e30e0e9fb43eb2f77c
BLAKE2b-256 checksum
How to use checksums
8ead568c3db7ba0fa1723291ef3f267a6e88f82dbf2765e3244322ac3d01ae87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"openSUSE Tumbleweed","version":"20260223","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for fastapi-correlation 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page