Skip to content
Merged
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
2 changes: 1 addition & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def capture_event(self, event, scope=None, hint=None):
event = self._prepare_event(event, scope, hint)
if event is not None:
self._transport.capture_event(event)
return True
return rv

def drain_events(self, timeout=None):
if timeout is None:
Expand Down
10 changes: 9 additions & 1 deletion sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, client_or_hub=None, scope=None):
if scope is None:
scope = Scope()
self._stack = [(client, scope)]
self._last_event_id = None

def __enter__(self):
return _HubManager(self)
Expand All @@ -96,6 +97,10 @@ def client(self):
"""Returns the current client on the hub."""
return self._stack[-1][0]

def last_event_id(self):
"""Returns the last event ID."""
return self._last_event_id

def bind_client(self, new):
"""Binds a new client to the hub."""
top = self._stack[-1]
Expand All @@ -105,7 +110,10 @@ def capture_event(self, event, hint=None):
"""Captures an event."""
client, scope = self._stack[-1]
if client is not None:
return client.capture_event(event, scope, hint)
rv = client.capture_event(event, scope, hint)
if rv is not None:
self._last_event_id = rv
return rv

def capture_message(self, message, level=None):
"""Captures a message."""
Expand Down
7 changes: 7 additions & 0 deletions sentry_sdk/minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def get_current_hub():
return Hub.current


@public
def last_event_id():
hub = Hub.current
if hub is not None:
return hub.last_event_id()


try:
from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope
Expand Down
19 changes: 18 additions & 1 deletion tests/test_basics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sentry_sdk import configure_scope, capture_exception
from sentry_sdk import configure_scope, capture_exception, last_event_id, Hub


def test_processors(sentry_init, capture_events):
Expand All @@ -21,3 +21,20 @@ def error_processor(event, exc_info):
event, = events

assert event["exception"]["values"][0]["value"] == "aha! whatever"


def test_event_id(sentry_init, capture_events):
sentry_init()
events = capture_events()

try:
raise ValueError("aha!")
except Exception:
event_id = capture_exception()
int(event_id, 16)
assert len(event_id) == 32

event, = events
assert event["event_id"] == event_id
assert last_event_id() == event_id
assert Hub.current.last_event_id() == event_id