Skip to main content

ASGI request id middleware

Project description

asgi-request-id

PyPI Downloads PyPI Version License

This was developed at GRID for use with our python backend services and intended to make it easier to log/generate request IDs.

installation

pip install asgi-request-id

usage

import logging
import uvicorn

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse

from asgi_request_id import RequestIDMiddleware, get_request_id

logger = logging.getLogger(__name__)
app = Starlette()


@app.route("/")
def homepage(request):
    logger.info(f"Request ID: {get_request_id()}")
    return PlainTextResponse("hello world")


app.add_middleware(
    RequestIDMiddleware,
    incoming_request_id_header="x-amzn-trace-id",
    prefix="myapp-",
)

if __name__ == "__main__":
    uvicorn.run(app)

The package will do the following:

Search for an incoming request identifier and use it as the request id if found. If it is not found, an unique request id with an optional prefix is generated.

The request id is stored in a context variable and made available via get_request_id

Finally, it is set as the request_id response header.

Project details


Download files

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

Files for asgi-request-id, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size asgi_request_id-0.1.2-py3-none-any.whl (2.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size asgi-request-id-0.1.2.tar.gz (2.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page