Skip to content

Commit 4fc45bf

Browse files
committed
remove monkey patching for quart for another PR
1 parent e153022 commit 4fc45bf

File tree

2 files changed

+9
-103
lines changed

2 files changed

+9
-103
lines changed

sentry_sdk/integrations/quart.py

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

3-
import inspect
4-
import threading
5-
63
from sentry_sdk.hub import _should_send_default_pii, Hub
74
from sentry_sdk.integrations import DidNotEnable, Integration
85
from sentry_sdk.integrations._wsgi_common import _filter_headers
@@ -14,7 +11,6 @@
1411
event_from_exception,
1512
)
1613

17-
from sentry_sdk._functools import wraps
1814
from sentry_sdk._types import MYPY
1915

2016
if MYPY:
@@ -38,15 +34,13 @@
3834
request,
3935
websocket,
4036
)
41-
from quart.scaffold import Scaffold # type: ignore
4237
from quart.signals import ( # type: ignore
4338
got_background_exception,
4439
got_request_exception,
4540
got_websocket_exception,
4641
request_started,
4742
websocket_started,
4843
)
49-
from quart.utils import is_coroutine_function # type: ignore
5044
except ImportError:
5145
raise DidNotEnable("Quart is not installed")
5246

@@ -77,62 +71,18 @@ def setup_once():
7771
got_request_exception.connect(_capture_exception)
7872
got_websocket_exception.connect(_capture_exception)
7973

80-
patch_asgi_app()
81-
patch_scaffold_route()
82-
83-
84-
def patch_asgi_app():
85-
# type: () -> None
86-
old_app = Quart.__call__
87-
88-
async def sentry_patched_asgi_app(self, scope, receive, send):
89-
# type: (Any, Any, Any, Any) -> Any
90-
if Hub.current.get_integration(QuartIntegration) is None:
91-
return await old_app(self, scope, receive, send)
92-
93-
middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))
94-
middleware.__call__ = middleware._run_asgi3
95-
return await middleware(scope, receive, send)
96-
97-
Quart.__call__ = sentry_patched_asgi_app
98-
99-
100-
def patch_scaffold_route():
101-
# type: () -> None
102-
old_route = Scaffold.route
103-
104-
def _sentry_route(*args, **kwargs):
105-
# type: (*Any, **Any) -> Any
106-
old_decorator = old_route(*args, **kwargs)
107-
108-
def decorator(old_func):
109-
# type: (Any) -> Any
110-
111-
if inspect.isfunction(old_func) and not is_coroutine_function(old_func):
112-
113-
@wraps(old_func)
114-
def _sentry_func(*args, **kwargs):
115-
# type: (*Any, **Any) -> Any
116-
hub = Hub.current
117-
integration = hub.get_integration(QuartIntegration)
118-
if integration is None:
119-
return old_func(*args, **kwargs)
120-
121-
with hub.configure_scope() as sentry_scope:
122-
if sentry_scope.profile is not None:
123-
sentry_scope.profile.active_thread_id = (
124-
threading.current_thread().ident
125-
)
126-
127-
return old_func(*args, **kwargs)
128-
129-
return old_decorator(_sentry_func)
74+
old_app = Quart.__call__
13075

131-
return old_decorator(old_func)
76+
async def sentry_patched_asgi_app(self, scope, receive, send):
77+
# type: (Any, Any, Any, Any) -> Any
78+
if Hub.current.get_integration(QuartIntegration) is None:
79+
return await old_app(self, scope, receive, send)
13280

133-
return decorator
81+
middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))
82+
middleware.__call__ = middleware._run_asgi3
83+
return await middleware(scope, receive, send)
13484

135-
Scaffold.route = _sentry_route
85+
Quart.__call__ = sentry_patched_asgi_app
13686

13787

13888
def _set_transaction_name_and_source(scope, transaction_style, request):

tests/integrations/quart/test_quart.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import json
2-
import threading
3-
41
import pytest
52
import pytest_asyncio
63

@@ -44,20 +41,6 @@ async def hi_with_id(message_id):
4441
capture_message("hi with id")
4542
return "ok with id"
4643

47-
@app.get("/sync/thread_ids")
48-
def _thread_ids_sync():
49-
return {
50-
"main": str(threading.main_thread().ident),
51-
"active": str(threading.current_thread().ident),
52-
}
53-
54-
@app.get("/async/thread_ids")
55-
async def _thread_ids_async():
56-
return {
57-
"main": str(threading.main_thread().ident),
58-
"active": str(threading.current_thread().ident),
59-
}
60-
6144
return app
6245

6346

@@ -540,30 +523,3 @@ async def dispatch_request(self):
540523

541524
assert event["message"] == "hi"
542525
assert event["transaction"] == "hello_class"
543-
544-
545-
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
546-
async def test_active_thread_id(sentry_init, capture_envelopes, endpoint, app):
547-
sentry_init(
548-
traces_sample_rate=1.0,
549-
_experiments={"profiles_sample_rate": 1.0},
550-
)
551-
552-
envelopes = capture_envelopes()
553-
554-
async with app.test_client() as client:
555-
response = await client.get(endpoint)
556-
assert response.status_code == 200
557-
558-
data = json.loads(response.content)
559-
560-
envelopes = [envelope for envelope in envelopes]
561-
assert len(envelopes) == 1
562-
563-
profiles = [item for item in envelopes[0].items if item.type == "profile"]
564-
assert len(profiles) == 1
565-
566-
for profile in profiles:
567-
transactions = profile.payload.json["transactions"]
568-
assert len(transactions) == 1
569-
assert str(data["active"]) == transactions[0]["active_thread_id"]

0 commit comments

Comments
 (0)