Make transports monitoring easier #2580
Replies: 3 comments
-
Can you explain to me what you mean here? |
Beta Was this translation helpful? Give feedback.
-
|
Right now, to monitor every request, we must create sub-classes for each transport: class MonitoredASGITransport(httpx.ASGITransport):
async def handle_async_request(
self,
request: httpx.Request,
) -> httpx.Response:
start_time = time.time()
response = await super().handle_async_request(request)
print("Request took", time.time() - start_time)
return responseSince it is redundant for each transport class to override async def monitored_handle_async_request(
self,
request: httpx.Request,
) -> httpx.Response:
start_time = time.time()
response = await old_handle(request)
print("Request took", time.time() - start_time)
return response
old_handle = httpx.AsyncBaseTransport.handle_async_request
httpx.AsyncBaseTransport.handle_async_request = monitored_handle_async_requestThis is achieved by having |
Beta Was this translation helpful? Give feedback.
-
|
@tomchristie By looking at the example commit I made, which is fully backward-compatible, you can review the entire change. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, the only way to use public methods to monitor/instrument requests is to monkey-patch each transport class. By making
BaseTransport.handle_requestandAsyncBaseTransport.handle_async_requestsingle entry points to all transport subclasses, it would be much easier to monitor them all (by patching onlyBaseTransportandAsyncBaseTransport), something like the below:I would be happy to try to open a PR for this. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions