Skip to main content

Build Status PyPI version PyPI license codecov

starlette context

Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.

Motivation

I use FastAPI. I needed something that will allow me to log with context data. Right now I can just log.info('Message') and I have log (in ELK) with request id and correlation id. I don't even think about passing this data to logger. It's there automatically.

Installation

$ pip install starlette-context

Requirements

Python 3.7+

For Python 3.6 only:
contextvar came out with python3.7 and is required for this to run. If you need to use Starlette with python3.6, there is a backport of contextvar for py36. https://github.com/MagicStack/contextvars

Dependencies

  • starlette

All other dependencies from requirements-dev.txt are only needed to run tests or examples. Test/dev env is dockerized if you want to try them yourself.

Example

examples/simple_examples/set_context_in_middleware.py

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse

import uvicorn
from starlette_context import context, plugins
from starlette_context.middleware import ContextMiddleware


app = Starlette(debug=True)
app.add_middleware(ContextMiddleware.with_plugins(  # easily extensible
    plugins.RequestIdPlugin,  # request id
    plugins.CorrelationIdPlugin  # correlation id
))


@app.route('/')
async def index(request: Request):
    return JSONResponse(context.data)


uvicorn.run(app, host="0.0.0.0")

In this example the response containes a json with

{
  "X-Correlation-ID":"5ca2f0b43115461bad07ccae5976a990",
  "X-Request-ID":"21f8d52208ec44948d152dc49a713fdd"
}

Context can be updated and accessed at anytime if it's created in the middleware.

Contribution

All tickets or PRs are more than welcome.

Metadata

Release files for starlette-context 0.2.0

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

Source distribution (sdist)

Source distribution for starlette-context 0.2.0
File Size Uploaded Provenance Actions
starlette_context-0.2.0.tar.gz 6.9 kB Without provenance Details for starlette_context-0.2.0.tar.gz

Release files / starlette_context-0.2.0.tar.gz

Download URL starlette_context-0.2.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ad0d034ae296b2af3dea6cc2a1773514a1c515761ca562a53ee7fef3b1109e66
BLAKE2b-256 checksum
How to use checksums
c78d77c1d2a1a1b9aa49f41cb561628fa255b351f6c1b263aa61af03188be867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.0

Security for starlette-context 0.2.0

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.

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