Skip to content

Commit c557b26

Browse files
committed
move common function between flask and starlette to more central place
1 parent c74acb1 commit c557b26

3 files changed

Lines changed: 12 additions & 18 deletions

File tree

sentry_sdk/integrations/_wsgi_common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ def _is_json_content_type(ct: "Optional[str]") -> bool:
214214
)
215215

216216

217+
def _serialize_request_body_data(data: "Any") -> str:
218+
def _default(value: "Any") -> "Any":
219+
if isinstance(value, AnnotatedValue):
220+
return value.value
221+
return str(value)
222+
223+
return json.dumps(data, default=_default)
224+
225+
217226
def _filter_headers(
218227
headers: "Mapping[str, str]",
219228
use_annotated_value: bool = True,

sentry_sdk/integrations/flask.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from typing import TYPE_CHECKING
32

43
import sentry_sdk
@@ -7,6 +6,7 @@
76
_RAW_DATA_EXCEPTIONS,
87
DEFAULT_HTTP_METHODS_TO_CAPTURE,
98
RequestExtractor,
9+
_serialize_request_body_data,
1010
request_body_within_bounds,
1111
)
1212
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
@@ -197,14 +197,9 @@ def _set_request_body_data_on_streaming_segment(
197197
else:
198198
return
199199

200-
def _default(value: "Any") -> "Any":
201-
if isinstance(value, AnnotatedValue):
202-
return value.value
203-
return str(value)
204-
205200
current_span._segment.set_attribute(
206201
"http.request.body.data",
207-
json.dumps(data, default=_default),
202+
_serialize_request_body_data(data),
208203
)
209204

210205

sentry_sdk/integrations/starlette.py

Lines changed: 1 addition & 11 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
@@ -18,6 +17,7 @@
1817
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1918
HttpCodeRangeContainer,
2019
_is_json_content_type,
20+
_serialize_request_body_data,
2121
request_body_within_bounds,
2222
)
2323
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
@@ -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:

0 commit comments

Comments
 (0)