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
23 changes: 18 additions & 5 deletions sentry_sdk/integrations/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
has_data_collection_enabled,
reraise,
)

Expand Down Expand Up @@ -86,10 +87,16 @@ def sentry_patched_call_view(

scope = sentry_sdk.get_isolation_scope()

if should_send_default_pii() and has_span_streaming_enabled(client.options):
user_id = authenticated_userid(request)
if user_id:
scope.set_user({"id": user_id})
if has_span_streaming_enabled(client.options):
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
user_id = authenticated_userid(request)
if user_id:
scope.set_user({"id": user_id})
elif should_send_default_pii():
user_id = authenticated_userid(request)
if user_id:
scope.set_user({"id": user_id})

scope.add_event_processor(
_make_event_processor(weakref.ref(request), integration)
Expand Down Expand Up @@ -229,7 +236,13 @@ def pyramid_event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event":
with capture_internal_exceptions():
PyramidRequestExtractor(request).extract_into_event(event)

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"]:
with capture_internal_exceptions():
user_info = event.setdefault("user", {})
user_info.setdefault("id", authenticated_userid(request))
elif should_send_default_pii():
with capture_internal_exceptions():
user_info = event.setdefault("user", {})
user_info.setdefault("id", authenticated_userid(request))
Expand Down
46 changes: 42 additions & 4 deletions tests/integrations/pyramid/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
from sentry_sdk.traces import SpanStatus
from tests.conftest import unpack_werkzeug_response
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES

try:
from importlib.metadata import version
Expand Down Expand Up @@ -559,19 +560,20 @@ def test_span_origin(
assert event["contexts"]["trace"]["origin"] == "auto.http.pyramid"


@pytest.mark.parametrize("send_default_pii", [True, False])
@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
def test_span_sets_user_id_on_segment(
sentry_init,
pyramid_config,
capture_items,
get_client,
send_default_pii,
init_kwargs,
expect_user,
):
sentry_init(
integrations=[PyramidIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
trace_lifecycle="stream",
**init_kwargs,
)

class AuthenticationPolicy:
Expand All @@ -592,7 +594,43 @@ def authenticated_userid(self, request):
assert len(spans) == 1
(segment,) = spans

if send_default_pii:
if expect_user:
assert segment["attributes"]["user.id"] == "123-abc"
else:
assert "user.id" not in segment["attributes"]


@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
def test_user_id_error_event_data_collection(
sentry_init,
pyramid_config,
capture_events,
route,
get_client,
init_kwargs,
expect_user,
):
sentry_init(integrations=[PyramidIntegration()], **init_kwargs)
events = capture_events()

class AuthenticationPolicy:
def authenticated_userid(self, request):
return "123-abc"

pyramid_config.set_authorization_policy(ACLAuthorizationPolicy())
pyramid_config.set_authentication_policy(AuthenticationPolicy())

@route("/crash")
def crash(request):
1 / 0

client = get_client()
with pytest.raises(ZeroDivisionError):
client.get("/crash")

(event,) = events

if expect_user:
assert event["user"]["id"] == "123-abc"
else:
assert "id" not in event.get("user", {})
Loading