Skip to content
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

fix(pymongo): update pymongo integration for compatibility with 4.5.x [backport 1.18] #6775

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .riot/requirements/1854ba5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/1854ba5.in
# pip-compile --no-annotate .riot/requirements/1854ba5.in
#
attrs==22.2.0
coverage[toml]==7.2.2
dnspython==2.3.0
attrs==23.1.0
coverage[toml]==7.3.0
dnspython==2.4.2
hypothesis==6.45.0
iniconfig==2.0.0
mock==5.0.1
mock==5.1.0
mongoengine==0.27.0
opentracing==2.4.0
packaging==23.0
pluggy==1.0.0
pymongo==4.3.3
pytest==7.2.2
pytest-cov==4.0.0
pytest-mock==3.10.0
packaging==23.1
pluggy==1.2.0
pymongo==4.5.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
sortedcontainers==2.4.0
24 changes: 11 additions & 13 deletions .riot/requirements/1922cee.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/1922cee.in
#
attrs==22.2.0
click==8.1.3
coverage[toml]==7.2.2
exceptiongroup==1.1.1
attrs==23.1.0
click==8.1.7
coverage[toml]==7.3.0
hypothesis==6.45.0
iniconfig==2.0.0
mock==5.0.1
mock==5.1.0
opentracing==2.4.0
packaging==23.0
pluggy==1.0.0
pytest==7.2.2
pytest-cov==4.0.0
pytest-mock==3.10.0
slotscheck==0.16.5
packaging==23.1
pluggy==1.3.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
slotscheck==0.17.0
sortedcontainers==2.4.0
tomli==2.0.1
26 changes: 0 additions & 26 deletions .riot/requirements/1b81325.txt

This file was deleted.

24 changes: 12 additions & 12 deletions .riot/requirements/1fd1158.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/1fd1158.in
# pip-compile --no-annotate .riot/requirements/1fd1158.in
#
attrs==22.2.0
coverage[toml]==7.2.2
dnspython==2.3.0
exceptiongroup==1.1.1
attrs==23.1.0
coverage[toml]==7.3.0
dnspython==2.4.2
exceptiongroup==1.1.3
hypothesis==6.45.0
iniconfig==2.0.0
mock==5.0.1
mock==5.1.0
mongoengine==0.27.0
opentracing==2.4.0
packaging==23.0
pluggy==1.0.0
pymongo==4.3.3
pytest==7.2.2
pytest-cov==4.0.0
pytest-mock==3.10.0
packaging==23.1
pluggy==1.2.0
pymongo==4.5.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
sortedcontainers==2.4.0
tomli==2.0.1
23 changes: 23 additions & 0 deletions .riot/requirements/41bf6ef.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/41bf6ef.in
#
attrs==23.1.0
coverage[toml]==7.3.0
dnspython==2.4.2
exceptiongroup==1.1.3
hypothesis==6.45.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.27.0
opentracing==2.4.0
packaging==23.1
pluggy==1.2.0
pymongo==4.5.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
sortedcontainers==2.4.0
tomli==2.0.1
26 changes: 26 additions & 0 deletions .riot/requirements/4bf8418.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# This file is autogenerated by pip-compile with Python 3.7
# by the following command:
#
# pip-compile --config=pyproject.toml --no-annotate --resolver=backtracking .riot/requirements/4bf8418.in
#
attrs==23.1.0
coverage[toml]==7.2.7
dnspython==2.3.0
exceptiongroup==1.1.3
hypothesis==6.45.0
importlib-metadata==6.7.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.27.0
opentracing==2.4.0
packaging==23.1
pluggy==1.2.0
pymongo==4.5.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
sortedcontainers==2.4.0
tomli==2.0.1
typing-extensions==4.7.1
zipp==3.15.0
23 changes: 0 additions & 23 deletions .riot/requirements/a1e6119.txt

This file was deleted.

23 changes: 0 additions & 23 deletions .riot/requirements/bddee76.txt

This file was deleted.

2 changes: 1 addition & 1 deletion .riot/requirements/c01e3b4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/c01e3b4.in
# pip-compile --no-annotate .riot/requirements/c01e3b4.in
#
attrs==22.2.0
coverage[toml]==7.2.2
Expand Down
23 changes: 23 additions & 0 deletions .riot/requirements/f92d9dc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/f92d9dc.in
#
attrs==23.1.0
coverage[toml]==7.3.0
dnspython==2.4.2
exceptiongroup==1.1.3
hypothesis==6.45.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.27.0
opentracing==2.4.0
packaging==23.1
pluggy==1.2.0
pymongo==4.5.0
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
sortedcontainers==2.4.0
tomli==2.0.1
31 changes: 20 additions & 11 deletions ddtrace/contrib/pymongo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,26 @@ def _datadog_trace_operation(self, operation):
span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, sample_rate)
return span

# Pymongo >= 3.12
if VERSION >= (4, 5, 0):

@contextlib.contextmanager
def checkout(self, *args, **kwargs):
with self.__wrapped__.checkout(*args, **kwargs) as s:
if not isinstance(s, TracedSocket):
s = TracedSocket(s)
ddtrace.Pin.get_from(self).onto(s)
yield s

else:

@contextlib.contextmanager
def get_socket(self, *args, **kwargs):
with self.__wrapped__.get_socket(*args, **kwargs) as s:
if not isinstance(s, TracedSocket):
s = TracedSocket(s)
ddtrace.Pin.get_from(self).onto(s)
yield s

if VERSION >= (3, 12, 0):

def run_operation(self, sock_info, operation, *args, **kwargs):
Expand All @@ -159,7 +178,6 @@ def run_operation(self, sock_info, operation, *args, **kwargs):
set_query_rowcount(docs=result.docs, span=span)
return result

# Pymongo >= 3.9, <3.12
elif (3, 9, 0) <= VERSION < (3, 12, 0):

def run_operation_with_response(self, sock_info, operation, *args, **kwargs):
Expand All @@ -175,7 +193,6 @@ def run_operation_with_response(self, sock_info, operation, *args, **kwargs):
set_query_rowcount(docs=result.docs, span=span)
return result

# Pymongo < 3.9
else:

def send_message_with_response(self, operation, *args, **kwargs):
Expand All @@ -200,14 +217,6 @@ def send_message_with_response(self, operation, *args, **kwargs):
set_query_rowcount(docs=docs, span=span)
return result

@contextlib.contextmanager
def get_socket(self, *args, **kwargs):
with self.__wrapped__.get_socket(*args, **kwargs) as s:
if not isinstance(s, TracedSocket):
s = TracedSocket(s)
ddtrace.Pin.get_from(self).onto(s)
yield s

@staticmethod
def _is_query(op):
# NOTE: _Query should always have a spec field
Expand Down
11 changes: 8 additions & 3 deletions ddtrace/contrib/pymongo/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
# Original Client class
_MongoClient = pymongo.MongoClient

_VERSION = pymongo.version_tuple
_CHECKOUT_FN_NAME = "get_socket" if _VERSION < (4, 5) else "checkout"


def patch():
patch_pymongo_module()
Expand All @@ -50,15 +53,15 @@ def patch_pymongo_module():
# Whenever a pymongo command is invoked, the lib either:
# - Creates a new socket & performs a TCP handshake
# - Grabs a socket already initialized before
_w("pymongo.server", "Server.get_socket", traced_get_socket)
_w("pymongo.server", "Server.%s" % _CHECKOUT_FN_NAME, traced_get_socket)


def unpatch_pymongo_module():
if not getattr(pymongo, "_datadog_patch", False):
return
setattr(pymongo, "_datadog_patch", False)

_u(pymongo.server.Server, "get_socket")
_u(pymongo.server.Server, _CHECKOUT_FN_NAME)


@contextlib.contextmanager
Expand All @@ -70,7 +73,9 @@ def traced_get_socket(wrapped, instance, args, kwargs):
return

with pin.tracer.trace(
"pymongo.get_socket", service=trace_utils.int_service(pin, config.pymongo), span_type=SpanTypes.MONGODB
"pymongo.%s" % _CHECKOUT_FN_NAME,
service=trace_utils.int_service(pin, config.pymongo),
span_type=SpanTypes.MONGODB,
) as span:
span.set_tag_str(COMPONENT, config.pymongo.integration_name)
span.set_tag_str(db.SYSTEM, mongo.SERVICE)
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/pymongo-get-socket-808421288343ab81.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
pymongo: This upgrades the PyMongo integration to work with PyMongo versions 4.5.0 and above by choosing the root function
of the integration on the basis of the PyMongo version.
2 changes: 1 addition & 1 deletion riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def select_pys(min_version=MIN_PYTHON_VERSION, max_version=MAX_PYTHON_VERSION):
pkgs={"pymongo": ["~=3.4", "~=3.11", "~=3.13"]},
),
Venv(
pys=select_pys(min_version="3.7", max_version="3.9"), pkgs={"pymongo": ["~=3.11", "~=4.0", latest]}
pys=select_pys(min_version="3.7", max_version="3.9"), pkgs={"pymongo": ["~=3.11", "~=4.5", latest]}
),
Venv(
# pymongo added support for Python 3.10 in 3.12.1
Expand Down
Loading