Skip to main content

An aiohttp utils to track request journey between services.

Project description

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. Guidelines will help you get ready to contribute to this project!

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

belvaio-request-id-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

belvaio_request_id-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file belvaio-request-id-0.1.1.tar.gz.

File metadata

  • Download URL: belvaio-request-id-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for belvaio-request-id-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bca30dce290f718a2e1b8cad4185ea84cc99eb468a3b07b051a10e895102d563
MD5 e61f3efafd66de0e45a636d137c30d4a
BLAKE2b-256 41bb2811a15933729332ada4f36b431a4654de0b7930df666ae0a418d25a4489

See more details on using hashes here.

File details

Details for the file belvaio_request_id-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: belvaio_request_id-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for belvaio_request_id-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3d384986bce7c17ec86a704a5933f94238d0c324b85b8ca39bd088a34590343c
MD5 6609bec83672d3f73cf6a40a789924eb
BLAKE2b-256 7e4570264a0e1c4c8c0214a87a17ad65601bb6c28242b826114d2679d7122400

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