Skip to content

Commit

Permalink
feat: aiohttp request body (#527)
Browse files Browse the repository at this point in the history
* feat: aiohttp request body

* feat: placeholder text for the case when aiohttp request body can't be read

* tests: add tests for getting aiohttp request body

* tests: fixes for tests
  • Loading branch information
revimi authored and untitaker committed Oct 11, 2019
1 parent 39120dc commit 0ace04d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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

0 comments on commit 0ace04d

Please sign in to comment.