Skip to content

Commit

Permalink
feat(scope): Add last_event_id to Scope
Browse files Browse the repository at this point in the history
Fixes #3049
  • Loading branch information
szokeasaurusrex committed May 15, 2024
1 parent 94a6c2a commit 77d4a45
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
21 changes: 20 additions & 1 deletion sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class Scope(object):
"_propagation_context",
"client",
"_type",
"_last_event_id",
)

def __init__(self, ty=None, client=None):
Expand All @@ -207,6 +208,9 @@ def __init__(self, ty=None, client=None):
incoming_trace_information = self._load_trace_data_from_env()
self.generate_propagation_context(incoming_data=incoming_trace_information)

# self._last_event_id is only applicable to isolation scopes
self._last_event_id = None # type: Optional[str]

def __copy__(self):
# type: () -> Scope
"""
Expand Down Expand Up @@ -308,6 +312,16 @@ def get_global_scope(cls):

return _global_scope

@classmethod
def last_event_id(cls):
# type: () -> Optional[str]
"""
.. versionadded:: 2.X.X
Returns the last event id of the isolation scope.
"""
return cls.get_isolation_scope()._last_event_id

def _merge_scopes(self, additional_scope=None, additional_scope_kwargs=None):
# type: (Optional[Scope], Optional[Dict[str, Any]]) -> Scope
"""
Expand Down Expand Up @@ -1089,7 +1103,12 @@ def capture_event(self, event, hint=None, scope=None, **scope_kwargs):
"""
scope = self._merge_scopes(scope, scope_kwargs)

return Scope.get_client().capture_event(event=event, hint=hint, scope=scope)
event_id = Scope.get_client().capture_event(event=event, hint=hint, scope=scope)

if event_id is not None and event.get("type") != "transaction":
self.get_isolation_scope()._last_event_id = event_id

return event_id

def capture_message(self, message, level=None, scope=None, **scope_kwargs):
# type: (str, Optional[LogLevelStr], Optional[Scope], Any) -> Optional[str]
Expand Down
21 changes: 21 additions & 0 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,24 @@ def test_set_tags():
"tag2": "updated",
"tag3": "new",
}, "Updating tags with empty dict changed tags"


def test_last_event_id(sentry_init):
sentry_init(enable_tracing=True)

assert Scope.last_event_id() is None

sentry_sdk.capture_exception(Exception("test"))

assert Scope.last_event_id() is not None


def test_last_event_id_transaction(sentry_init):
sentry_init(enable_tracing=True)

assert Scope.last_event_id() is None

with sentry_sdk.start_transaction(name="test"):
pass

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

0 comments on commit 77d4a45

Please sign in to comment.