Skip to content

Commit 97a9675

Browse files
Kyle-VerhoogEmanuele Palazzetti
authored andcommitted
[pymongo] Fix multiple host kwarg (#535)
* [pymongo] replicate multiple kwarg from #369 * [pymongo] fix for multiple kwarg host bug
1 parent 07a6e93 commit 97a9675

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

ddtrace/contrib/pymongo/client.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,22 @@ def __init__(self, client=None, *args, **kwargs):
4040
# To support the former trace_mongo_client interface, we have to keep this old interface
4141
# TODO(Benjamin): drop it in a later version
4242
if not isinstance(client, _MongoClient):
43-
# Patched interface, instanciate the client
44-
# Note that, in that case, the client argument isn't a client, it's just the first arg
45-
client = _MongoClient(client, *args, **kwargs)
43+
# Patched interface, instantiate the client
44+
45+
# client is just the first arg which could be the host if it is
46+
# None, then it could be that the caller:
47+
48+
# if client is None then __init__ was:
49+
# 1) invoked with host=None
50+
# 2) not given a first argument (client defaults to None)
51+
# we cannot tell which case it is, but it should not matter since
52+
# the default value for host is None, in either case we can simply
53+
# not provide it as an argument
54+
if client is None:
55+
client = _MongoClient(*args, **kwargs)
56+
# else client is a value for host so just pass it along
57+
else:
58+
client = _MongoClient(client, *args, **kwargs)
4659

4760
super(TracedMongoClient, self).__init__(client)
4861
# NOTE[matt] the TracedMongoClient attempts to trace all of the network

tests/contrib/pymongo/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,20 @@ def test_service(self):
280280
assert s['app_type'] == 'db'
281281
assert s['app'] == 'mongodb'
282282

283+
def test_host_kwarg(self):
284+
# simulate what celery and django do when instantiating a new client
285+
conf = {
286+
'host': 'localhost'
287+
}
288+
client = pymongo.MongoClient(**conf)
289+
290+
conf = {
291+
'host': None
292+
}
293+
client = pymongo.MongoClient(**conf)
294+
295+
assert client
296+
283297

284298
class TestPymongoPatchConfigured(PymongoCore):
285299
"""Test suite for pymongo with a configured patched library"""

0 commit comments

Comments
 (0)