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

Added method and url info to tracking signals #4674

Merged
merged 5 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -543,7 +544,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 @@ -603,9 +605,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 @@ -928,7 +933,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 @@ -958,10 +958,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 @@ -988,7 +990,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