Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace_request_ctx incorrect expected to be a mapping class, it can be any object. #112

Open
mabrowning opened this issue Jan 9, 2025 · 0 comments

Comments

@mabrowning
Copy link

This works with aiohttp:

import asyncio
import aiohttp
import time
from types import SimpleNamespace

def request_tracer():
    async def on_request_start(session, context, params):
        context.trace_request_ctx.request_start = time.perf_counter()

    async def on_headers_sent(session, context, params):
        context.trace_request_ctx.headers_sent = time.perf_counter()

    async def on_request_end(session, context, params):
        context.trace_request_ctx.request_end = time.perf_counter()

    trace_config = aiohttp.TraceConfig()

    trace_config.on_request_start.append(on_request_start)
    trace_config.on_request_headers_sent.append(on_headers_sent)
    trace_config.on_request_end.append(on_request_end)
    return trace_config


async def make_request(session, url):
    trace_times = SimpleNamespace(
        request_start=0,
        headers_sent=0,
        request_end=0,
    )
    async with session.get(url, trace_request_ctx=trace_times) as response:
        await response.text()
    return trace_times

async def main(): 
    async with aiohttp.ClientSession( 
        trace_configs=[request_tracer()]
    ) as session: 
        print(await make_request(session, "https://google.com/"))

asyncio.run(main())

e.g. printing

namespace(request_start=2316235.097936708, headers_sent=2316235.647196083, request_end=2316235.74696975)

But when I substitute in aiohttp_retry:

from aiohttp_retry import RetryClient 

async def main_with_retry(): 
    async with aiohttp.ClientSession( 
        trace_configs=[request_tracer()]
    ) as session: 
        session = RetryClient(client_session=session)
        print(await make_request(session, "https://google.com/"))
asyncio.run(main_with_retry())

I get an exception:

Traceback (most recent call last):
  File "<python-input-4>", line 9, in <module>
    asyncio.run(main_with_retry())
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "lib/python3.13/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "lib/python3.13/asyncio/base_events.py", line 721, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "<python-input-4>", line 8, in main_with_retry
    print(await make_request(session, "https://google.com/"))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python-input-3>", line 30, in make_request
    async with session.get(url, trace_request_ctx=trace_times) as response:
               ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.13/site-packages/aiohttp_retry/client.py", line 158, in __aenter__
    return await self._do_request()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.13/site-packages/aiohttp_retry/client.py", line 123, in _do_request
    trace_request_ctx={
                      ^
    ...<2 lines>...
    },
    ^
TypeError: 'types.SimpleNamespace' object is not a mapping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant