From a3206d07cd50b17e516cbbee9eb2d15eba35d14b Mon Sep 17 00:00:00 2001 From: Ahmed Etefy Date: Fri, 26 Feb 2021 10:41:09 +0100 Subject: [PATCH 1/4] Add test that ensreus transaction includes body data even if no exception was raised --- tests/integrations/flask/test_flask.py | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index d155e74a98..57992b3744 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -332,6 +332,38 @@ def index(): assert len(event["request"]["data"]["foo"]) == 512 +def test_flask_formdata_request_appear_transaction_body(sentry_init, capture_events, app): + """ + Test that ensures that transaction request data contains body, even if no exception was raised + """ + sentry_init(integrations=[flask_sentry.FlaskIntegration()], traces_sample_rate=1.0) + + data = {"username": "sentry-user", + "age": "26"} + + @app.route("/", methods=["POST"]) + def index(): + assert request.form["username"] == data["username"] + assert request.form["age"] == data["age"] + assert not request.get_data() + assert not request.get_json() + set_tag("view", "yes") + capture_message("hi") + return "ok" + + events = capture_events() + + client = app.test_client() + response = client.post("/", data=data) + assert response.status_code == 200 + + event, transaction_event = events + + assert "request" in transaction_event + assert "data" in transaction_event["request"] + assert transaction_event["request"]["data"] == data + + @pytest.mark.parametrize("input_char", [u"a", b"a"]) def test_flask_too_large_raw_request(sentry_init, input_char, capture_events, app): sentry_init(integrations=[flask_sentry.FlaskIntegration()], request_bodies="small") From 9691f96b7072bd1a8785a60790d3a5a53ed10604 Mon Sep 17 00:00:00 2001 From: Ahmed Etefy Date: Fri, 26 Feb 2021 11:26:28 +0100 Subject: [PATCH 2/4] Removed weakref to request that was being gc before it was passed to event_processor --- sentry_sdk/integrations/flask.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index 2d0883ab8a..1ce10d82b5 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -1,7 +1,5 @@ from __future__ import absolute_import -import weakref - from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.utils import capture_internal_exceptions, event_from_exception from sentry_sdk.integrations import Integration, DidNotEnable @@ -113,9 +111,8 @@ def _request_started(sender, **kwargs): except Exception: pass - weak_request = weakref.ref(request) evt_processor = _make_request_event_processor( - app, weak_request, integration # type: ignore + app, request, integration # type: ignore ) scope.add_event_processor(evt_processor) @@ -157,11 +154,11 @@ def size_of_file(self, file): return file.content_length -def _make_request_event_processor(app, weak_request, integration): +def _make_request_event_processor(app, request, integration): # type: (Flask, Callable[[], Request], FlaskIntegration) -> EventProcessor + def inner(event, hint): # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any] - request = weak_request() # if the request is gone we are fine not logging the data from # it. This might happen if the processor is pushed away to From 231b18b951d923c3c997f4eebe95aac047ab20ff Mon Sep 17 00:00:00 2001 From: sentry-bot Date: Fri, 26 Feb 2021 10:27:40 +0000 Subject: [PATCH 3/4] fix: Formatting --- tests/integrations/flask/test_flask.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 57992b3744..6c173e223d 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -332,14 +332,15 @@ def index(): assert len(event["request"]["data"]["foo"]) == 512 -def test_flask_formdata_request_appear_transaction_body(sentry_init, capture_events, app): +def test_flask_formdata_request_appear_transaction_body( + sentry_init, capture_events, app +): """ Test that ensures that transaction request data contains body, even if no exception was raised """ sentry_init(integrations=[flask_sentry.FlaskIntegration()], traces_sample_rate=1.0) - data = {"username": "sentry-user", - "age": "26"} + data = {"username": "sentry-user", "age": "26"} @app.route("/", methods=["POST"]) def index(): From 9a8cba8b3257a6378471db5140cccd9d0c9ee244 Mon Sep 17 00:00:00 2001 From: Ahmed Etefy Date: Fri, 26 Feb 2021 11:37:37 +0100 Subject: [PATCH 4/4] Linting fixes --- sentry_sdk/integrations/flask.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index 1ce10d82b5..f1856ed515 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -111,9 +111,7 @@ def _request_started(sender, **kwargs): except Exception: pass - evt_processor = _make_request_event_processor( - app, request, integration # type: ignore - ) + evt_processor = _make_request_event_processor(app, request, integration) scope.add_event_processor(evt_processor)