Skip to main content

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.

Project description

Test Suite Python PyPI version codecov Docs Downloads

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.

Resources:

Installation

$ pip install starlette-context

Requirements

Should be working fine on 3.7+. Official support starts at 3.8.

Dependencies

  • starlette

Example

import uvicorn

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

from starlette_context import context, plugins
from starlette_context.middleware import RawContextMiddleware

middleware = [
    Middleware(
        RawContextMiddleware,
        plugins=(
            plugins.RequestIdPlugin(),
            plugins.CorrelationIdPlugin()
        )
    )
]

app = Starlette(middleware=middleware)


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


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

In this example the response contains 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.

Sponsorship

A huge thank you to Adverity for sponsoring the development of this OSS library in 2022.

Contribution

See the guide on read the docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_context-0.3.6.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

starlette_context-0.3.6-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file starlette_context-0.3.6.tar.gz.

File metadata

  • Download URL: starlette_context-0.3.6.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.8.16 Linux/5.15.0-1031-azure

File hashes

Hashes for starlette_context-0.3.6.tar.gz
Algorithm Hash digest
SHA256 d361a36ba2d4acca3ab680f917b25e281533d725374752d47607a859041958cb
MD5 faa9ffb15570b2423170dfc82d2a813e
BLAKE2b-256 a821aa5d3848fcb77f0e7c9f1844271826ff74d83fad9920ca7f8105e0dc02ee

See more details on using hashes here.

File details

Details for the file starlette_context-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: starlette_context-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.8.16 Linux/5.15.0-1031-azure

File hashes

Hashes for starlette_context-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b14ce373fbb6895a2182a7104b9f63ba20c8db83444005fb9a844dd77ad9895c
MD5 06f61a7291e2bf8f8efbf4fb3be9d53e
BLAKE2b-256 d056a56af8066d9c17696207ee15bad43fd06dafc5917f6e7c7c3899427f6fc3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page