Skip to content

Commit

Permalink
[3.7] Added method and url info to tracking signals (#4674) (#5063)
Browse files Browse the repository at this point in the history
Backports the following commits to 3.7:
 - Added method and url info to tracking signals (#4674)

Co-authored-by: Adrian Krupa <adrian.krupa91@gmail.com>
  • Loading branch information
github-actions[bot] and adriankrupa authored Oct 16, 2020
1 parent ec11aca commit 0b66592
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES/4674.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `method` and `url` attributes to `TraceRequestChunkSentParams` and `TraceResponseChunkReceivedParams`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ A. Jesse Jiryu Davis
Adam Bannister
Adam Cooper
Adam Mills
Adrian Krupa
Adrián Chaves
Alan Tse
Alec Hanefeld
Expand Down
15 changes: 11 additions & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import codecs
import functools
import io
import re
import sys
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions aiohttp/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions docs/tracing_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ class TestTrace:
),
(
'request_chunk_sent',
(Mock(), ),
(Mock(), Mock(), Mock()),
TraceRequestChunkSentParams
),
(
'response_chunk_received',
(Mock(), ),
(Mock(), Mock(), Mock()),
TraceResponseChunkReceivedParams
),
(
Expand Down

0 comments on commit 0b66592

Please sign in to comment.