Skip to content
Closed
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: 2 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,8 @@ def span(self, span: "Optional[Span]") -> None:
self._transaction = transaction.name
if transaction.source:
self._transaction_info["source"] = transaction.source
elif span is None:
self._transaction = None

@property
def streamed_span(self) -> "Optional[StreamedSpan]":
Expand Down
19 changes: 19 additions & 0 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,25 @@ def test_conversation_id_copy():
assert scope2.get_conversation_id() == "modified-conv"


def test_transaction_name_cleared_when_span_unset():
"""Scope._transaction follows span lifecycle:
set by Transaction, preserved during child Span, cleared on None."""
from sentry_sdk.tracing import Transaction

scope = Scope()

txn = Transaction(name="my-txn")
scope.span = txn
assert scope._transaction == "my-txn"

child = txn.start_child(op="child")
scope.span = child
assert scope._transaction == "my-txn"

scope.span = None
assert scope._transaction is None


def test_conversation_id_clear():
"""Test that conversation_id is cleared when scope.clear() is called."""
scope = Scope()
Expand Down