Skip to content

Commit

Permalink
[PR #10332/a0e64bb4 backport][3.11] Log the remote that generates req…
Browse files Browse the repository at this point in the history
…uest errors (#10337)
  • Loading branch information
patchback[bot] authored Jan 20, 2025
1 parent 6c3f6f0 commit 68e426c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES/10332.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved logging of HTTP protocol errors to include the remote address -- by :user:`bdraco`.
8 changes: 6 additions & 2 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,13 @@ def handle_error(
# or encrypted traffic to an HTTP port. This is expected
# to happen when connected to the public internet so we log
# it at the debug level as to not fill logs with noise.
self.logger.debug("Error handling request", exc_info=exc)
self.logger.debug(
"Error handling request from %s", request.remote, exc_info=exc
)
else:
self.log_exception("Error handling request", exc_info=exc)
self.log_exception(
"Error handling request from %s", request.remote, exc_info=exc
)

# some data already got sent, connection is broken
if request.writer.output_size > 0:
Expand Down
36 changes: 27 additions & 9 deletions tests/test_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ async def handler(request):
assert txt.startswith("500 Internal Server Error")
assert "Traceback" not in txt

logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_logs_invalid_method_with_loop_debug(
Expand Down Expand Up @@ -85,7 +87,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
# on the first request since the client may
# be probing for TLS/SSL support which is
# expected to fail
logger.debug.assert_called_with("Error handling request", exc_info=exc)
logger.debug.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)
logger.debug.reset_mock()

# Now make another connection to the server
Expand All @@ -99,7 +103,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
# on the first request since the client may
# be probing for TLS/SSL support which is
# expected to fail
logger.debug.assert_called_with("Error handling request", exc_info=exc)
logger.debug.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_logs_invalid_method_without_loop_debug(
Expand Down Expand Up @@ -128,7 +134,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
# on the first request since the client may
# be probing for TLS/SSL support which is
# expected to fail
logger.debug.assert_called_with("Error handling request", exc_info=exc)
logger.debug.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_logs_invalid_method_second_request(
Expand Down Expand Up @@ -159,7 +167,9 @@ async def handler(request: web.BaseRequest) -> web.Response:
# BadHttpMethod should be logged as an exception
# if its not the first request since we know
# that the client already was speaking HTTP
logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_logs_bad_status_line_as_exception(
Expand All @@ -184,7 +194,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
txt = await resp.text()
assert "Traceback (most recent call last):\n" not in txt

logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_handler_timeout(
Expand Down Expand Up @@ -254,7 +266,9 @@ async def handler(request):
txt = await resp.text()
assert "Traceback (most recent call last):\n" in txt

logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_html_exception(aiohttp_raw_server, aiohttp_client):
Expand All @@ -278,7 +292,9 @@ async def handler(request):
"</body></html>\n"
)

logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_raw_server_html_exception_debug(aiohttp_raw_server, aiohttp_client):
Expand All @@ -302,7 +318,9 @@ async def handler(request):
"<pre>Traceback (most recent call last):\n"
)

logger.exception.assert_called_with("Error handling request", exc_info=exc)
logger.exception.assert_called_with(
"Error handling request from %s", cli.host, exc_info=exc
)


async def test_handler_cancellation(unused_port_socket: socket.socket) -> None:
Expand Down

0 comments on commit 68e426c

Please sign in to comment.