From 496444e81c55756aa9c47c315298e9d1d4178502 Mon Sep 17 00:00:00 2001 From: Adrian Krupa Date: Fri, 16 Oct 2020 17:07:44 +0200 Subject: [PATCH] Added method and url info to tracking signals (#4674) * Added method and url info to `request_chunk_sent` and `response_chunk_received` signals * Add CHANGES * Reorder contributors * Updated docs Co-authored-by: Andrew Svetlov --- CHANGES/4674.feature | 1 + CONTRIBUTORS.txt | 1 + aiohttp/client_reqrep.py | 15 +++++++++++---- aiohttp/tracing.py | 18 ++++++++++++++---- docs/tracing_reference.rst | 16 ++++++++++++++++ tests/test_client_response.py | 6 ++++-- tests/test_tracing.py | 4 ++-- 7 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 CHANGES/4674.feature diff --git a/CHANGES/4674.feature b/CHANGES/4674.feature new file mode 100644 index 00000000000..4ecc652d76e --- /dev/null +++ b/CHANGES/4674.feature @@ -0,0 +1 @@ +Add `method` and `url` attributes to `TraceRequestChunkSentParams` and `TraceResponseChunkReceivedParams`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index e512f4857d0..0dfd7717559 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -4,6 +4,7 @@ A. Jesse Jiryu Davis Adam Bannister Adam Cooper Adam Mills +Adrian Krupa Adrián Chaves Alan Tse Alec Hanefeld diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 79fee449d51..957c8e5e2db 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -1,5 +1,6 @@ import asyncio import codecs +import functools import io import re import sys @@ -595,7 +596,8 @@ async def send(self, conn: 'Connection') -> 'ClientResponse': assert protocol is not None writer = StreamWriter( protocol, self.loop, - on_chunk_sent=self._on_chunk_request_sent + on_chunk_sent=functools.partial(self._on_chunk_request_sent, + self.method, self.url) ) if self.compress: @@ -655,9 +657,12 @@ def terminate(self) -> None: self._writer.cancel() self._writer = None - async def _on_chunk_request_sent(self, chunk: bytes) -> None: + async def _on_chunk_request_sent(self, + method: str, + url: URL, + chunk: bytes) -> None: for trace in self._traces: - await trace.send_request_chunk_sent(chunk) + await trace.send_request_chunk_sent(method, url, chunk) class ClientResponse(HeadersMixin): @@ -972,7 +977,9 @@ async def read(self) -> bytes: try: self._body = await self.content.read() for trace in self._traces: - await trace.send_response_chunk_received(self._body) + await trace.send_response_chunk_received(self.method, + self.url, + self._body) except BaseException: self.close() raise diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index 0c07c642eda..d78334dcf4f 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -217,12 +217,16 @@ class TraceRequestStartParams: @attr.s(frozen=True, slots=True) class TraceRequestChunkSentParams: """ Parameters sent by the `on_request_chunk_sent` signal""" + method = attr.ib(type=str) + url = attr.ib(type=URL) chunk = attr.ib(type=bytes) @attr.s(frozen=True, slots=True) class TraceResponseChunkReceivedParams: """ Parameters sent by the `on_response_chunk_received` signal""" + method = attr.ib(type=str) + url = attr.ib(type=URL) chunk = attr.ib(type=bytes) @@ -324,18 +328,24 @@ async def send_request_start(self, TraceRequestStartParams(method, url, headers) ) - async def send_request_chunk_sent(self, chunk: bytes) -> None: + async def send_request_chunk_sent(self, + method: str, + url: URL, + chunk: bytes) -> None: return await self._trace_config.on_request_chunk_sent.send( self._session, self._trace_config_ctx, - TraceRequestChunkSentParams(chunk) + TraceRequestChunkSentParams(method, url, chunk) ) - async def send_response_chunk_received(self, chunk: bytes) -> None: + async def send_response_chunk_received(self, + method: str, + url: URL, + chunk: bytes) -> None: return await self._trace_config.on_response_chunk_received.send( self._session, self._trace_config_ctx, - TraceResponseChunkReceivedParams(chunk) + TraceResponseChunkReceivedParams(method, url, chunk) ) async def send_request_end(self, diff --git a/docs/tracing_reference.rst b/docs/tracing_reference.rst index 96f77a26ea1..772b485ddcb 100644 --- a/docs/tracing_reference.rst +++ b/docs/tracing_reference.rst @@ -299,6 +299,14 @@ TraceRequestChunkSentParams See :attr:`TraceConfig.on_request_chunk_sent` for details. + .. attribute:: method + + Method that will be used to make the request. + + .. attribute:: url + + URL that will be used for the request. + .. attribute:: chunk Bytes of chunk sent @@ -313,6 +321,14 @@ TraceResponseChunkReceivedParams See :attr:`TraceConfig.on_response_chunk_received` for details. + .. attribute:: method + + Method that will be used to make the request. + + .. attribute:: url + + URL that will be used for the request. + .. attribute:: chunk Bytes of chunk received diff --git a/tests/test_client_response.py b/tests/test_client_response.py index 9a7cf7eb2aa..0fe82e537bf 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -970,10 +970,12 @@ def test_redirect_history_in_exception() -> None: async def test_response_read_triggers_callback(loop, session) -> None: trace = mock.Mock() trace.send_response_chunk_received = make_mocked_coro() + response_method = 'get' + response_url = URL('http://def-cl-resp.org') response_body = b'This is response' response = ClientResponse( - 'get', URL('http://def-cl-resp.org'), + response_method, response_url, request_info=mock.Mock, writer=mock.Mock(), continue100=None, @@ -1000,7 +1002,7 @@ def side_effect(*args, **kwargs): assert trace.send_response_chunk_received.called assert ( trace.send_response_chunk_received.call_args == - mock.call(response_body) + mock.call(response_method, response_url, response_body) ) diff --git a/tests/test_tracing.py b/tests/test_tracing.py index 1155f2539dd..7198d82328e 100644 --- a/tests/test_tracing.py +++ b/tests/test_tracing.py @@ -73,12 +73,12 @@ class TestTrace: ), ( 'request_chunk_sent', - (Mock(), ), + (Mock(), Mock(), Mock()), TraceRequestChunkSentParams ), ( 'response_chunk_received', - (Mock(), ), + (Mock(), Mock(), Mock()), TraceResponseChunkReceivedParams ), (