Skip to content

PYTHON-5963 Only attach a shared operation id to retry attempts when APM/logging is enabled - #2960

Merged
NoahStapp merged 1 commit into
mongodb:mainfrom
NoahStapp:PYTHON-5963
Jul 24, 2026
Merged

PYTHON-5963 Only attach a shared operation id to retry attempts when APM/logging is enabled#2960
NoahStapp merged 1 commit into
mongodb:mainfrom
NoahStapp:PYTHON-5963

Conversation

@NoahStapp

@NoahStapp NoahStapp commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5963

Changes in this PR

PYTHON-5903 ensured that all retry attempts share a single operation id. However, it does so even if there is no active event listener or logger, causing a ~5-10% performance regression due to extra overhead on very fast operations such as pings.

Test Plan

Added a new test to verify correct behavior.

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?

@NoahStapp
NoahStapp requested a review from a team as a code owner July 24, 2026 15:45
@NoahStapp
NoahStapp requested review from Copilot and sleepyStick July 24, 2026 15:45
@NoahStapp NoahStapp changed the title PYTHON-5963 Only attach a shared operation id to retry attempts when… PYTHON-5963 Only attach a shared operation id to retry attempts when APM/logging is enabled Jul 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a performance regression introduced by always generating a stable “operation id” for retry attempts, even when command monitoring (APM) and structured logging are both disabled. It makes operation-id generation conditional on observability being enabled, while keeping the “shared operation id across retries” behavior when APM/logging is active.

Changes:

  • Conditionally generate and attach a shared operation id for retry attempts only when command monitoring listeners are enabled or command/server-selection structured logging is enabled.
  • Skip the _op_id._OpIdContext overhead entirely when no operation id is present.
  • Add sync + async regression tests asserting that no operation id is generated/propagated when neither APM nor logging is enabled.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pymongo/asynchronous/mongo_client.py Only generate/use a shared operation id for retries when APM/logging is enabled (async path).
pymongo/synchronous/mongo_client.py Same conditional operation-id behavior for the synchronous client.
test/asynchronous/test_operation_id_retry.py Adds async test ensuring retries do not generate/attach an operation id when observability is off.
test/test_operation_id_retry.py Adds sync equivalent of the “no APM/logging => no operation id” regression test.
pymongo/_op_id.py Updates internal docstring to reflect the new conditional behavior.

Comment on lines +144 to +148
# Make sure APM and logging are disabled
for logger in (_COMMAND_LOGGER, _SERVER_SELECTION_LOGGER):
self.assertFalse(logger.isEnabledFor(logging.DEBUG))
self.assertFalse(client._event_listeners.enabled_for_commands)

Comment on lines +146 to +150
# Make sure APM and logging are disabled
for logger in (_COMMAND_LOGGER, _SERVER_SELECTION_LOGGER):
self.assertFalse(logger.isEnabledFor(logging.DEBUG))
self.assertFalse(client._event_listeners.enabled_for_commands)

Comment thread pymongo/_op_id.py
Comment on lines 15 to 19
"""Internal helpers for the APM operation id.

The retryable read/write logic sets OP_ID for the duration of each attempt so
The retryable read/write logic sets OP_ID if APM/logging is enabled for the duration of each attempt so
that every attempt of one logical operation publishes the same operation_id.
Commands run outside that scope (handshake, auth, killCursors, pinned-cursor
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@NoahStapp

Copy link
Copy Markdown
Contributor Author

Test failures are all known and unrelated.

@NoahStapp
NoahStapp merged commit 60aa8cd into mongodb:main Jul 24, 2026
75 of 89 checks passed
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.

4 participants