Skip to content

Create Scope.set_extras #530

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -205,6 +212,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
):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@ 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"


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"