Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scope): set_user should not overwrite existing user data #2742

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions MIGRATION_GUIDE.md
Expand Up @@ -8,6 +8,7 @@ Looking to upgrade from Sentry SDK 1.x to 2.x? Here's a comprehensive list of wh

## Changed

- Subsequent calls to `sentry_sdk.set_user` will now update existing user data instead of overwriting it. To reset user data, call the function with an empty dictionary: `sentry_sdk.set_user({})`
- The `BackgroundWorker` thread used to process events was renamed from `raven-sentry.BackgroundWorker` to `sentry-sdk.BackgroundWorker`.
- The `reraise` function was moved from `sentry_sdk._compat` to `sentry_sdk.utils`.
- Moved the contents of `tracing_utils_py3.py` to `tracing_utils.py`. The `start_child_span_decorator` is now in `sentry_sdk.tracing_utils`.
Expand Down
7 changes: 6 additions & 1 deletion sentry_sdk/scope.py
Expand Up @@ -518,7 +518,12 @@
def set_user(self, value):
# type: (Optional[Dict[str, Any]]) -> None
"""Sets a user for the scope."""
self._user = value
if value:
self._user = self._user or {}
self._user.update(value)
else:
self._user = None

Check warning on line 525 in sentry_sdk/scope.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/scope.py#L525

Added line #L525 was not covered by tests

if self._session is not None:
self._session.update(user=value)

Expand Down
38 changes: 38 additions & 0 deletions tests/test_scope.py
Expand Up @@ -154,3 +154,41 @@ def test_load_trace_data_from_env(env, excepted_value):
s = Scope()
incoming_trace_data = s._load_trace_data_from_env()
assert incoming_trace_data == excepted_value


def test_user_update(sentry_init, capture_events):
sentry_init()
events = capture_events()

scope = Scope()
assert scope._user is None

scope.set_user({"id": 23})
assert scope._user == {"id": 23}

scope.set_user({"email": "lucy@dogs.com"})
assert scope._user == {"id": 23, "email": "lucy@dogs.com"}

capture_exception(NameError(), scope=scope)

(event,) = events
assert event["user"] == {"id": 23, "email": "lucy@dogs.com"}


def test_user_reset(sentry_init, capture_events):
sentry_init()
events = capture_events()

scope = Scope()
assert scope._user is None

scope.set_user({"id": 23, "email": "dottie@dogs.com"})
assert scope._user == {"id": 23, "email": "dottie@dogs.com"}

scope.set_user({})
assert scope._user is None

capture_exception(NameError(), scope=scope)

(event,) = events
assert "user" not in event