8000 bug(flask): Transactions missing body (#1034) · chdsbd/sentry-python@ed7d722 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed7d722

Browse files
ahmedetefysentry-bot
andauthored
bug(flask): Transactions missing body (getsentry#1034)
* Add test that ensreus transaction includes body data even if no exception was raised * Removed weakref to request that was being gc before it was passed to event_processor * fix: Formatting * Linting fixes Co-authored-by: sentry-bot <markus+ghbot@sentry.io>
1 parent 51987c5 commit ed7d722

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import absolute_import
22

3-
import weakref
4-
53
from sentry_sdk.hub import Hub, _should_send_default_pii
64
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
75
from sentry_sdk.integrations import Integration, DidNotEnable
@@ -113,10 +111,7 @@ def _request_started(sender, **kwargs):
113111
except Exception:
114112
pass
115113

116-
weak_request = weakref.ref(request)
117-
evt_processor = _make_request_event_processor(
118-
app, weak_request, integration # type: ignore
119-
)
114+
evt_processor = _make_request_event_processor(app, request, integration)
120115
scope.add_event_processor(evt_processor)
121116

122117

@@ -157,11 +152,11 @@ def size_of_file(self, file):
157152
return file.content_length
158153

159154

160-
def _make_request_event_processor(app, weak_request, integration):
155+
def _make_request_event_processor(app, request, integration):
161156
# type: (Flask, Callable[[], Request], FlaskIntegration) -> EventProcessor
157+
162158
def inner(event, hint):
163159
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
164-
request = weak_request()
165160

166161
# if the request is gone we are fine not logging the data from
167162
# it. This might happen if the processor is pushed away to

tests/integrations/flask/test_flask.py

Lines changed: 33 additions & 0 deletions
< 401D td data-grid-cell-id="diff-01db75b6a9e974a9450a181c711ba26dbff2f28520e7c4ff2044fbf0efa8af7d-336-369-2" data-line-anchor="diff-01db75b6a9e974a9450a181c711ba26dbff2f28520e7c4ff2044fbf0efa8af7dR369" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
def test_flask_too_large_raw_request(sentry_init, input_char, capture_events, app):
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,39 @@ def index():
332332
assert len(event["request"]["data"]["foo"]) == 512
333333

334334

335+
def test_flask_formdata_request_appear_transaction_body(
336+
sentry_init, capture_events, app
337+
):
338+
"""
339+
Test that ensures that transaction request data contains body, even if no exception was raised
340+
"""
341+
sentry_init(integrations=[flask_sentry.FlaskIntegration()], traces_sample_rate=1.0)
342+
343+
data = {"username": "sentry-user", "age": "26"}
344+
345+
@app.route("/", methods=["POST"])
346+
def index():
347+
assert request.form["username"] == data["username"]
348+
assert request.form["age"] == data["age"]
349+
assert not request.get_data()
350+
assert not request.get_json()
351+
set_tag("view", "yes")
352+
capture_message("hi")
353+
return "ok"
354+
355+
events = capture_events()
356+
357+
client = app.test_client()
358+
response = client.post("/", data=data)
359+
assert response.status_code == 200
360+
361+
event, transaction_event = events
362+
363+
assert "request" in transaction_event
364+
assert "data" in transaction_event["request"]
365+
assert transaction_event["request"]["data"] == data
366+
367+
335368
@pytest.mark.parametrize("input_char", [u"a", b"a"])
336369
337370
sentry_init(integrations=[flask_sentry.FlaskIntegration()], request_bodies="small")

0 commit comments

Comments
 (0)
0