Skip to content
Open
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
8 changes: 7 additions & 1 deletion sentry_sdk/integrations/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,14 @@ def retrieve_user_from_scope(scope: "LitestarScope") -> "Optional[dict[str, Any]
@ensure_integration_enabled(LitestarIntegration)
def exception_handler(exc: Exception, scope: "LitestarScope") -> None:
user_info: "Optional[dict[str, Any]]" = None
if should_send_default_pii():
client_options = sentry_sdk.get_client().options

if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
user_info = retrieve_user_from_scope(scope)
elif should_send_default_pii():
user_info = retrieve_user_from_scope(scope)

if user_info and isinstance(user_info, dict):
sentry_scope = sentry_sdk.get_isolation_scope()
sentry_scope.set_user(user_info)
Expand Down
8 changes: 7 additions & 1 deletion sentry_sdk/integrations/starlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,14 @@ def retrieve_user_from_scope(scope: "StarliteScope") -> "Optional[dict[str, Any]
@ensure_integration_enabled(StarliteIntegration)
def exception_handler(exc: Exception, scope: "StarliteScope", _: "State") -> None:
user_info: "Optional[dict[str, Any]]" = None
if should_send_default_pii():
client_options = sentry_sdk.get_client().options

if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
user_info = retrieve_user_from_scope(scope)
elif should_send_default_pii():
user_info = retrieve_user_from_scope(scope)

if user_info and isinstance(user_info, dict):
sentry_scope = sentry_sdk.get_isolation_scope()
sentry_scope.set_user(user_info)
Expand Down
20 changes: 6 additions & 14 deletions tests/integrations/litestar/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sentry_sdk.integrations.litestar import LitestarIntegration
from tests.conftest import ApproxDict
from tests.integrations.conftest import parametrize_test_configurable_status_codes
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES


def litestar_app_factory(middleware=None, debug=True, exception_handlers=None):
Expand Down Expand Up @@ -611,24 +612,15 @@ def test_span_origin(
assert span["origin"] == "auto.http.litestar"


@pytest.mark.parametrize(
"is_send_default_pii",
[
True,
False,
],
ids=[
"send_default_pii=True",
"send_default_pii=False",
],
)
@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_litestar_scope_user_on_exception_event(
sentry_init,
capture_exceptions,
capture_events,
capture_items,
is_send_default_pii,
init_kwargs,
expect_user,
span_streaming,
):
class TestUserMiddleware(AbstractMiddleware):
Expand All @@ -642,8 +634,8 @@ async def __call__(self, scope, receive, send):

sentry_init(
integrations=[LitestarIntegration()],
send_default_pii=is_send_default_pii,
trace_lifecycle="stream" if span_streaming else "static",
**init_kwargs,
)

litestar_app = litestar_app_factory(middleware=[TestUserMiddleware])
Expand Down Expand Up @@ -673,7 +665,7 @@ async def __call__(self, scope, receive, send):
assert len(events) == 1
(event,) = events

if is_send_default_pii:
if expect_user:
assert "user" in event
assert event["user"] == {
"email": "lennon@thebeatles.com",
Expand Down
21 changes: 5 additions & 16 deletions tests/integrations/starlite/test_starlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from sentry_sdk import capture_message
from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE
from sentry_sdk.integrations.starlite import StarliteIntegration
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES


def starlite_app_factory(middleware=None, debug=True, exception_handlers=None):
Expand Down Expand Up @@ -525,19 +526,9 @@ def test_span_origin(sentry_init, capture_events, capture_items, span_streaming)
assert span["origin"] == "auto.http.starlite"


@pytest.mark.parametrize(
"is_send_default_pii",
[
True,
False,
],
ids=[
"send_default_pii=True",
"send_default_pii=False",
],
)
@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
def test_starlite_scope_user_on_exception_event(
sentry_init, capture_exceptions, capture_events, is_send_default_pii
sentry_init, capture_exceptions, capture_events, init_kwargs, expect_user
):
class TestUserMiddleware(AbstractMiddleware):
async def __call__(self, scope, receive, send):
Expand All @@ -548,9 +539,7 @@ async def __call__(self, scope, receive, send):
}
await self.app(scope, receive, send)

sentry_init(
integrations=[StarliteIntegration()], send_default_pii=is_send_default_pii
)
sentry_init(integrations=[StarliteIntegration()], **init_kwargs)
starlite_app = starlite_app_factory(middleware=[TestUserMiddleware])
exceptions = capture_exceptions()
events = capture_events()
Expand All @@ -566,7 +555,7 @@ async def __call__(self, scope, receive, send):
assert len(events) == 1
(event,) = events

if is_send_default_pii:
if expect_user:
assert "user" in event
assert event["user"] == {
"email": "lennon@thebeatles.com",
Expand Down
Loading