Skip to content
Merged
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
48 changes: 21 additions & 27 deletions sentry_sdk/integrations/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import threading
from functools import wraps

from sentry_sdk.hub import _should_send_default_pii, Hub
import sentry_sdk
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.scope import Scope
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.tracing import SOURCE_FOR_STYLE
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
)
from sentry_sdk._types import TYPE_CHECKING
Expand Down Expand Up @@ -86,11 +87,9 @@ def patch_asgi_app():
# type: () -> None
old_app = Quart.__call__

@ensure_integration_enabled(QuartIntegration, old_app)
async def sentry_patched_asgi_app(self, scope, receive, send):
# type: (Any, Any, Any, Any) -> Any
if Hub.current.get_integration(QuartIntegration) is None:
return await old_app(self, scope, receive, send)

middleware = SentryAsgiMiddleware(lambda *a, **kw: old_app(self, *a, **kw))
middleware.__call__ = middleware._run_asgi3
return await middleware(scope, receive, send)
Expand All @@ -116,18 +115,19 @@ def decorator(old_func):
@wraps(old_func)
def _sentry_func(*args, **kwargs):
# type: (*Any, **Any) -> Any
hub = Hub.current
integration = hub.get_integration(QuartIntegration)
integration = sentry_sdk.get_client().get_integration(
QuartIntegration
)
if integration is None:
return old_func(*args, **kwargs)

with hub.configure_scope() as sentry_scope:
if sentry_scope.profile is not None:
sentry_scope.profile.active_thread_id = (
threading.current_thread().ident
)
scope = Scope.get_isolation_scope()
if scope.profile is not None:
scope.profile.active_thread_id = (
threading.current_thread().ident
)
Comment on lines +124 to +128

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this threading stuff go to the isolation scope? Or would current scope make more sense?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess isolation scope is fine. Other integrations also do it on the isolation scope (ex fastapi). One request will probably be handled by on thread in quart.


return old_func(*args, **kwargs)
return old_func(*args, **kwargs)

return old_decorator(_sentry_func)

Expand Down Expand Up @@ -156,8 +156,7 @@ def _set_transaction_name_and_source(scope, transaction_style, request):

async def _request_websocket_started(app, **kwargs):
# type: (Quart, **Any) -> None
hub = Hub.current
integration = hub.get_integration(QuartIntegration)
integration = sentry_sdk.get_client().get_integration(QuartIntegration)
if integration is None:
return

Expand All @@ -172,11 +171,9 @@ async def _request_websocket_started(app, **kwargs):
Scope.get_current_scope(), integration.transaction_style, request_websocket
)

with hub.configure_scope() as scope:
evt_processor = _make_request_event_processor(
app, request_websocket, integration
)
scope.add_event_processor(evt_processor)
scope = Scope.get_isolation_scope()
evt_processor = _make_request_event_processor(app, request_websocket, integration)
scope.add_event_processor(evt_processor)


def _make_request_event_processor(app, request, integration):
Expand All @@ -199,7 +196,7 @@ def inner(event, hint):
request_info["method"] = request.method
request_info["headers"] = _filter_headers(dict(request.headers))

if _should_send_default_pii():
if should_send_default_pii():
request_info["env"] = {"REMOTE_ADDR": request.access_route[0]}
_add_user_to_event(event)

Expand All @@ -210,20 +207,17 @@ def inner(event, hint):

async def _capture_exception(sender, exception, **kwargs):
# type: (Quart, Union[ValueError, BaseException], **Any) -> None
hub = Hub.current
if hub.get_integration(QuartIntegration) is None:
client = sentry_sdk.get_client()
if client.get_integration(QuartIntegration) is None:
return

# If an integration is there, a client has to be there.
client = hub.client # type: Any

event, hint = event_from_exception(
exception,
client_options=client.options,
mechanism={"type": "quart", "handled": False},
)

hub.capture_event(event, hint=hint)
sentry_sdk.capture_event(event, hint=hint)


def _add_user_to_event(event):
Expand Down