From f210cf707f63fae37109072ee27f3ca26eb5dea2 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 15 Feb 2024 17:50:08 +0100 Subject: [PATCH 1/4] fix(user): Make set_user update existing data --- MIGRATION_GUIDE.md | 1 + sentry_sdk/scope.py | 7 ++++++- tests/test_scope.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index 7936fa52bf..0397d80279 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -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 erase 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`. diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index f1ce6890e5..c5d6019111 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -518,7 +518,12 @@ def user(self, value): 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 + if self._session is not None: self._session.update(user=value) diff --git a/tests/test_scope.py b/tests/test_scope.py index 88022e3920..0b5b2379d1 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -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({"email": "lucy@dogs.com"}) + assert scope._user == {"id": 23, "email": "lucy@dogs.com"} + + scope.set_user({}) + assert scope._user is None + + capture_exception(NameError(), scope=scope) + + (event,) = events + assert not event["user"] From 72cfa4fdfeef28f5df84aa9d5af46e9c478a244c Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 15 Feb 2024 17:55:54 +0100 Subject: [PATCH 2/4] fix test --- tests/test_scope.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_scope.py b/tests/test_scope.py index 0b5b2379d1..bb8ac3a2c6 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -182,8 +182,8 @@ def test_user_reset(sentry_init, capture_events): scope = Scope() assert scope._user is None - scope.set_user({"email": "lucy@dogs.com"}) - assert scope._user == {"id": 23, "email": "lucy@dogs.com"} + 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 From 7b83ded32fd548610985f33687310c3b46289100 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 15 Feb 2024 17:58:02 +0100 Subject: [PATCH 3/4] wording --- MIGRATION_GUIDE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index 0397d80279..04a9a3d4e5 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -8,7 +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 erase user data, call the function with an empty dictionary: `sentry_sdk.set_user({})` +- 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`. From 00e905039a2ee82c30573e79e6b4df1a7225d84e Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 15 Feb 2024 18:07:38 +0100 Subject: [PATCH 4/4] keyerror --- tests/test_scope.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_scope.py b/tests/test_scope.py index bb8ac3a2c6..dfa5fc2fa0 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -191,4 +191,4 @@ def test_user_reset(sentry_init, capture_events): capture_exception(NameError(), scope=scope) (event,) = events - assert not event["user"] + assert "user" not in event