From a608cba2da351b6a9156c55a9d53d9a5ec6e1a11 Mon Sep 17 00:00:00 2001 From: Marcelo Theodoro Date: Mon, 14 Oct 2019 19:15:24 -0300 Subject: [PATCH 1/3] Create Scope.set_extras --- sentry_sdk/scope.py | 8 ++++++++ tests/test_scope.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 1ea2f11b17..2f2f0d8048 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -205,6 +205,14 @@ def set_extra( """Sets an extra key to a specific value.""" self._extras[key] = value + def set_extras( + self, extras # type: Dict[str, Any] + ): + # type: (...) -> None + """Adds the keys and values of a dict in the extras.""" + for key, value in extras.items(): + self._extras[key] = value + def remove_extra( self, key # type: str ): diff --git a/tests/test_scope.py b/tests/test_scope.py index b9c3335116..26cf4d158f 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -15,3 +15,15 @@ def test_copying(): assert "bam" not in s2._tags assert s1._fingerprint is s2._fingerprint + + +def test_set_extras(): + scope = Scope() + + extras = {"foo": "bar", "bar": "foo"} + scope.set_extras(extras) + + assert "foo" in scope._extras + assert "bar" in scope._extras + assert scope._extras["foo"] == "bar" + assert scope._extras["bar"] == "foo" From 671cc06f32abc5586ee38f70d0711e1c31dcbeee Mon Sep 17 00:00:00 2001 From: Marcelo Theodoro Date: Fri, 25 Oct 2019 10:26:59 -0300 Subject: [PATCH 2/3] Create Scope.set_tags --- sentry_sdk/scope.py | 7 +++++++ tests/test_scope.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 2f2f0d8048..945cc3ee18 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -173,6 +173,13 @@ def set_tag( """Sets a tag for a key to a specific value.""" self._tags[key] = value + def set_tags( + self, tags # type: Dict[str, Any] + ): + # type: (...) -> None + for key, value in tags.items(): + self._tags[key] = value + def remove_tag( self, key # type: str ): diff --git a/tests/test_scope.py b/tests/test_scope.py index 26cf4d158f..f3b7507a30 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -27,3 +27,15 @@ def test_set_extras(): assert "bar" in scope._extras assert scope._extras["foo"] == "bar" assert scope._extras["bar"] == "foo" + + +def test_set_tags(): + scope = Scope() + + tags = {"foo": "bar", "bar": "foo"} + scope.set_tags(tags) + + assert "foo" in scope._tags + assert "bar" in scope._tags + assert scope._tags["foo"] == "bar" + assert scope._tags["bar"] == "foo" From 2a0d2cb58bf47d88ff23eadd432412c4dbc31ece Mon Sep 17 00:00:00 2001 From: Marcelo Theodoro Date: Fri, 25 Oct 2019 10:51:33 -0300 Subject: [PATCH 3/3] Add set_extras and set_tags to global scope --- sentry_sdk/api.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 6ecb33b1c8..86fae66cbc 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -38,8 +38,10 @@ def overload(x): "last_event_id", "start_span", "set_tag", + "set_tags", "set_context", "set_extra", + "set_extras", "set_user", "set_level", ] @@ -186,6 +188,14 @@ def set_tag(key, value): hub.scope.set_tag(key, value) +@scopemethod # noqa +def set_tags(tags): + # type: (Dict[str, Any]) -> None + hub = Hub.current + if hub is not None: + hub.scope.set_tags(tags) + + @scopemethod # noqa def set_context(key, value): # type: (str, Any) -> None @@ -202,6 +212,14 @@ def set_extra(key, value): hub.scope.set_extra(key, value) +@scopemethod # noqa +def set_extras(extras): + # type: (Dict[str, Any]) -> None + hub = Hub.current + if hub is not None: + hub.scope.set_extras(extras) + + @scopemethod # noqa def set_user(value): # type: (Dict[str, Any]) -> None