Skip to content

feat: Access transaction in current scope #734

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

Merged
merged 1 commit into from
Jun 26, 2020
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
27 changes: 25 additions & 2 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,33 @@ def fingerprint(self, value):
"""When set this overrides the default fingerprint."""
self._fingerprint = value

@_attr_setter
@property
def transaction(self):
# type: () -> Any
# would be type: () -> Optional[Span], see https://github.com/python/mypy/issues/3004
# XXX: update return type to Optional[Transaction]
"""Return the transaction (root span) in the scope."""
if self._span is None or self._span._span_recorder is None:
return None
try:
return self._span._span_recorder.spans[0]
except (AttributeError, IndexError):
return None

@transaction.setter
def transaction(self, value):
# type: (Optional[str]) -> None
# type: (Any) -> None
# would be type: (Optional[str]) -> None, see https://github.com/python/mypy/issues/3004
"""When set this forces a specific transaction name to be set."""
# XXX: the docstring above is misleading. The implementation of
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to fix that behavior in this PR, I don't think anybody would notice even.

# apply_to_event prefers an existing value of event.transaction over
# anything set in the scope.
# XXX: note that with the introduction of the Scope.transaction getter,
# there is a semantic and type mismatch between getter and setter. The
# getter returns a transaction, the setter sets a transaction name.
# Without breaking version compatibility, we could make the setter set a
# transaction name or transaction (self._span) depending on the type of
# the value argument.
self._transaction = value
span = self._span
if span:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from sentry_sdk import Hub, capture_message
from sentry_sdk import Hub, capture_message, start_span
from sentry_sdk.tracing import Span


Expand Down Expand Up @@ -180,3 +180,17 @@ def before_send(event, hint):
pass

assert len(events) == 1


def test_get_transaction_from_scope(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()

with start_span(transaction="/"):
with start_span(op="child-span"):
with start_span(op="child-child-span"):
scope = Hub.current.scope
assert scope.span.op == "child-child-span"
assert scope.transaction.transaction == "/"

assert len(events) == 1