Skip to content

DRIVERS-2985: Allow on-demand client metadata updates after MongoClient initialization. #1798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin commented May 7, 2025

Description

MongoDB’s handshake specification allows libraries and frameworks that wrap MongoDB drivers (e.g., ODMs, integrations) to append their own metadata.

However, this mechanism assumes the wrapping layer creates the MongoClient. When a wrapping environment instead receives a pre-created MongoClient (e.g., via dependency injection), there was no way to append additional metadata.

Changes

This PR updates the handshake specification with the following additions:

  • Specifies that drivers MUST provide an API to allow updating client metadata (name, version, platform) after initialization.
  • Appended values must be joined to existing client.driver fields using a | delimiter.
  • Only new connections will include the updated metadata; existing connections MUST remain unaffected, as metadata propagation depends on the creation of new connections over time.
  • Adds suggested prose tests to validate the above behavior. As some drivers may not support a testing backdoor to intercept handshake command events, or may face technical limitations, the exact test implementation is left to the discretion of the driver team.

Please complete the following before merging:

Sorry, something went wrong.

vbabanin added 2 commits May 7, 2025 13:56
…ed MongoClients.
@vbabanin vbabanin self-assigned this May 9, 2025
vbabanin added 2 commits May 9, 2025 16:19
@vbabanin vbabanin requested a review from sleepyStick May 10, 2025 01:15
Copy link

@sleepyStick sleepyStick left a comment

Choose a reason for hiding this comment

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

Just a couple minor comments, everything else looks good! great work!

[Motor](https://www.mongodb.com/docs/drivers/motor/) wraps PyMongo, the following fields are updated to include Motor's
"driver info":

```typescript
{
{

Choose a reason for hiding this comment

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

is this white space change intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed in 5ce8b92, thanks!

appended to their respective fields, and be delimited by a `|` character. For example, when
### Metadata updates after MongoClient initialization

Drivers MUST provide an API that allows to append `DriverInfoOptions` to a MongoClient instance after initialization.
Copy link

@sleepyStick sleepyStick May 12, 2025

Choose a reason for hiding this comment

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

"...that allows to append..." reads weirdly to me? not sure if its just me or not though. It's possible that my brain just isn't parsing it correctly? I think my brain wants it to be "...that allows to append..."

Copy link
Member Author

@vbabanin vbabanin May 17, 2025

Choose a reason for hiding this comment

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

Yeah, you're right, that allows to append is not correct. Originally, it was supposed to be that allows wrapping libraries to append.

I think my brain wants it to be "...that allows to append..."

Did you mean something like 'that allows appending' or 'that allows [users/wrapping libraries] to append'?

I currently changed it to that allows appending to keep it more general and avoid over-specifying "users" or "wrapping libraries". Let me know what you think!

Changed in 5ce8b92

Choose a reason for hiding this comment

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

Yeah, this is much better. (upon re-reading, realized my suggestion at the end makes no sense because i wrote the same thing HAHA sorry for the confusion) thanks!

@vbabanin vbabanin requested a review from sleepyStick May 17, 2025 02:37
vbabanin added 2 commits May 18, 2025 21:15
@vbabanin vbabanin requested review from jyemin and ShaneHarvey May 20, 2025 19:37
@vbabanin vbabanin marked this pull request as ready for review May 20, 2025 19:52
@vbabanin vbabanin requested a review from a team as a code owner May 20, 2025 19:52
vbabanin and others added 5 commits May 20, 2025 14:30

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Co-authored-by: Jeff Yemin <[email protected]>
@vbabanin vbabanin requested a review from jyemin June 4, 2025 23:06
@vbabanin
Copy link
Member Author

vbabanin commented Jun 4, 2025

Lint errors are unrelated to the changes in this PR. See #1808 for reference.

@alcaeus
Copy link
Member

alcaeus commented Jun 5, 2025

@vbabanin #1808 was merged, so a rebase should fix the pre-commit checks here

appended to their respective fields, and be delimited by a `|` character. For example, when
### Metadata updates after MongoClient initialization

Drivers MUST provide an API that allows appending `DriverInfoOptions` to a `MongoClient` instance after initialization.
Copy link
Member

Choose a reason for hiding this comment

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

We usually give an example API (or at least function signature) that implementors can reference. Could we add that here?

For example, see how startSession is documented: https://github.com/mongodb/specifications/blob/master/source/sessions/driver-sessions.md#mongoclient-changes

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the reference! I have added explicit API guidance for clarity.

Previously, the specification left the mechanism for providing metadata during initialization up to the implementer.

(“Note that how these options are provided to a driver is left up to the implementer.”).

I have refined the language to clarify that this flexibility applies only during initialization. For subsequent metadata updates, the API guidance is now clearly defined, requiring the update method to reside on MongoClient.

Updated in: 4be80aa

Copy link

@sleepyStick sleepyStick left a comment

Choose a reason for hiding this comment

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

Great work!

2. Send a `ping` command to the server and verify:

- The command succeeds.
- The wrapping library metadata is appended to the respective `client.driver` fields of the `hello` command.
Copy link
Member

Choose a reason for hiding this comment

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

Could we simplify this test by removing the "library metadata" in step 1 and removing this assert from step 2?

Copy link
Member Author

@vbabanin vbabanin Jun 6, 2025

Choose a reason for hiding this comment

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

Good catch - I missed removing the assertions in test 3. Removed assertions and library metadata in f0315f4, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Actually, couldn't this particular test be a unified spec test? It doesn't rely on any mocking.

Copy link
Member Author

@vbabanin vbabanin Jun 10, 2025

Choose a reason for hiding this comment

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

Yes, good point - it makes sense to convert this to a unified test. I’ve updated it accordingly.

I also added an assertion to verify no new connection is created. Since unified tests don’t assume a test backdoor, we can’t directly assert on handshake commands. However, verifying that no new connection is established indirectly ensures no hello was sent during the handshake. Any subsequent hello's over an existing connection would appear as regular commands and cause the test to fail.

@vbabanin vbabanin requested a review from ShaneHarvey June 6, 2025 20:35
@vbabanin vbabanin requested a review from a team as a code owner June 10, 2025 04:10
@vbabanin vbabanin requested review from alcaeus and removed request for a team June 10, 2025 04:10
vbabanin added 4 commits June 9, 2025 21:18
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.

None yet

5 participants