Skip to content

Commit ecbe592

Browse files
authored
fix: Honor request_bodies for aiohttp (getsentry#529)
1 parent 8288600 commit ecbe592

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

sentry_sdk/integrations/_wsgi_common.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from sentry_sdk._types import MYPY
99

1010
if MYPY:
11+
import sentry_sdk
12+
1113
from typing import Any
1214
from typing import Dict
1315
from typing import Optional
@@ -29,6 +31,19 @@
2931
)
3032

3133

34+
def request_body_within_bounds(client, content_length):
35+
# type: (Optional[sentry_sdk.Client], int) -> bool
36+
if client is None:
37+
return False
38+
39+
bodies = client.options["request_bodies"]
40+
return not (
41+
bodies == "never"
42+
or (bodies == "small" and content_length > 10 ** 3)
43+
or (bodies == "medium" and content_length > 10 ** 4)
44+
)
45+
46+
3247
class RequestExtractor(object):
3348
def __init__(self, request):
3449
# type: (Any) -> None
@@ -48,12 +63,7 @@ def extract_into_event(self, event):
4863
if _should_send_default_pii():
4964
request_info["cookies"] = dict(self.cookies())
5065

51-
bodies = client.options["request_bodies"]
52-
if (
53-
bodies == "never"
54-
or (bodies == "small" and content_length > 10 ** 3)
55-
or (bodies == "medium" and content_length > 10 ** 4)
56-
):
66+
if not request_body_within_bounds(client, content_length):
5767
data = AnnotatedValue(
5868
"",
5969
{"rem": [["!config", "x", 0, content_length]], "len": content_length},

sentry_sdk/integrations/aiohttp.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
from sentry_sdk.hub import Hub
66
from sentry_sdk.integrations import Integration
77
from sentry_sdk.integrations.logging import ignore_logger
8-
from sentry_sdk.integrations._wsgi_common import _filter_headers
8+
from sentry_sdk.integrations._wsgi_common import (
9+
_filter_headers,
10+
request_body_within_bounds,
11+
)
912
from sentry_sdk.serializer import partial_serialize
1013
from sentry_sdk.utils import (
1114
capture_internal_exceptions,
1215
event_from_exception,
1316
transaction_from_function,
1417
HAS_REAL_CONTEXTVARS,
18+
AnnotatedValue,
1519
)
1620

1721
import asyncio
@@ -27,6 +31,7 @@
2731
from typing import Optional
2832
from typing import Tuple
2933
from typing import Callable
34+
from typing import Union
3035

3136
from sentry_sdk.utils import ExcInfo
3237
from sentry_sdk._types import EventProcessor
@@ -118,9 +123,6 @@ def aiohttp_processor(
118123
return event
119124

120125
with capture_internal_exceptions():
121-
# TODO: Figure out what to do with request body. Methods on request
122-
# are async, but event processors are not.
123-
124126
request_info = event.setdefault("request", {})
125127

126128
request_info["url"] = "%s://%s%s" % (
@@ -132,12 +134,18 @@ def aiohttp_processor(
132134
request_info["query_string"] = request.query_string
133135
request_info["method"] = request.method
134136
request_info["env"] = {"REMOTE_ADDR": request.remote}
137+
138+
hub = Hub.current
135139
request_info["headers"] = partial_serialize(
136-
Hub.current.client,
140+
hub.client,
137141
_filter_headers(dict(request.headers)),
138142
should_repr_strings=False,
139143
)
140-
request_info["data"] = get_aiohttp_request_data(request)
144+
145+
# Just attach raw data here if it is within bounds, if available.
146+
# Unfortunately there's no way to get structured data from aiohttp
147+
# without awaiting on some coroutine.
148+
request_info["data"] = get_aiohttp_request_data(hub, request)
141149

142150
return event
143151

@@ -159,14 +167,20 @@ def _capture_exception(hub):
159167
BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"
160168

161169

162-
def get_aiohttp_request_data(request):
163-
# type: (Request) -> Optional[str]
170+
def get_aiohttp_request_data(hub, request):
171+
# type: (Hub, Request) -> Union[Optional[str], AnnotatedValue]
164172
bytes_body = request._read_bytes
165173

166174
if bytes_body is not None:
167175
# we have body to show
176+
if not request_body_within_bounds(hub.client, len(bytes_body)):
177+
178+
return AnnotatedValue(
179+
"",
180+
{"rem": [["!config", "x", 0, len(bytes_body)]], "len": len(bytes_body)},
181+
)
168182
encoding = request.charset or "utf-8"
169-
return bytes_body.decode(encoding)
183+
return bytes_body.decode(encoding, "replace")
170184

171185
if request.can_read_body:
172186
# body exists but we can't show it

0 commit comments

Comments
 (0)