Skip to content

Commit 5fbc0bf

Browse files
committed
PYTHON-6035 Trim the transaction span comments and docstrings
Cut each to the invariant a reader needs, dropping spec references and restatements of the code.
1 parent 5d1a661 commit 5fbc0bf

5 files changed

Lines changed: 74 additions & 157 deletions

File tree

pymongo/_otel.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,8 @@ def end_operation_span_failure(handle: Optional[_OperationSpanHandle], exc: Base
537537
def start_transaction_span(tracing_options: Optional[TracingOptions]) -> Optional[Span]:
538538
"""Start (but do not make current) the ``"transaction"`` pseudo-span, or None.
539539
540-
Stored on ``session._transaction.span`` and passed as the explicit
541-
``parent_span`` for operation spans under this transaction, never pushed as
542-
ambient context. Per the spec it carries exactly one attribute.
540+
Passed as the explicit ``parent_span`` for operation spans in this
541+
transaction, never pushed as ambient context.
543542
"""
544543
if not _is_tracing_enabled(tracing_options):
545544
return None

pymongo/asynchronous/client_session.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,7 @@ def __init__(
565565
self._implicit = implicit
566566
self._transaction = _Transaction(None, client)
567567
# The one "transaction" span shared across every retry of a single
568-
# with_transaction() call, or None outside of it, where
569-
# start/commit/abort_transaction each manage their own span.
568+
# with_transaction() call; the direct API manages its own span instead.
570569
self._with_transaction_span: Optional[Any] = None
571570
# Is this session attached to a cursor?
572571
self._attached_to_cursor = False
@@ -776,17 +775,14 @@ async def callback(session, custom_arg, custom_kwarg=None):
776775
https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback
777776
"""
778777
if self._with_transaction_span is not None:
779-
# Raise before any span bookkeeping, so a nested call cannot
780-
# clobber and leak the outer call's span.
778+
# Before any span bookkeeping, so a nested call cannot leak the outer span.
781779
raise InvalidOperation(
782780
"Cannot call with_transaction() while a previous with_transaction() "
783781
"call on this session has not returned; sessions do not support "
784782
"nested or concurrent with_transaction() calls"
785783
)
786-
# One span for the whole call: start_transaction reuses it and
787-
# commit/abort leave it open, so a retried with_transaction() yields a
788-
# single span. Skipped when a direct-API transaction is already active,
789-
# since start_transaction() raises below and the span would be empty.
784+
# Skipped when a direct-API transaction is already active, since
785+
# start_transaction() raises below and the span would be empty.
790786
tracing_options = self._client.options.tracing
791787
if _otel._is_tracing_enabled(tracing_options) and not self.in_transaction:
792788
self._with_transaction_span = _otel.start_transaction_span(tracing_options)
@@ -797,8 +793,7 @@ async def callback(session, custom_arg, custom_kwarg=None):
797793
finally:
798794
if self._with_transaction_span is not None:
799795
_otel.end_transaction_span(self._with_transaction_span)
800-
# Only clear the span this call owns; a concurrent direct-API
801-
# transaction's span belongs to that transaction.
796+
# A direct-API transaction's span belongs to that transaction.
802797
if self._transaction.span is self._with_transaction_span:
803798
self._transaction.span = None
804799
self._with_transaction_span = None
@@ -908,8 +903,7 @@ async def start_transaction(
908903
await self._transaction.reset()
909904
self._transaction.state = _TxnState.STARTING
910905
if self._with_transaction_span is not None:
911-
# Reuse with_transaction's shared span so a retried call still
912-
# produces exactly one "transaction" span.
906+
# Reuse it so a retried with_transaction() still produces one span.
913907
self._transaction.span = self._with_transaction_span
914908
elif _otel._is_tracing_enabled(self._transaction.client.options.tracing):
915909
self._transaction.span = _otel.start_transaction_span(
@@ -921,9 +915,8 @@ async def start_transaction(
921915
def _end_own_transaction_span(self) -> None:
922916
"""End and clear the transaction span, unless with_transaction() owns it.
923917
924-
A no-op while ``_with_transaction_span`` is set: that span is shared
925-
across every retry, so ending it here would kill it on the first failed
926-
attempt.
918+
That span is shared across retries, so ending it here would kill it on
919+
the first failed attempt.
927920
"""
928921
if self._transaction.span is not None and self._with_transaction_span is None:
929922
_otel.end_transaction_span(self._transaction.span)
@@ -949,9 +942,8 @@ async def commit_transaction(self) -> None:
949942
# We're explicitly retrying the commit, move the state back to
950943
# "in progress" so that in_transaction returns true.
951944
self._transaction.state = _TxnState.IN_PROGRESS
952-
# A direct-API retry needs a fresh span: the prior attempt's
953-
# finally block already ended and cleared it. with_transaction
954-
# pins its shared span instead, see _end_own_transaction_span.
945+
# The prior attempt's finally block already ended and cleared the
946+
# span, so an explicit commit retry needs a fresh one.
955947
if self._transaction.span is None and _otel._is_tracing_enabled(
956948
self._transaction.client.options.tracing
957949
):

pymongo/synchronous/client_session.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ def __init__(
564564
self._implicit = implicit
565565
self._transaction = _Transaction(None, client)
566566
# The one "transaction" span shared across every retry of a single
567-
# with_transaction() call, or None outside of it, where
568-
# start/commit/abort_transaction each manage their own span.
567+
# with_transaction() call; the direct API manages its own span instead.
569568
self._with_transaction_span: Optional[Any] = None
570569
# Is this session attached to a cursor?
571570
self._attached_to_cursor = False
@@ -775,17 +774,14 @@ def callback(session, custom_arg, custom_kwarg=None):
775774
https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback
776775
"""
777776
if self._with_transaction_span is not None:
778-
# Raise before any span bookkeeping, so a nested call cannot
779-
# clobber and leak the outer call's span.
777+
# Before any span bookkeeping, so a nested call cannot leak the outer span.
780778
raise InvalidOperation(
781779
"Cannot call with_transaction() while a previous with_transaction() "
782780
"call on this session has not returned; sessions do not support "
783781
"nested or concurrent with_transaction() calls"
784782
)
785-
# One span for the whole call: start_transaction reuses it and
786-
# commit/abort leave it open, so a retried with_transaction() yields a
787-
# single span. Skipped when a direct-API transaction is already active,
788-
# since start_transaction() raises below and the span would be empty.
783+
# Skipped when a direct-API transaction is already active, since
784+
# start_transaction() raises below and the span would be empty.
789785
tracing_options = self._client.options.tracing
790786
if _otel._is_tracing_enabled(tracing_options) and not self.in_transaction:
791787
self._with_transaction_span = _otel.start_transaction_span(tracing_options)
@@ -796,8 +792,7 @@ def callback(session, custom_arg, custom_kwarg=None):
796792
finally:
797793
if self._with_transaction_span is not None:
798794
_otel.end_transaction_span(self._with_transaction_span)
799-
# Only clear the span this call owns; a concurrent direct-API
800-
# transaction's span belongs to that transaction.
795+
# A direct-API transaction's span belongs to that transaction.
801796
if self._transaction.span is self._with_transaction_span:
802797
self._transaction.span = None
803798
self._with_transaction_span = None
@@ -905,8 +900,7 @@ def start_transaction(
905900
self._transaction.reset()
906901
self._transaction.state = _TxnState.STARTING
907902
if self._with_transaction_span is not None:
908-
# Reuse with_transaction's shared span so a retried call still
909-
# produces exactly one "transaction" span.
903+
# Reuse it so a retried with_transaction() still produces one span.
910904
self._transaction.span = self._with_transaction_span
911905
elif _otel._is_tracing_enabled(self._transaction.client.options.tracing):
912906
self._transaction.span = _otel.start_transaction_span(
@@ -918,9 +912,8 @@ def start_transaction(
918912
def _end_own_transaction_span(self) -> None:
919913
"""End and clear the transaction span, unless with_transaction() owns it.
920914
921-
A no-op while ``_with_transaction_span`` is set: that span is shared
922-
across every retry, so ending it here would kill it on the first failed
923-
attempt.
915+
That span is shared across retries, so ending it here would kill it on
916+
the first failed attempt.
924917
"""
925918
if self._transaction.span is not None and self._with_transaction_span is None:
926919
_otel.end_transaction_span(self._transaction.span)
@@ -946,9 +939,8 @@ def commit_transaction(self) -> None:
946939
# We're explicitly retrying the commit, move the state back to
947940
# "in progress" so that in_transaction returns true.
948941
self._transaction.state = _TxnState.IN_PROGRESS
949-
# A direct-API retry needs a fresh span: the prior attempt's
950-
# finally block already ended and cleared it. with_transaction
951-
# pins its shared span instead, see _end_own_transaction_span.
942+
# The prior attempt's finally block already ended and cleared the
943+
# span, so an explicit commit retry needs a fresh one.
952944
if self._transaction.span is None and _otel._is_tracing_enabled(
953945
self._transaction.client.options.tracing
954946
):

0 commit comments

Comments
 (0)