Skip to main content

asyncio-context

Tool for store data within asyncio task (for example, in all coroutines across handling aiohttp request) and enrich logging record with this data.
Small, nice-looking and with annotations.

Install

pip install asyncio-context

Example

It can be used for logging trace id and other info about request:

...
    'formatters': {
        'standard': {
            'format': '[trace_id:%(trace_id)s] [user_id:%(user_id)s]:%(message)s'
        },
    },
    'filters': {
        'context2record': {
            '()': 'asyncio_context.logging.Context2Record',
            'ctx_name': 'ViewsCtx'
        },
    },
...
from asyncio_context import Context

class ViewsCtx(Context):
    user_id: str = 'UNKNOWN'
    trace_id: str = 'NO_TRACE'
    
ctx = ViewsCtx()
log = logging.getLogger()

class MyView(web.View):
    async def get(self):
        
        ctx.trace_id = self.request.headers.get('X-Trace-Id', str(uuid.uuid4()))
        # or
        ViewsCtx().user_id = self.request.match_info.get('user_id')
        
        log.info('Test request')
        # GET http://localhost:8080/user/123
        # [trace_id:ea38e89d-fba1-40a3-899e-297c6cd76732] [user_id:123]:Test request

Context can be copied to another asyncio task:

task = asyncio.get_running_loop().create_task(another_task())
ViewsCtx.copy_to_task(task)

Metadata

Release files for asyncio-context 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncio-context 0.0.1
File Size Uploaded Provenance
asyncio-context-0.0.1.tar.gz 4.2 kB Without provenance Details

Release files / asyncio-context-0.0.1.tar.gz

Download URL asyncio-context-0.0.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0e3895535ee71dc6873ba74037cdbf9d1dbd998a0dc74a2a5cb65d128d63c829
BLAKE2b-256 checksum
How to use checksums
9b1393d878a590bb6536923b9afb5ad8bdd3dfeb7db10bfb438a8c288791af52
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.6

Security for asyncio-context 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

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