Skip to content

Pre-process incoming requests? #3378

Closed
Closed
@thanatos

Description

Long story short

Is it possible to hook/log requests at the start of the request? (aiohttp's AccessLogger is invoked after the request, as it receives the response as one of its arguments.)

As part of our logging, we'd like to assign each request a unique ID. Ideally the request comes in w/ this in a header, but if it doesn't, we assign it one. Currently, we do this in an AccessLogger, when we log there, however, this means this ID isn't associated w/ the request until the very end. I was wondering if it's possible to hook the very start of the request, s.t. this can happen as early as possible. This allows other logging calls that occur during the processing of the request to access this context. Essentially, I want to be able to know that "this log line indicating a DB call belongs to this request", which is made difficult when aiohttp handles multiple requests simultaneously, as the logs get intertwined. Hence, attaching an ID allows the log reader to understand which lines apply to which requests.

I can wrap every request handler in a decorator to accomplish this, but I was wondering if there was something more convenient.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions