Skip to content

fix(flask): Remove double-scope #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
__version__ as FLASK_VERSION,
)
from flask.signals import (
appcontext_pushed,
appcontext_tearing_down,
got_request_exception,
request_started,
)
Expand Down Expand Up @@ -74,8 +72,6 @@ def setup_once():
if version < (0, 11):
raise DidNotEnable("Flask 0.11 or newer is required.")

appcontext_pushed.connect(_push_appctx)
appcontext_tearing_down.connect(_pop_appctx)
request_started.connect(_request_started)
got_request_exception.connect(_capture_exception)

Expand All @@ -93,26 +89,6 @@ def sentry_patched_wsgi_app(self, environ, start_response):
Flask.__call__ = sentry_patched_wsgi_app # type: ignore


def _push_appctx(*args, **kwargs):
# type: (*Flask, **Any) -> None
hub = Hub.current
if hub.get_integration(FlaskIntegration) is not None:
# always want to push scope regardless of whether WSGI app might already
# have (not the case for CLI for example)
scope_manager = hub.push_scope()
scope_manager.__enter__()
_app_ctx_stack.top.sentry_sdk_scope_manager = scope_manager
with hub.configure_scope() as scope:
scope._name = "flask"


def _pop_appctx(*args, **kwargs):
# type: (*Flask, **Any) -> None
scope_manager = getattr(_app_ctx_stack.top, "sentry_sdk_scope_manager", None)
if scope_manager is not None:
scope_manager.__exit__(None, None, None)


def _request_started(sender, **kwargs):
# type: (Flask, **Any) -> None
hub = Hub.current
Expand Down
21 changes: 18 additions & 3 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flask_login import LoginManager, login_user

from sentry_sdk import (
set_tag,
configure_scope,
capture_message,
capture_exception,
Expand Down Expand Up @@ -630,20 +631,34 @@ def zerodivision(e):
def test_tracing_success(sentry_init, capture_events, app):
sentry_init(traces_sample_rate=1.0, integrations=[flask_sentry.FlaskIntegration()])

@app.before_request
def _():
set_tag("before_request", "yes")

@app.route("/message_tx")
def hi_tx():
set_tag("view", "yes")
capture_message("hi")
return "ok"

events = capture_events()

with app.test_client() as client:
response = client.get("/message")
response = client.get("/message_tx")
assert response.status_code == 200

message_event, transaction_event = events

assert transaction_event["type"] == "transaction"
assert transaction_event["transaction"] == "hi"
assert transaction_event["transaction"] == "hi_tx"
assert transaction_event["contexts"]["trace"]["status"] == "ok"
assert transaction_event["tags"]["view"] == "yes"
assert transaction_event["tags"]["before_request"] == "yes"

assert message_event["message"] == "hi"
assert message_event["transaction"] == "hi"
assert message_event["transaction"] == "hi_tx"
assert message_event["tags"]["view"] == "yes"
assert message_event["tags"]["before_request"] == "yes"


def test_tracing_error(sentry_init, capture_events, app):
Expand Down