diff --git a/sentry_sdk/__init__.py b/sentry_sdk/__init__.py index 1b646992ff..94d97a87d8 100644 --- a/sentry_sdk/__init__.py +++ b/sentry_sdk/__init__.py @@ -33,6 +33,7 @@ "get_traceparent", "is_initialized", "isolation_scope", + "last_event_id", "new_scope", "push_scope", "set_context", diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 37c81afcc5..30867e95bf 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -59,6 +59,7 @@ def overload(x): "get_traceparent", "is_initialized", "isolation_scope", + "last_event_id", "new_scope", "push_scope", "set_context", @@ -332,6 +333,12 @@ def start_transaction( ) +@scopemethod +def last_event_id(): + # type: () -> Optional[str] + return Scope.last_event_id() + + def set_measurement(name, value, unit=""): # type: (str, float, MeasurementUnit) -> None transaction = Scope.get_current_scope().transaction diff --git a/tests/test_basics.py b/tests/test_basics.py index bf42634710..aeb8488a0f 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -15,6 +15,7 @@ capture_exception, capture_message, start_transaction, + last_event_id, add_breadcrumb, Hub, Scope, @@ -778,3 +779,24 @@ def test_classmethod_tracing(sentry_init): with patch_start_tracing_child() as fake_start_child: assert instance_or_class.class_(1) == (TracingTestClass, 1) assert fake_start_child.call_count == 1 + + +def test_last_event_id(sentry_init): + sentry_init(enable_tracing=True) + + assert last_event_id() is None + + capture_exception(Exception("test")) + + assert last_event_id() is not None + + +def test_last_event_id_transaction(sentry_init): + sentry_init(enable_tracing=True) + + assert last_event_id() is None + + with start_transaction(name="test"): + pass + + assert last_event_id() is None, "Transaction should not set last_event_id"