Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added top level api to get current span and transaction #1954

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions sentry_sdk/api.py
Expand Up @@ -53,6 +53,9 @@ def overload(x):
"set_user",
"set_level",
"set_measurement",
"get_current_span",
"get_current_transaction",
"get_current_span_or_transaction",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also have to add these to __init__.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

]


Expand Down Expand Up @@ -228,3 +231,44 @@ def set_measurement(name, value, unit=""):
transaction = Hub.current.scope.transaction
if transaction is not None:
transaction.set_measurement(name, value, unit)


def get_current_span(hub=None):
# type: (Optional[Hub]) -> Optional[Span]
"""
Returns the currently active span if there is one running, otherwise `None`
"""
if hub is None:
hub = Hub.current

current_span = hub.scope.span
return current_span


def get_current_transaction(hub=None):
# type: (Optional[Hub]) -> Optional[Transaction]
"""
Returns the currently active transaction if there is one running, otherwise `None`
"""
if hub is None:
hub = Hub.current

transaction = hub.scope.transaction
return transaction


def get_current_span_or_transaction(hub=None):
# type: (Optional[Hub]) -> Optional[Union[Span, Transaction]]
"""
Returns the currently active span if there is one running,
otherwise the currently active transaction if there is one running,
otherwise `None`
"""
if hub is None:
hub = Hub.current

current_span = get_current_span(hub)
if current_span is not None:
return current_span

return get_current_transaction(hub)
84 changes: 84 additions & 0 deletions tests/test_api.py
@@ -0,0 +1,84 @@
import mock

from sentry_sdk import (
configure_scope,
get_current_span_or_transaction,
get_current_span,
get_current_transaction,
start_transaction,
)


def test_get_current_span():
fake_hub = mock.MagicMock()
fake_hub.scope = mock.MagicMock()

fake_hub.scope.span = mock.MagicMock()
assert get_current_span(fake_hub) == fake_hub.scope.span

fake_hub.scope.span = None
assert get_current_span(fake_hub) is None


def test_get_current_span_default_hub(sentry_init):
sentry_init()

assert get_current_span() is None

with configure_scope() as scope:
fake_span = mock.MagicMock()
scope.span = fake_span

assert get_current_span() == fake_span


def test_get_current_transaction():
fake_hub = mock.MagicMock()
fake_hub.scope = mock.MagicMock()

fake_hub.scope.transaction = mock.MagicMock()
assert get_current_transaction(fake_hub) == fake_hub.scope.transaction

fake_hub.scope.transaction = None
assert get_current_transaction(fake_hub) is None


def test_get_current_transaction_default_hub(sentry_init):
sentry_init()

assert get_current_transaction() is None

with start_transaction() as new_transaction:
assert get_current_transaction() == new_transaction


def test_get_current_span_or_transaction():
fake_hub = mock.MagicMock()
fake_hub.scope = mock.MagicMock()

fake_hub.scope.span = mock.MagicMock()
fake_hub.scope.transaction = None
assert get_current_span_or_transaction(fake_hub) == fake_hub.scope.span

fake_hub.scope.span = None
fake_hub.scope.transaction = mock.MagicMock()
assert get_current_span_or_transaction(fake_hub) == fake_hub.scope.transaction

fake_hub.scope.span = None
fake_hub.scope.transaction = None
assert get_current_span_or_transaction(fake_hub) is None


def test_get_current_span_or_transaction_default_hub(sentry_init):
sentry_init()

assert get_current_span_or_transaction() is None

with start_transaction() as new_transaction:
assert get_current_span_or_transaction() == new_transaction

with configure_scope() as scope:
fake_span = mock.MagicMock()
scope.span = fake_span

assert get_current_span_or_transaction() == fake_span