@@ -185,6 +185,7 @@ class Scope(object):
185185 "_propagation_context" ,
186186 "client" ,
187187 "_type" ,
188+ "_last_event_id" ,
188189 )
189190
190191 def __init__ (self , ty = None , client = None ):
@@ -207,6 +208,9 @@ def __init__(self, ty=None, client=None):
207208 incoming_trace_information = self ._load_trace_data_from_env ()
208209 self .generate_propagation_context (incoming_data = incoming_trace_information )
209210
211+ # self._last_event_id is only applicable to isolation scopes
212+ self ._last_event_id = None # type: Optional[str]
213+
210214 def __copy__ (self ):
211215 # type: () -> Scope
212216 """
@@ -308,6 +312,23 @@ def get_global_scope(cls):
308312
309313 return _global_scope
310314
315+ @classmethod
316+ def last_event_id (cls ):
317+ # type: () -> Optional[str]
318+ """
319+ .. versionadded:: 2.2.0
320+
321+ Returns event ID of the event most recently captured by the isolation scope, or None if no event
322+ has been captured. We do not consider events that are dropped, e.g. by a before_send hook.
323+ Transactions also are not considered events in this context.
324+
325+ The event corresponding to the returned event ID is NOT guaranteed to actually be sent to Sentry;
326+ whether the event is sent depends on the transport. The event could be sent later or not at all.
327+ Even a sent event could fail to arrive in Sentry due to network issues, exhausted quotas, or
328+ various other reasons.
329+ """
330+ return cls .get_isolation_scope ()._last_event_id
331+
311332 def _merge_scopes (self , additional_scope = None , additional_scope_kwargs = None ):
312333 # type: (Optional[Scope], Optional[Dict[str, Any]]) -> Scope
313334 """
@@ -1089,7 +1110,12 @@ def capture_event(self, event, hint=None, scope=None, **scope_kwargs):
10891110 """
10901111 scope = self ._merge_scopes (scope , scope_kwargs )
10911112
1092- return Scope .get_client ().capture_event (event = event , hint = hint , scope = scope )
1113+ event_id = Scope .get_client ().capture_event (event = event , hint = hint , scope = scope )
1114+
1115+ if event_id is not None and event .get ("type" ) != "transaction" :
1116+ self .get_isolation_scope ()._last_event_id = event_id
1117+
1118+ return event_id
10931119
10941120 def capture_message (self , message , level = None , scope = None , ** scope_kwargs ):
10951121 # type: (str, Optional[LogLevelStr], Optional[Scope], Any) -> Optional[str]
0 commit comments