diff --git a/sentry_sdk/hub.py b/sentry_sdk/hub.py index 2af3091f5d..8ac2348597 100644 --- a/sentry_sdk/hub.py +++ b/sentry_sdk/hub.py @@ -60,7 +60,7 @@ def overload(x): def _should_send_default_pii(): # type: () -> bool - # TODO: Migrate existing code to client.should_send_default_pii() and remove this function. + # TODO: Migrate existing code to `scope.should_send_default_pii()` and remove this function. # New code should not use this function! client = Hub.current.client if not client: diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 27624ed817..a6fed8f620 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -5,7 +5,7 @@ import sentry_sdk from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.integrations import DidNotEnable -from sentry_sdk.scope import Scope +from sentry_sdk.scope import Scope, should_send_default_pii from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE from sentry_sdk.utils import ( transaction_from_function, @@ -118,10 +118,7 @@ def event_processor(event, hint): # Extract information from request request_info = event.get("request", {}) if info: - if ( - "cookies" in info - and sentry_sdk.get_client().should_send_default_pii() - ): + if "cookies" in info and should_send_default_pii(): request_info["cookies"] = info["cookies"] if "data" in info: request_info["data"] = info["data"] diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 5b92bf7433..b173e13303 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -1658,5 +1658,11 @@ def use_isolation_scope(isolation_scope): _isolation_scope.reset(isolation_token) +def should_send_default_pii(): + # type: () -> bool + """Shortcut for `Scope.get_client().should_send_default_pii()`.""" + return Scope.get_client().should_send_default_pii() + + # Circular imports from sentry_sdk.client import NonRecordingClient diff --git a/tests/test_scope.py b/tests/test_scope.py index a1d7d8c397..d5910a8c1d 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -10,7 +10,13 @@ new_scope, ) from sentry_sdk.client import Client, NonRecordingClient -from sentry_sdk.scope import Scope, ScopeType, use_isolation_scope, use_scope +from sentry_sdk.scope import ( + Scope, + ScopeType, + use_isolation_scope, + use_scope, + should_send_default_pii, +) def test_copying(): @@ -778,3 +784,15 @@ def test_nested_scopes_with_tags(sentry_init, capture_envelopes): assert transaction["tags"] == {"isolation_scope1": 1, "current_scope2": 1, "trx": 1} assert transaction["spans"][0]["tags"] == {"a": 1} assert transaction["spans"][1]["tags"] == {"b": 1} + + +def test_should_send_default_pii_true(sentry_init): + sentry_init(send_default_pii=True) + + assert should_send_default_pii() is True + + +def test_should_send_default_pii_false(sentry_init): + sentry_init(send_default_pii=False) + + assert should_send_default_pii() is False