feat(django): Add user attributes in span streaming#6541
feat(django): Add user attributes in span streaming#6541alexander-alderman-webb wants to merge 8 commits into
Conversation
Codecov Results 📊✅ 88749 passed | ⏭️ 6013 skipped | Total: 94762 | Pass Rate: 93.65% | Execution Time: 299m 45s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 82.35%. Project has 2374 uncovered lines. Files with missing lines (1)
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 +1Generated by Codecov Action |
sentrivana
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Yes thanks for calling this out!
Updated in 3db9491
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ 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.
| except Exception: | ||
| pass | ||
| if user_id is not None: | ||
| scope.set_attribute(SPANDATA.USER_ID, user_id) |
There was a problem hiding this comment.
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.

Description
Adapt logic for extracting user information from
_set_user_info()to run afterBaseHandler.get_response.Do not set user attributes if the user is an instance of
SimpleLazyObjectand 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
uv run ruff.feat:,fix:,ref:,meta:)