Skip to content

feat(django): Add user attributes in span streaming#6541

Open
alexander-alderman-webb wants to merge 8 commits into
masterfrom
webb/django/user
Open

feat(django): Add user attributes in span streaming#6541
alexander-alderman-webb wants to merge 8 commits into
masterfrom
webb/django/user

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Description

Adapt logic for extracting user information from _set_user_info() to run after BaseHandler.get_response.

Do not set user attributes if the user is an instance of SimpleLazyObject and the underlying user object has not been cached yet. Bailing early ensures that we avoid the exception reported in #5274.

Issues

Contributes to #6201

Reminders

Comment thread sentry_sdk/consts.py
Comment thread sentry_sdk/consts.py
Comment thread sentry_sdk/consts.py
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

88749 passed | ⏭️ 6013 skipped | Total: 94762 | Pass Rate: 93.65% | Execution Time: 299m 45s

📊 Comparison with Base Branch

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

All tests are passing successfully.

✅ Patch coverage is 82.35%. Project has 2374 uncovered lines.
❌ Project coverage is 89.86%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/django/init.py 78.57% ⚠️ 6 Missing and 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.88%    89.86%    -0.02%
==========================================
  Files          192       192         —
  Lines        23371     23403       +32
  Branches      8034      8044       +10
==========================================
+ Hits         21004     21029       +25
- Misses        2367      2374        +7
- Partials      1327      1328        +1

Generated by Codecov Action

Comment thread tests/integrations/django/test_basic.py
@alexander-alderman-webb alexander-alderman-webb marked this pull request as ready for review June 9, 2026 13:10
@alexander-alderman-webb alexander-alderman-webb requested a review from a team as a code owner June 9, 2026 13:10
Comment thread sentry_sdk/integrations/django/__init__.py Outdated
Comment thread sentry_sdk/integrations/django/__init__.py Outdated
Comment thread sentry_sdk/integrations/django/__init__.py
Comment thread sentry_sdk/integrations/django/__init__.py
Comment thread sentry_sdk/integrations/django/__init__.py Outdated

@sentrivana sentrivana 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.

See comment, rest lgtm! Ty for adding a test, too

except Exception:
pass
if user_id is not None:
segment_span.set_attribute(SPANDATA.USER_ID, user_id)

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.

All the user attrs should be set on all spans, not just the segment, see https://develop.sentry.dev/sdk/telemetry/spans/span-protocol/#common-attribute-keys

So we don't need to fetch the current span and its segment here, we can just set_attribute on the current scope so that all spans get it automatically

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes thanks for calling this out!
Updated in 3db9491

@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 2 potential issues.

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 3db9491. Configure here.

Comment thread sentry_sdk/integrations/django/__init__.py Outdated
Comment thread sentry_sdk/integrations/django/__init__.py Outdated
except Exception:
pass
if user_id is not None:
scope.set_attribute(SPANDATA.USER_ID, user_id)

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.

Sorry, one more comment here that I only realized now that I've read the scope code that sets user attrs: instead of setting the attrs directly, we should use the scope.set_user API since that sets it on the scope and then other parts of the SDK take it from there and transform it into what they need (like span attrs, here). Just note that the field names set_user expects are different from the span attribute names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me. Addressed in ea4a3cf

@sentrivana sentrivana 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.

🙏🏻

Comment thread sentry_sdk/integrations/django/__init__.py
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.

2 participants