Add MongoDB driver handshake metadata#821
Open
alexbevi wants to merge 1 commit into
Open
Conversation
Identify Burr in the MongoDB handshake so server-side telemetry can
distinguish Burr traffic. A module-level constant
_DRIVER_INFO = DriverInfo(name="Burr", version=_VERSION) is defined
once in b_pymongo.py and used at every client construction site.
b_pymongo.MongoDBBasePersister has three patterns:
Pattern A – from_values() constructs MongoClient:
mongo_client_kwargs.setdefault("driver", _DRIVER_INFO) guards against
overriding a caller-supplied driver value.
Pattern B – __init__() receives an existing client:
if hasattr(client, "append_metadata"): client.append_metadata(_DRIVER_INFO)
append_metadata is idempotent; the hasattr guard keeps this safe with
older pymongo versions that predate the API.
Pattern A – __setstate__() reconstructs MongoClient after unpickling:
driver=_DRIVER_INFO passed directly.
b_mongodb.py is a deprecated shim that constructs its own MongoClient
instances in from_values() and __init__(). Both receive the same
setdefault guard, importing _DRIVER_INFO from b_pymongo.py.
Version is read via importlib.metadata("apache-burr") with a try/except
fallback so it works whether or not the package is pip-installed.
Adds tests/integrations/persisters/test_b_pymongo_driver_info.py
(no live DB required) verifying all five behaviours:
- _DRIVER_INFO has name="Burr" and matches the installed version
- from_values() passes driver=_DRIVER_INFO to MongoClient
- from_values() does not override a caller-supplied driver
- __init__() calls append_metadata(_DRIVER_INFO) when the client supports it
- __init__() does not raise when client lacks append_metadata
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
This PR identifies Burr in the MongoDB driver handshake, enabling server-side telemetry to attribute connections back to the library. MongoDB records this metadata at connection time, making it visible in
currentOp, Atlas performance advisor, andmongodlogs.What changes
A module-level constant is defined once in
b_pymongo.py:This is injected at every
MongoClientconstruction site and, for the case where a caller passes in an existing client, viaappend_metadata.How it appears in the logs
On a MongoDB 4.4+ server, the
isMaster/hellohandshake carries thefollowing client metadata document (truncated to key fields):
{ "driver": { "name": "PyMongo|Burr", "version": "4.9.2|0.42.0" }, "platform": "CPython 3.10.3.final.0" }In
mongoddiagnostic logs this appears as:The
PyMongo|Burrdriver name lets operators distinguish Burr connections from bare PyMongo connections in server logs, Atlas metrics, anddb.currentOp().Testing
Six unit tests added in
tests/integrations/persisters/test_b_pymongo_driver_info.py(no live database required):
_DRIVER_INFOhasname="Burr"and version matching the installed packagefrom_values()passesdriver=_DRIVER_INFOtoMongoClientfrom_values()does not override a caller-supplieddriver__init__()callsappend_metadata(_DRIVER_INFO)when the client supports it__init__()does not raise when the client lacksappend_metadata(older pymongo)No version constraint change is needed —
DriverInfois available sincepymongo 4.0, and
append_metadatasince 4.14; the existing unconstrained"pymongo"dependency satisfies both.Checklist