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

Allow for tracing headers sent on the wire by the client #5105

Closed
alex-kowalczyk opened this issue Oct 23, 2020 · 2 comments
Closed

Allow for tracing headers sent on the wire by the client #5105

alex-kowalczyk opened this issue Oct 23, 2020 · 2 comments

Comments

@alex-kowalczyk
Copy link

alex-kowalczyk commented Oct 23, 2020

🐣 Is your feature request related to a problem? Please describe.
Feature request:
TraceConfig does not allow for tracing "headers sent" stage, while it is quite often useful to verify them on-the-wire.

💡 Describe the solution you'd like
Implement on_headers_sent signal which provides exact on-the wire headers buffer sent, similar to on_request_chunk_sent.

The implementation should be quite straightforward:

  • Define a new signal in TraceConfig
  • Provide the callback as on_headers_sent to StreamWriter constructor, similar like on_chunk_sent is provided
  • Call the callback in write_headers, providing the serialized buf.

Describe alternatives you've considered
Workaround to see on-the-wire headers seems to be possible only with unittest.patch, or with monkey-copying whole ClientRequest.send lenghty function into a child class, adding logging there and providing this class to ClientSession.

Here is a sample workaround with patching:

class StreamWriter(aiohttp.http_writer.StreamWriter):
    async def write_headers(self, status_line: str, headers) -> None:
        buf = aiohttp._http_writer._serialize_headers(status_line, headers)
        self._write(buf)
        logging.debug(f'Wrote headers\n:{buf.decode("utf-8")}')
patch('aiohttp.client_reqrep.StreamWriter', StreamWriter).start()

📋 Additional context
When performing some API calls to remote server requiring very specific or complicated headers, it is useful to compare exact headers sent to e.g. what is sent through postman or browser. Not having access to textual representation of headers make this tedious. Also such a feature would allow for validating if cookie headers are correctly set by aiohttp.ClientSession.

@asvetlov
Copy link
Member

Agree, a new tracing signal sounds reasonable.
I'm happy to review a Pull Request when ready.

@derlih derlih mentioned this issue Oct 24, 2020
5 tasks
@asvetlov
Copy link
Member

Fixed by #5122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants