Skip to content

Commit 8b28bef

Browse files
committed
Revert changes as they were centred around adding the request body, which we are no longer doing. Add test coverage to ensure span streaming is working as expected
1 parent 234e667 commit 8b28bef

4 files changed

Lines changed: 102 additions & 487 deletions

File tree

sentry_sdk/integrations/_wsgi_common.py

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

211211

212-
def _serialize_request_body_data(data: "Any") -> str:
213-
def _default(value: "Any") -> "Any":
214-
if isinstance(value, AnnotatedValue):
215-
return value.value
216-
return str(value)
217-
218-
return json.dumps(data, default=_default)
219-
220-
221212
def _filter_headers(
222213
headers: "Mapping[str, str]",
223214
use_annotated_value: bool = True,

sentry_sdk/integrations/flask.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
from sentry_sdk.integrations._wsgi_common import (
66
DEFAULT_HTTP_METHODS_TO_CAPTURE,
77
RequestExtractor,
8-
_serialize_request_body_data,
9-
request_body_within_bounds,
108
)
119
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1210
from sentry_sdk.scope import should_send_default_pii
13-
from sentry_sdk.traces import StreamedSpan, _get_current_streamed_span
1411
from sentry_sdk.tracing import SOURCE_FOR_STYLE
15-
from sentry_sdk.tracing_utils import has_span_streaming_enabled
1612
from sentry_sdk.utils import (
17-
AnnotatedValue,
1813
capture_internal_exceptions,
1914
ensure_integration_enabled,
2015
event_from_exception,
@@ -41,11 +36,9 @@
4136
from flask.signals import (
4237
before_render_template,
4338
got_request_exception,
44-
request_finished,
4539
request_started,
4640
)
4741
from markupsafe import Markup
48-
from werkzeug.exceptions import ClientDisconnected
4942
except ImportError:
5043
raise DidNotEnable("Flask is not installed")
5144

@@ -95,7 +88,6 @@ def setup_once() -> None:
9588

9689
before_render_template.connect(_add_sentry_trace)
9790
request_started.connect(_request_started)
98-
request_finished.connect(_request_finished)
9991
got_request_exception.connect(_capture_exception)
10092

10193
old_app = Flask.__call__
@@ -167,72 +159,6 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None:
167159
scope.add_event_processor(evt_processor)
168160

169161

170-
def _request_finished(sender: "Flask", response: "Any", **kwargs: "Any") -> None:
171-
integration = sentry_sdk.get_client().get_integration(FlaskIntegration)
172-
if integration is None:
173-
return
174-
175-
client = sentry_sdk.get_client()
176-
if has_span_streaming_enabled(client.options):
177-
request = flask_request._get_current_object()
178-
_set_request_body_data_on_streaming_segment(request, client)
179-
180-
181-
def _set_request_body_data_on_streaming_segment(
182-
request: "Request", client: "sentry_sdk.client.BaseClient"
183-
) -> None:
184-
current_span = _get_current_streamed_span()
185-
if type(current_span) is not StreamedSpan:
186-
return
187-
188-
with capture_internal_exceptions():
189-
content_length = int(request.content_length or 0)
190-
191-
# Proceeding without a content length means that we may be consuming the request
192-
# without respecting the bounds specified by the user via `max_request_body_size`
193-
# option in the SDK.
194-
if not content_length:
195-
return
196-
197-
if not request_body_within_bounds(client, content_length):
198-
data = AnnotatedValue.substituted_because_over_size_limit()
199-
else:
200-
raw_data = getattr(request, "_cached_data", None)
201-
parsed_body = None
202-
if "form" in request.__dict__:
203-
extractor = FlaskRequestExtractor(request)
204-
parsed_body = extractor.parsed_body()
205-
elif raw_data is not None:
206-
extractor = FlaskRequestExtractor(request)
207-
if extractor.is_json():
208-
parsed_body = extractor.json()
209-
else:
210-
# The route never read the body via Werkzeug, but it
211-
# may have consumed wsgi.input directly. get_data()
212-
# raises ClientDisconnected if the stream is exhausted.
213-
try:
214-
raw_data = request.get_data()
215-
except ClientDisconnected:
216-
raw_data = None
217-
218-
if raw_data:
219-
extractor = FlaskRequestExtractor(request)
220-
if extractor.is_json():
221-
parsed_body = extractor.json()
222-
223-
if parsed_body is not None:
224-
data = parsed_body
225-
elif raw_data:
226-
data = AnnotatedValue.substituted_because_raw_data()
227-
else:
228-
return
229-
230-
current_span._segment.set_attribute(
231-
"http.request.body.data",
232-
_serialize_request_body_data(data),
233-
)
234-
235-
236162
class FlaskRequestExtractor(RequestExtractor):
237163
def env(self) -> "Dict[str, str]":
238164
return self.request.environ

sentry_sdk/integrations/starlette.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import json
23
import sys
34
import warnings
45
from collections.abc import Set
@@ -17,7 +18,6 @@
1718
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1819
HttpCodeRangeContainer,
1920
_is_json_content_type,
20-
_serialize_request_body_data,
2121
request_body_within_bounds,
2222
)
2323
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
@@ -241,6 +241,16 @@ async def _sentry_send(*args: "Any", **kwargs: "Any") -> "Any":
241241
return middleware_class
242242

243243

244+
def _serialize_request_body_data(data: "Any") -> str:
245+
# data may be a JSON-serializable value, an AnnotatedValue, or a dict with AnnotatedValue values
246+
def _default(value: "Any") -> "Any":
247+
if isinstance(value, AnnotatedValue):
248+
return value.value
249+
return str(value)
250+
251+
return json.dumps(data, default=_default)
252+
253+
244254
def _set_request_body_data_on_streaming_segment(
245255
info: "Optional[Dict[str, Any]]",
246256
) -> None:

0 commit comments

Comments
 (0)