Skip to content

Commit

Permalink
bug(flask): Transactions missing body (#1034)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ahmedetefy and sentry-bot authored Mar 2, 2021
1 parent 51987c5 commit ed7d722
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
11 changes: 3 additions & 8 deletions sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -113,10 +111,7 @@ 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
)
evt_processor = _make_request_event_processor(app, request, integration)
scope.add_event_processor(evt_processor)


Expand Down Expand Up @@ -157,11 +152,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
Expand Down
33 changes: 33 additions & 0 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,39 @@ 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")
Expand Down

0 comments on commit ed7d722

Please sign in to comment.