PYTHON-5963 Only attach a shared operation id to retry attempts when APM/logging is enabled - #2960
Merged
Merged
Conversation
…APM/logging is enabled
Contributor
There was a problem hiding this comment.
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._OpIdContextoverhead 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 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 |
sleepyStick
approved these changes
Jul 24, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Author
|
Test failures are all known and unrelated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 any followup work tracked in a JIRA ticket? If so, add link(s).Checklist for Reviewer