Skip to main content

Belvaio Request Id

Package version PyPI - Downloads Build Status codecov

Belvaio Request Id is an aiohttp set of utils that help us to track request journey between services.

  1. request_id_middleware: aiohttp middleware that generate random request_id or read it from X-Request-Id header for each http request.

  2. RequestIdFilter: logging filter that allow attach request_id to every logging record.

  3. RequestIdAccessLogger: add request_id to aiohttp access log. This log message is logged outside the scope where we set the context var that store the request_id, so we need to define our own AccessLogger that fixes this.

  4. If Sentry is used a request_id tag is added when the http request is processed.

Motivation: Skyscanner / aiotask-context

Requirements

Installation

pip install belvaio-request-id

Example

"""
POC to demonstrate the usage of the belvaio-request-id package for writing the request_id from aiohttp into every log call. If you run this script, you can try to query with curl or the browser:

    $ curl http://127.0.0.1:8080/Mateu
    Hello, Mateu. Your request id is 93234aa6d4524f4bb76622e5d0c85589.

    $ curl -H "X-Request-ID: e72ec21b412845cf86a8aee50331cc4f" http://127.0.0.1:8080/Mateu
    Hello, Mateu. Your request id is e72ec21b412845cf86a8aee50331cc4f.

In the terminal you should see something similar to:

    ======== Running on http://0.0.0.0:8080 ========
    (Press CTRL+C to quit)
    2020-03-20 11:43:20,248 INFO __main__ 93234aa6d4524f4bb76622e5d0c85589 | Received new GET /Mateu call
    2020-03-20 11:43:20,249 INFO aiohttp.access 93234aa6d4524f4bb76622e5d0c85589 | 127.0.0.1 "GET /Mateu HTTP/1.1" 200 266 "curl/7.64.1"
"""

import logging.config

from aiohttp import web
from belvaio_request_id.logger import RequestIdAccessLogger
from belvaio_request_id.middleware import request_id_middleware
from belvaio_request_id.utils import get_request_id

LOG_SETTINGS = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "INFO",
            "formatter": "default",
            "filters": ["requestid"],
        },
    },
    "filters": {"requestid": {"()": "belvaio_request_id.logger.RequestIdFilter",},},
    "formatters": {
        "default": {
            "format": "%(asctime)s %(levelname)s %(name)s %(request_id)s | %(message)s",
        },
    },
    "loggers": {"": {"level": "DEBUG", "handlers": ["console"], "propagate": True},},
}

logging.config.dictConfig(LOG_SETTINGS)
logger = logging.getLogger(__name__)


async def handle(request):
    name = request.match_info.get("name")
    logger.info("Received new GET /%s call", name)
    text = f"Hello, {name}. Your request id is {get_request_id()}.\n"
    return web.Response(text=text)


if __name__ == "__main__":
    app = web.Application(middlewares=[request_id_middleware])
    app.router.add_route("GET", "/{name}", handle)
    web.run_app(
        app,
        access_log_format='%a "%r" %s %b "%{User-Agent}i"',
        access_log_class=RequestIdAccessLogger,
    )

Contributing

The Belvo team happily welcomes contributions. If you wish to submit a pull request, please be sure check the items on this list:

Guidelines will help you get ready to contribute to this project!

Metadata

Release files for belvaio-request-id 0.1.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 belvaio-request-id 0.1.2
File Size Uploaded
belvaio-request-id-0.1.2.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for belvaio-request-id 0.1.2
File Interpreter ABI Platform
belvaio_request_id-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / belvaio-request-id-0.1.2.tar.gz

Download URL belvaio-request-id-0.1.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0c55696d465bee0d24addcb941e58fd86890ec30c029671839e7641eecd63b19
BLAKE2b-256 checksum
How to use checksums
d0c4db17c0113b177cd2b07ce49d6aece61b7e724b5b1755c1bdb49742e3e783
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.1

Release files / belvaio_request_id-0.1.2-py3-none-any.whl

Download URL belvaio_request_id-0.1.2-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad5bf9c0fec7054e648b8bcbaf134be555e264aa883b32eb06ea88f47bdf5535
BLAKE2b-256 checksum
How to use checksums
a38467d694a13344d49d9a8a7fd6d03d187668eafa0c352b764e210eeaaefdac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.1

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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