Skip to content

Commit 7c43f6f

Browse files
authored
ref(flask): Use new scopes API in Flask (#2863)
1 parent 431a521 commit 7c43f6f

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import sentry_sdk
12
from sentry_sdk._types import TYPE_CHECKING
2-
from sentry_sdk.hub import Hub, _should_send_default_pii
33
from sentry_sdk.integrations import DidNotEnable, Integration
44
from sentry_sdk.integrations._wsgi_common import RequestExtractor
55
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
6-
from sentry_sdk.scope import Scope
6+
from sentry_sdk.scope import Scope, should_send_default_pii
77
from sentry_sdk.tracing import SOURCE_FOR_STYLE
88
from sentry_sdk.utils import (
99
capture_internal_exceptions,
10+
ensure_integration_enabled,
1011
event_from_exception,
1112
package_version,
1213
)
@@ -75,11 +76,9 @@ def setup_once():
7576

7677
old_app = Flask.__call__
7778

79+
@ensure_integration_enabled(FlaskIntegration, old_app)
7880
def sentry_patched_wsgi_app(self, environ, start_response):
7981
# type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
80-
if Hub.current.get_integration(FlaskIntegration) is None:
81-
return old_app(self, environ, start_response)
82-
8382
return SentryWsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))(
8483
environ, start_response
8584
)
@@ -92,8 +91,8 @@ def _add_sentry_trace(sender, template, context, **extra):
9291
if "sentry_trace" in context:
9392
return
9493

95-
hub = Hub.current
96-
trace_meta = Markup(hub.trace_propagation_meta())
94+
scope = Scope.get_current_scope()
95+
trace_meta = Markup(scope.trace_propagation_meta())
9796
context["sentry_trace"] = trace_meta # for backwards compatibility
9897
context["sentry_trace_meta"] = trace_meta
9998

@@ -115,8 +114,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
115114

116115
def _request_started(app, **kwargs):
117116
# type: (Flask, **Any) -> None
118-
hub = Hub.current
119-
integration = hub.get_integration(FlaskIntegration)
117+
integration = sentry_sdk.get_client().get_integration(FlaskIntegration)
120118
if integration is None:
121119
return
122120

@@ -128,9 +126,10 @@ def _request_started(app, **kwargs):
128126
Scope.get_current_scope(), integration.transaction_style, request
129127
)
130128

131-
with hub.configure_scope() as scope:
132-
evt_processor = _make_request_event_processor(app, request, integration)
133-
scope.add_event_processor(evt_processor)
129+
scope = Scope.get_isolation_scope()
130+
scope.generate_propagation_context()
131+
evt_processor = _make_request_event_processor(app, request, integration)
132+
scope.add_event_processor(evt_processor)
134133

135134

136135
class FlaskRequestExtractor(RequestExtractor):
@@ -185,7 +184,7 @@ def inner(event, hint):
185184
with capture_internal_exceptions():
186185
FlaskRequestExtractor(request).extract_into_event(event)
187186

188-
if _should_send_default_pii():
187+
if should_send_default_pii():
189188
with capture_internal_exceptions():
190189
_add_user_to_event(event)
191190

@@ -196,20 +195,17 @@ def inner(event, hint):
196195

197196
def _capture_exception(sender, exception, **kwargs):
198197
# type: (Flask, Union[ValueError, BaseException], **Any) -> None
199-
hub = Hub.current
200-
if hub.get_integration(FlaskIntegration) is None:
198+
client = sentry_sdk.get_client()
199+
if client.get_integration(FlaskIntegration) is None:
201200
return
202201

203-
# If an integration is there, a client has to be there.
204-
client = hub.client # type: Any
205-
206202
event, hint = event_from_exception(
207203
exception,
208204
client_options=client.options,
209205
mechanism={"type": "flask", "handled": False},
210206
)
211207

212-
hub.capture_event(event, hint=hint)
208+
sentry_sdk.capture_event(event, hint=hint)
213209

214210

215211
def _add_user_to_event(event):

tests/integrations/flask/test_flask.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
except ImportError:
2121
UnsupportedMediaType = None
2222

23+
import sentry_sdk
2324
import sentry_sdk.integrations.flask as flask_sentry
2425
from sentry_sdk import (
2526
set_tag,
2627
configure_scope,
2728
capture_message,
2829
capture_exception,
29-
Hub,
3030
)
3131
from sentry_sdk.integrations.logging import LoggingIntegration
32+
from sentry_sdk.scope import Scope
3233
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
3334

3435

@@ -294,7 +295,7 @@ def index():
294295
except ZeroDivisionError:
295296
pass
296297

297-
Hub.current.client.flush()
298+
sentry_sdk.get_client().flush()
298299

299300
(first_event, error_event, session) = envelopes
300301
first_event = first_event.get_event()
@@ -838,8 +839,10 @@ def test_template_tracing_meta(sentry_init, app, capture_events, template_string
838839

839840
@app.route("/")
840841
def index():
841-
hub = Hub.current
842-
capture_message(hub.get_traceparent() + "\n" + hub.get_baggage())
842+
scope = Scope.get_isolation_scope()
843+
capture_message(
844+
scope.get_traceparent() + "\n" + scope.get_baggage().serialize()
845+
)
843846
return render_template_string(template_string)
844847

845848
with app.test_client() as client:
@@ -916,7 +919,7 @@ def test_response_status_code_ok_in_transaction_context(
916919
client = app.test_client()
917920
client.get("/message")
918921

919-
Hub.current.client.flush()
922+
sentry_sdk.get_client().flush()
920923

921924
(_, transaction_envelope, _) = envelopes
922925
transaction = transaction_envelope.get_transaction_event()
@@ -943,7 +946,7 @@ def test_response_status_code_not_found_in_transaction_context(
943946
client = app.test_client()
944947
client.get("/not-existing-route")
945948

946-
Hub.current.client.flush()
949+
sentry_sdk.get_client().flush()
947950

948951
(transaction_envelope, _) = envelopes
949952
transaction = transaction_envelope.get_transaction_event()

0 commit comments

Comments
 (0)