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.

Source Distribution

asgi-request-id-0.1.2.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

asgi_request_id-0.1.2-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file asgi-request-id-0.1.2.tar.gz.

File metadata

  • Download URL: asgi-request-id-0.1.2.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.8.1 Darwin/19.3.0

File hashes

Hashes for asgi-request-id-0.1.2.tar.gz
Algorithm Hash digest
SHA256 707acde3e695898575e61d8c1caac89f7525d6df259e1b0977ab6ce950323b84
MD5 91042e116d970a5f5f3d0e65f9c60992
BLAKE2b-256 bad2c45131257a9272cb709fe99b3de6c416647b39123866b79f25a5a7948415

See more details on using hashes here.

File details

Details for the file asgi_request_id-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: asgi_request_id-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.8.1 Darwin/19.3.0

File hashes

Hashes for asgi_request_id-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fabbd60e7928ec7a5ad386c2a384cd724cd70ad8ccbd6782242fa2a3658db7b1
MD5 25f2b5d84939e9c897cf6078944272b9
BLAKE2b-256 7c0410fcb71bae08628f679e7431766ce8609f1b059375a091eb62ce78060ee0

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