Skip to content
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
34 changes: 34 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,16 @@ def remove_tag(self, key):
"""
self._tags.pop(key, None)

def get_tag(self, key):
# type: (str) -> (Any | None)
"""
Retrieves the value of a specific tag.

:param key: Key of the tag to retrieve.
:return: Value of the tag, or None if the tag does not exist.
"""
return self._tags.get(key)

def set_context(
self,
key, # type: str
Expand All @@ -866,6 +876,18 @@ def remove_context(
"""Removes a context."""
self._contexts.pop(key, None)

def get_context(
self, key # type: str
):
# type: (...) -> (Any | None)
"""
Retrieves the value of a specific context.

:param key: Key of the context to retrieve.
:return: Value of the context key, or None if the context key does not exist.
"""
return self._contexts.get(key)

def set_extra(
self,
key, # type: str
Expand All @@ -882,6 +904,18 @@ def remove_extra(
"""Removes a specific extra key."""
self._extras.pop(key, None)

def get_extra(
self, key # type: str
):
# type: (...) -> (Any | None)
"""
Retrieves the value of a specific extra.

:param key: Key of the extra to retrieve.
:return: Value of the extra key, or None if the extra key does not exist.
"""
return self._extras.get(key)

def clear_breadcrumbs(self):
# type: () -> None
"""Clears breadcrumb buffer."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,25 @@ def test_last_event_id_transaction(sentry_init):
pass

assert Scope.last_event_id() is None, "Transaction should not set last_event_id"


def test_get_tag():
scope = Scope()
scope.set_tags({"tag1": "value1", "tag2": "value2"})
assert scope.get_tag("tag1") == "value1"
assert scope.get_tag("tag2") == "value2"
assert scope.get_tag("missing") is None


def test_get_context():
scope = Scope()
scope.set_context("device", {"a": "b"})
assert scope.get_context("device") == {"a": "b"}
assert scope.get_context("missing") is None


def test_get_extra():
scope = Scope()
scope.set_extra("foo", "bar")
assert scope.get_extra("foo") == "bar"
assert scope.get_extra("missing") is None