5
5
from sentry_sdk .hub import Hub
6
6
from sentry_sdk .integrations import Integration
7
7
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
+ )
9
12
from sentry_sdk .serializer import partial_serialize
10
13
from sentry_sdk .utils import (
11
14
capture_internal_exceptions ,
12
15
event_from_exception ,
13
16
transaction_from_function ,
14
17
HAS_REAL_CONTEXTVARS ,
18
+ AnnotatedValue ,
15
19
)
16
20
17
21
import asyncio
27
31
from typing import Optional
28
32
from typing import Tuple
29
33
from typing import Callable
34
+ from typing import Union
30
35
31
36
from sentry_sdk .utils import ExcInfo
32
37
from sentry_sdk ._types import EventProcessor
@@ -118,9 +123,6 @@ def aiohttp_processor(
118
123
return event
119
124
120
125
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
-
124
126
request_info = event .setdefault ("request" , {})
125
127
126
128
request_info ["url" ] = "%s://%s%s" % (
@@ -132,12 +134,18 @@ def aiohttp_processor(
132
134
request_info ["query_string" ] = request .query_string
133
135
request_info ["method" ] = request .method
134
136
request_info ["env" ] = {"REMOTE_ADDR" : request .remote }
137
+
138
+ hub = Hub .current
135
139
request_info ["headers" ] = partial_serialize (
136
- Hub . current .client ,
140
+ hub .client ,
137
141
_filter_headers (dict (request .headers )),
138
142
should_repr_strings = False ,
139
143
)
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 )
141
149
142
150
return event
143
151
@@ -159,14 +167,20 @@ def _capture_exception(hub):
159
167
BODY_NOT_READ_MESSAGE = "[Can't show request body due to implementation details.]"
160
168
161
169
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 ]
164
172
bytes_body = request ._read_bytes
165
173
166
174
if bytes_body is not None :
167
175
# 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
+ )
168
182
encoding = request .charset or "utf-8"
169
- return bytes_body .decode (encoding )
183
+ return bytes_body .decode (encoding , "replace" )
170
184
171
185
if request .can_read_body :
172
186
# body exists but we can't show it
0 commit comments