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

feat: aiohttp request body #527

Merged
merged 4 commits into from
Oct 11, 2019
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
22 changes: 22 additions & 0 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from aiohttp.abc import AbstractMatchInfo
from typing import Any
from typing import Dict
from typing import Optional
from typing import Tuple
from typing import Callable

Expand Down Expand Up @@ -136,6 +137,7 @@ def aiohttp_processor(
_filter_headers(dict(request.headers)),
should_repr_strings=False,
)
request_info["data"] = get_aiohttp_request_data(request)

return event

Expand All @@ -152,3 +154,23 @@ def _capture_exception(hub):
)
hub.capture_event(event, hint=hint)
return exc_info


BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"


def get_aiohttp_request_data(request):
# type: (Request) -> Optional[str]
bytes_body = request._read_bytes

if bytes_body is not None:
# we have body to show
encoding = request.charset or "utf-8"
return bytes_body.decode(encoding)

if request.can_read_body:
# body exists but we can't show it
return BODY_NOT_READ_MESSAGE

# request has no body
return None
60 changes: 60 additions & 0 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from aiohttp import web

from sentry_sdk.integrations.aiohttp import AioHttpIntegration
Expand Down Expand Up @@ -33,6 +35,7 @@ async def hello(request):
assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
assert request["method"] == "GET"
assert request["query_string"] == ""
assert request.get("data") is None
assert request["url"] == "http://{host}/".format(host=host)
assert request["headers"] == {
"Accept": "*/*",
Expand All @@ -42,6 +45,63 @@ async def hello(request):
}


async def test_post_body_not_read(sentry_init, aiohttp_client, loop, capture_events):
from sentry_sdk.integrations.aiohttp import BODY_NOT_READ_MESSAGE

sentry_init(integrations=[AioHttpIntegration()])

body = {"some": "value"}

async def hello(request):
1 / 0

app = web.Application()
app.router.add_post("/", hello)

events = capture_events()

client = await aiohttp_client(app)
resp = await client.post("/", json=body)
assert resp.status == 500

event, = events
exception, = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
request = event["request"]

assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
assert request["method"] == "POST"
assert request["data"] == BODY_NOT_READ_MESSAGE


async def test_post_body_read(sentry_init, aiohttp_client, loop, capture_events):
sentry_init(integrations=[AioHttpIntegration()])

body = {"some": "value"}

async def hello(request):
await request.json()
1 / 0

app = web.Application()
app.router.add_post("/", hello)

events = capture_events()

client = await aiohttp_client(app)
resp = await client.post("/", json=body)
assert resp.status == 500

event, = events
exception, = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
request = event["request"]

assert request["env"] == {"REMOTE_ADDR": "127.0.0.1"}
assert request["method"] == "POST"
assert request["data"] == json.dumps(body)


async def test_403_not_captured(sentry_init, aiohttp_client, loop, capture_events):
sentry_init(integrations=[AioHttpIntegration()])

Expand Down