Skip to content

Commit 1bed4e2

Browse files
authored
feat(pyramid): Gate user info behind data_collection config (#6873)
Apply the data_collection.user_info setting to the Pyramid integration's user id collection, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This covers both the user.id segment attribute under span streaming and the user id attached to error events, mirroring the behavior already shipped for Flask, WSGI, ASGI, Django, Sanic, and Tornado. The user id segment test is reparametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py, and a new test adds the first coverage of the user id on error events. Refs PY-2583 Refs #6746
1 parent ed31d89 commit 1bed4e2

2 files changed

Lines changed: 60 additions & 9 deletions

File tree

sentry_sdk/integrations/pyramid.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
capture_internal_exceptions,
1616
ensure_integration_enabled,
1717
event_from_exception,
18+
has_data_collection_enabled,
1819
reraise,
1920
)
2021

@@ -86,10 +87,16 @@ def sentry_patched_call_view(
8687

8788
scope = sentry_sdk.get_isolation_scope()
8889

89-
if should_send_default_pii() and has_span_streaming_enabled(client.options):
90-
user_id = authenticated_userid(request)
91-
if user_id:
92-
scope.set_user({"id": user_id})
90+
if has_span_streaming_enabled(client.options):
91+
if has_data_collection_enabled(client.options):
92+
if client.options["data_collection"]["user_info"]:
93+
user_id = authenticated_userid(request)
94+
if user_id:
95+
scope.set_user({"id": user_id})
96+
elif should_send_default_pii():
97+
user_id = authenticated_userid(request)
98+
if user_id:
99+
scope.set_user({"id": user_id})
93100

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

232-
if should_send_default_pii():
239+
client_options = sentry_sdk.get_client().options
240+
if has_data_collection_enabled(client_options):
241+
if client_options["data_collection"]["user_info"]:
242+
with capture_internal_exceptions():
243+
user_info = event.setdefault("user", {})
244+
user_info.setdefault("id", authenticated_userid(request))
245+
elif should_send_default_pii():
233246
with capture_internal_exceptions():
234247
user_info = event.setdefault("user", {})
235248
user_info.setdefault("id", authenticated_userid(request))

tests/integrations/pyramid/test_pyramid.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
1616
from sentry_sdk.traces import SpanStatus
1717
from tests.conftest import unpack_werkzeug_response
18+
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES
1819

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

561562

562-
@pytest.mark.parametrize("send_default_pii", [True, False])
563+
@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
563564
def test_span_sets_user_id_on_segment(
564565
sentry_init,
565566
pyramid_config,
566567
capture_items,
567568
get_client,
568-
send_default_pii,
569+
init_kwargs,
570+
expect_user,
569571
):
570572
sentry_init(
571573
integrations=[PyramidIntegration()],
572574
traces_sample_rate=1.0,
573-
send_default_pii=send_default_pii,
574575
trace_lifecycle="stream",
576+
**init_kwargs,
575577
)
576578

577579
class AuthenticationPolicy:
@@ -592,7 +594,43 @@ def authenticated_userid(self, request):
592594
assert len(spans) == 1
593595
(segment,) = spans
594596

595-
if send_default_pii:
597+
if expect_user:
596598
assert segment["attributes"]["user.id"] == "123-abc"
597599
else:
598600
assert "user.id" not in segment["attributes"]
601+
602+
603+
@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
604+
def test_user_id_error_event_data_collection(
605+
sentry_init,
606+
pyramid_config,
607+
capture_events,
608+
route,
609+
get_client,
610+
init_kwargs,
611+
expect_user,
612+
):
613+
sentry_init(integrations=[PyramidIntegration()], **init_kwargs)
614+
events = capture_events()
615+
616+
class AuthenticationPolicy:
617+
def authenticated_userid(self, request):
618+
return "123-abc"
619+
620+
pyramid_config.set_authorization_policy(ACLAuthorizationPolicy())
621+
pyramid_config.set_authentication_policy(AuthenticationPolicy())
622+
623+
@route("/crash")
624+
def crash(request):
625+
1 / 0
626+
627+
client = get_client()
628+
with pytest.raises(ZeroDivisionError):
629+
client.get("/crash")
630+
631+
(event,) = events
632+
633+
if expect_user:
634+
assert event["user"]["id"] == "123-abc"
635+
else:
636+
assert "id" not in event.get("user", {})

0 commit comments

Comments
 (0)