Skip to content

Commit 4f36419

Browse files
committed
wip req bod
1 parent e4bc156 commit 4f36419

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

sentry_sdk/integrations/starlette.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import json
32
import sys
43
import warnings
54
from collections.abc import Set
@@ -35,6 +34,7 @@
3534
ensure_integration_enabled,
3635
event_from_exception,
3736
parse_version,
37+
serialize_request_body_data,
3838
transaction_from_function,
3939
)
4040

@@ -242,16 +242,6 @@ async def _sentry_send(*args: "Any", **kwargs: "Any") -> "Any":
242242
return middleware_class
243243

244244

245-
def _serialize_request_body_data(data: "Any") -> str:
246-
# data may be a JSON-serializable value, an AnnotatedValue, or a dict with AnnotatedValue values
247-
def _default(value: "Any") -> "Any":
248-
if isinstance(value, AnnotatedValue):
249-
return value.value
250-
return str(value)
251-
252-
return json.dumps(data, default=_default)
253-
254-
255245
def _set_request_body_data_on_streaming_segment(
256246
info: "Optional[Dict[str, Any]]",
257247
) -> None:
@@ -260,7 +250,7 @@ def _set_request_body_data_on_streaming_segment(
260250
with capture_internal_exceptions():
261251
current_span._segment.set_attribute(
262252
"http.request.body.data",
263-
_serialize_request_body_data(info["data"]),
253+
serialize_request_body_data(info["data"]),
264254
)
265255

266256

sentry_sdk/integrations/tornado.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
from sentry_sdk.tracing import TransactionSource
1111
from sentry_sdk.tracing_utils import has_span_streaming_enabled
1212
from sentry_sdk.utils import (
13+
AnnotatedValue,
1314
HAS_REAL_CONTEXTVARS,
1415
CONTEXTVARS_ERROR_MESSAGE,
1516
ensure_integration_enabled,
1617
event_from_exception,
1718
capture_internal_exceptions,
19+
serialize_request_body_data,
1820
transaction_from_function,
1921
)
2022
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable
2123
from sentry_sdk.integrations._wsgi_common import (
2224
RequestExtractor,
2325
_filter_headers,
2426
_is_json_content_type,
27+
request_body_within_bounds,
2528
)
2629
from sentry_sdk.integrations.logging import ignore_logger
2730

@@ -169,7 +172,7 @@ def _handle_request_impl(self: "RequestHandler") -> "Generator[None, None, None]
169172

170173
method = getattr(self, self.request.method.lower(), None)
171174
if method is not None:
172-
span_name = transaction_from_function(method) or ""
175+
span_name = transaction_from_function(method)
173176
if span_name:
174177
span.name = span_name
175178
span.set_attribute(
@@ -213,9 +216,37 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]":
213216
attributes["client.address"] = request.remote_ip
214217
attributes["user.ip_address"] = request.remote_ip
215218

219+
with capture_internal_exceptions():
220+
body_data = _extract_request_body_data(request)
221+
if body_data is not None:
222+
attributes["http.request.body.data"] = serialize_request_body_data(
223+
body_data
224+
)
225+
216226
return attributes
217227

218228

229+
def _extract_request_body_data(request: "Any") -> "Any":
230+
client = sentry_sdk.get_client()
231+
extractor = TornadoRequestExtractor(request)
232+
content_length = extractor.content_length()
233+
234+
if not content_length:
235+
return None
236+
237+
if not request_body_within_bounds(client, content_length):
238+
return AnnotatedValue.removed_because_over_size_limit()
239+
240+
parsed_body = extractor.parsed_body()
241+
if parsed_body is not None:
242+
return parsed_body
243+
244+
if extractor.raw_data():
245+
return AnnotatedValue.removed_because_raw_data()
246+
247+
return None
248+
249+
219250
@ensure_integration_enabled(TornadoIntegration)
220251
def _capture_exception(ty: type, value: BaseException, tb: "Any") -> None:
221252
if isinstance(value, HTTPError):

sentry_sdk/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ def json_dumps(data: "Any") -> bytes:
143143
return json.dumps(data, allow_nan=False, separators=(",", ":")).encode("utf-8")
144144

145145

146+
def serialize_request_body_data(data: "Any") -> str:
147+
def _default(value: "Any") -> "Any":
148+
if isinstance(value, AnnotatedValue):
149+
return value.value
150+
return str(value)
151+
152+
return json.dumps(data, default=_default)
153+
154+
146155
def get_git_revision() -> "Optional[str]":
147156
try:
148157
with open(os.path.devnull, "w+") as null:

0 commit comments

Comments
 (0)