Skip to content

feat(pyramid): Set user.id on spans when PII is enabled#6606

Merged
ericapisani merged 3 commits into
masterfrom
py-2402-set-user-id-on-segment
Jun 19, 2026
Merged

feat(pyramid): Set user.id on spans when PII is enabled#6606
ericapisani merged 3 commits into
masterfrom
py-2402-set-user-id-on-segment

Conversation

@ericapisani

@ericapisani ericapisani commented Jun 18, 2026

Copy link
Copy Markdown
Member

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

When span streaming is enabled and send_default_pii is true, set
the user.id attribute on the segment span using the authenticated
user ID from the Pyramid request.

Fixes PY-2402
Fixes #6605
@linear-code

linear-code Bot commented Jun 18, 2026

Copy link
Copy Markdown

PY-2402

PY-2542

@ericapisani ericapisani marked this pull request as ready for review June 18, 2026 19:12
@ericapisani ericapisani requested a review from a team as a code owner June 18, 2026 19:12
@ericapisani ericapisani marked this pull request as draft June 18, 2026 19:15
Comment thread sentry_sdk/integrations/pyramid.py Outdated
Comment on lines +89 to +94
if should_send_default_pii() and has_span_streaming_enabled(client.options):
current_span = current_scope.streamed_span
user_id = authenticated_userid(request)

if user_id and type(current_span) is StreamedSpan:
current_span._segment.set_attribute("user.id", user_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The call to authenticated_userid(request) is not wrapped in an exception handler, which can crash the view handler if the authentication policy raises an exception.
Severity: HIGH

Suggested Fix

Wrap the call to authenticated_userid(request) and the subsequent span attribute setting within a with capture_internal_exceptions(): block. This will prevent exceptions from the authentication policy from crashing the application, aligning with the established pattern in the integration.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/pyramid.py#L89-L94

Potential issue: In the `sentry_patched_call_view` function, the call to
`authenticated_userid(request)` is not wrapped in an exception handler. Custom Pyramid
authentication policies can raise exceptions, for example, during a database failure.
Because this instrumentation runs in a critical request path, an unhandled exception
from `authenticated_userid` will propagate and crash the application's view handler.
This can occur in production when `send_default_pii` is true and span streaming is
enabled. The existing pattern in `_make_event_processor` correctly wraps this call in
`capture_internal_exceptions()` to prevent such failures.

Did we get this right? 👍 / 👎 to inform future reviews.

@ericapisani ericapisani marked this pull request as ready for review June 18, 2026 19:20
@ericapisani ericapisani changed the title feat(pyramid): Set user.id on segment span when PII is enabled feat(pyramid): Set user.id on spans when PII is enabled Jun 18, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cad10e8. Configure here.

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})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auth policy errors abort views

Medium Severity

With span streaming and send_default_pii enabled, authenticated_userid(request) runs in _call_view without capture_internal_exceptions. The Pyramid event processor wraps the same call, so an authentication policy that raises can fail the whole view before the handler runs, instead of only skipping user data on telemetry.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cad10e8. Configure here.

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})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The call to scope.set_user({"id": user_id}) in the Pyramid integration overwrites any pre-existing user data on the scope, instead of merging with it.
Severity: LOW

Suggested Fix

Modify the implementation to merge with existing user data instead of overwriting it. Fetch the current user data from the scope, merge the new {"id": user_id} into it, and then call scope.set_user with the complete, merged dictionary. This follows the safer pattern seen in the Quart integration.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/pyramid.py#L92

Potential issue: In the Pyramid integration, `scope.set_user` is called with only the
user ID (`{"id": user_id}`). This action replaces the entire `_user` dictionary on the
isolation scope. If any other middleware or integration has previously set user
properties like email or username, that information will be lost. Consequently,
subsequent telemetry spans will only contain the `user.id` attribute, losing other
valuable user context. This behavior is inconsistent with other integrations, such as
Quart, which correctly merge new user data with existing properties.

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

90826 passed | ⏭️ 6129 skipped | Total: 96955 | Pass Rate: 93.68% | Execution Time: 314m 53s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +16
Passed Tests 📈 +16
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2410 uncovered lines.
✅ Project coverage is 89.85%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/pyramid.py 100.00% ⚠️ 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.84%    89.85%    +0.01%
==========================================
  Files          192       192         —
  Lines        23740     23745        +5
  Branches      8194      8198        +4
==========================================
+ Hits         21329     21335        +6
- Misses        2411      2410        -1
- Partials      1343      1343         —

Generated by Codecov Action

@alexander-alderman-webb alexander-alderman-webb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ericapisani ericapisani merged commit 883e585 into master Jun 19, 2026
144 checks passed
@ericapisani ericapisani deleted the py-2402-set-user-id-on-segment branch June 19, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Pyramid] Set user id on segment span

2 participants