Skip to content

Commit 883e585

Browse files
authored
feat(pyramid): Set user.id on spans when PII is enabled (#6606)
When span streaming is enabled and `send_default_pii` is true, set the `user.id` attribute on all spans using `scope.set_user` using the authenticated user ID from the Pyramid request. Fixes #6605 Fixes PY-2542
1 parent 907dd48 commit 883e585

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

sentry_sdk/integrations/pyramid.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,22 @@ def setup_once() -> None:
7575
def sentry_patched_call_view(
7676
registry: "Any", request: "Request", *args: "Any", **kwargs: "Any"
7777
) -> "Response":
78-
integration = sentry_sdk.get_client().get_integration(PyramidIntegration)
78+
client = sentry_sdk.get_client()
79+
integration = client.get_integration(PyramidIntegration)
7980
if integration is None:
8081
return old_call_view(registry, request, *args, **kwargs)
8182

8283
_set_transaction_name_and_source(
8384
sentry_sdk.get_current_scope(), integration.transaction_style, request
8485
)
86+
8587
scope = sentry_sdk.get_isolation_scope()
88+
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})
93+
8694
scope.add_event_processor(
8795
_make_event_processor(weakref.ref(request), integration)
8896
)

tests/integrations/pyramid/test_pyramid.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,12 @@ def tracing_error(request):
527527

528528
@pytest.mark.parametrize("span_streaming", [True, False])
529529
def test_span_origin(
530-
sentry_init, capture_events, capture_items, get_client, span_streaming
530+
sentry_init,
531+
pyramid_config,
532+
capture_events,
533+
capture_items,
534+
get_client,
535+
span_streaming,
531536
):
532537
sentry_init(
533538
integrations=[PyramidIntegration()],
@@ -552,3 +557,42 @@ def test_span_origin(
552557
else:
553558
(_, event) = events
554559
assert event["contexts"]["trace"]["origin"] == "auto.http.pyramid"
560+
561+
562+
@pytest.mark.parametrize("send_default_pii", [True, False])
563+
def test_span_sets_user_id_on_segment(
564+
sentry_init,
565+
pyramid_config,
566+
capture_items,
567+
get_client,
568+
send_default_pii,
569+
):
570+
sentry_init(
571+
integrations=[PyramidIntegration()],
572+
traces_sample_rate=1.0,
573+
send_default_pii=send_default_pii,
574+
_experiments={"trace_lifecycle": "stream"},
575+
)
576+
577+
class AuthenticationPolicy:
578+
def authenticated_userid(self, request):
579+
return "123-abc"
580+
581+
pyramid_config.set_authorization_policy(ACLAuthorizationPolicy())
582+
pyramid_config.set_authentication_policy(AuthenticationPolicy())
583+
584+
items = capture_items("span")
585+
586+
client = get_client()
587+
client.get("/message")
588+
589+
sentry_sdk.flush()
590+
spans = [i.payload for i in items if i.type == "span"]
591+
592+
assert len(spans) == 1
593+
(segment,) = spans
594+
595+
if send_default_pii:
596+
assert segment["attributes"]["user.id"] == "123-abc"
597+
else:
598+
assert "user.id" not in segment["attributes"]

0 commit comments

Comments
 (0)