Skip to content

PYTHON-6035 Add OpenTelemetry transaction spans - #2992

Draft
blink1073 wants to merge 1 commit into
PYTHON-5947-otel-1-operationsfrom
PYTHON-5947-otel-2-transactions
Draft

PYTHON-6035 Add OpenTelemetry transaction spans#2992
blink1073 wants to merge 1 commit into
PYTHON-5947-otel-1-operationsfrom
PYTHON-5947-otel-2-transactions

Conversation

@blink1073

@blink1073 blink1073 commented Aug 17, 2026

Copy link
Copy Markdown
Member

PYTHON-6035

Second of four PRs splitting #2964. Base is PYTHON-5947-otel-1-operations.

Branch Contents
1 PYTHON-5947-otel-1-operations operation spans
2 PYTHON-5947-otel-2-transactions transaction spans (this PR)
3 PYTHON-5947-otel-3-unified unified runner and vendored fixtures
4 PYTHON-5947-otel-4-getmore getMore spans

Changes in this PR

Wraps a transaction's operation spans in a "transaction" span, per the OpenTelemetry driver specification, so the operations belonging to one transaction group under it instead of appearing as unrelated siblings.

  • A "transaction" span covers start_transaction() through commit_transaction()/abort_transaction(), with the operations inside nested under it.
  • A retried with_transaction() produces one span for the whole call rather than one per attempt.
  • A nested or concurrent with_transaction() call on the same session now raises InvalidOperation instead of leaking the outer call's span.
  • Committing or aborting a transaction that never started on the server closes its span instead of leaving it open.
  • A transaction span parents only the operations of its own session, so a concurrent unrelated session cannot be captured by it.

with_transaction's retry loop moves into a helper to keep the span handling readable; diffing with whitespace ignored shows the loop body untouched.

Test Plan

  • 10 new tests each side: operations nesting under the transaction span, one span across with_transaction retries, retried commits, early-return commit and abort paths, spans across two sessions, and the nested-call guard.
  • Locally against a replica set: 20 new tests pass, along with test_transactions.py and PR 1's 98 otel tests. just lint clean.

Transaction spans only run on a replica set, which PR 1's expanded Evergreen variant covers in full.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 1c0a690 to 1852427 Compare August 17, 2026 22:39
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 1852427 to e6cb0d5 Compare August 17, 2026 22:50
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from e6cb0d5 to 820fb1d Compare August 17, 2026 23:09
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 820fb1d to 0c73147 Compare August 18, 2026 01:40
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 0c73147 to c936c5a Compare August 18, 2026 01:54
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from c936c5a to 55eae9c Compare August 18, 2026 02:06
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 55eae9c to 252d25e Compare August 18, 2026 10:22
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 252d25e to 36c501e Compare August 18, 2026 10:29
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 36c501e to b70f3b6 Compare August 18, 2026 10:44
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch 2 times, most recently from 39d9eaa to cb1d5c5 Compare August 18, 2026 11:06
@blink1073 blink1073 changed the title PYTHON-5947 Add OpenTelemetry transaction spans PYTHON-6035 Add OpenTelemetry transaction spans Aug 18, 2026
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from cb1d5c5 to 96daee2 Compare August 18, 2026 11:18
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 96daee2 to 2a7a131 Compare August 18, 2026 11:42
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 2a7a131 to bf998fc Compare August 18, 2026 11:50
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from bf998fc to 7f50b68 Compare August 18, 2026 12:02
Wrap a transaction's operation spans in a "transaction" pseudo-span, per the
OpenTelemetry driver specification. The span is stored on the session's
_Transaction and passed as the explicit parent when an operation span starts,
rather than read from ambient context, so a concurrently running unrelated
session cannot pick up this transaction by accident.

with_transaction() pins one span across all of its retries, so a retried call
still yields a single span rather than one per attempt. Its retry loop moves
into a helper to keep the span bookkeeping readable. A nested with_transaction()
call on the same session now raises instead of clobbering and leaking the outer
call's span, and a direct-API commit retry starts a fresh span, the previous
attempt having already ended its own.
@blink1073
blink1073 force-pushed the PYTHON-5947-otel-2-transactions branch from 7f50b68 to b5dee1f Compare August 18, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant