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): use bytecode wrapping to trace pymongo clients #10516

Merged
merged 23 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
25 changes: 25 additions & 0 deletions .riot/requirements/12616cb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/12616cb.in
#
attrs==24.2.0
coverage[toml]==7.6.1
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==8.4.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.1
pluggy==1.5.0
pymongo==3.9.0
pytest==8.3.2
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-randomly==3.15.0
sortedcontainers==2.4.0
tomli==2.0.1
zipp==3.20.1
26 changes: 26 additions & 0 deletions .riot/requirements/14b37e6.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 --no-annotate --resolver=backtracking .riot/requirements/14b37e6.in
#
attrs==24.2.0
coverage[toml]==7.2.7
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==6.7.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.0
pluggy==1.2.0
pymongo==3.9.0
pytest==7.4.4
pytest-cov==4.1.0
pytest-mock==3.11.1
pytest-randomly==3.12.0
sortedcontainers==2.4.0
tomli==2.0.1
typing-extensions==4.7.1
zipp==3.15.0
25 changes: 25 additions & 0 deletions .riot/requirements/167d6de.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/167d6de.in
#
attrs==24.2.0
coverage[toml]==7.6.1
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==8.4.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.1
pluggy==1.5.0
pymongo==3.8.0
pytest==8.3.2
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-randomly==3.15.0
sortedcontainers==2.4.0
tomli==2.0.1
zipp==3.20.1
25 changes: 25 additions & 0 deletions .riot/requirements/1fd0884.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile --no-annotate --resolver=backtracking .riot/requirements/1fd0884.in
#
attrs==24.2.0
coverage[toml]==7.6.1
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==8.4.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.1
pluggy==1.5.0
pymongo==3.8.0
pytest==8.3.2
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-randomly==3.15.0
sortedcontainers==2.4.0
tomli==2.0.1
zipp==3.20.1
26 changes: 26 additions & 0 deletions .riot/requirements/3343686.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 --no-annotate --resolver=backtracking .riot/requirements/3343686.in
#
attrs==24.2.0
coverage[toml]==7.2.7
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==6.7.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.0
pluggy==1.2.0
pymongo==3.8.0
pytest==7.4.4
pytest-cov==4.1.0
pytest-mock==3.11.1
pytest-randomly==3.12.0
sortedcontainers==2.4.0
tomli==2.0.1
typing-extensions==4.7.1
zipp==3.15.0
25 changes: 25 additions & 0 deletions .riot/requirements/d66afaf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --no-annotate .riot/requirements/d66afaf.in
#
attrs==24.2.0
coverage[toml]==7.6.1
exceptiongroup==1.2.2
hypothesis==6.45.0
importlib-metadata==8.4.0
iniconfig==2.0.0
mock==5.1.0
mongoengine==0.29.0
opentracing==2.4.0
packaging==24.1
pluggy==1.5.0
pymongo==3.9.0
pytest==8.3.2
pytest-cov==5.0.0
pytest-mock==3.14.0
pytest-randomly==3.15.0
sortedcontainers==2.4.0
tomli==2.0.1
zipp==3.20.1
10 changes: 10 additions & 0 deletions ddtrace/contrib/internal/mongoengine/patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# TODO(mabdinur): Remove the pymongoengine integration, this integration does nothing special
# it just uses the pymongo integration and creates unnecessary pin objects
erikayasuda marked this conversation as resolved.
Show resolved Hide resolved
import mongoengine

from ..pymongo.patch import patch as patch_pymongo_module
from ..pymongo.patch import unpatch as unpatch_pymongo_module
from .trace import WrappedConnect


Expand All @@ -13,10 +17,16 @@ def get_version():


def patch():
if getattr(mongoengine, "_datadog_patch", False):
return
mongoengine.connect = WrappedConnect(_connect)
mongoengine._datadog_patch = True
patch_pymongo_module()


def unpatch():
if not getattr(mongoengine, "_datadog_patch", False):
return
mongoengine.connect = _connect
mongoengine._datadog_patch = False
unpatch_pymongo_module()
9 changes: 4 additions & 5 deletions ddtrace/contrib/internal/mongoengine/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import wrapt

import ddtrace
from ddtrace.contrib.internal.pymongo.client import TracedMongoClient

# keep the TracedMongoClient import to avoid breaking the public api
from ddtrace.contrib.internal.pymongo.client import TracedMongoClient # noqa: F401
from ddtrace.ext import mongo as mongox
from ddtrace.internal.schema import schematize_service_name

Expand All @@ -13,6 +15,7 @@
_SERVICE = schematize_service_name(mongox.SERVICE)


# TODO(mabdinur): Remove this class when ``ddtrace.contrib.mongoengine.trace`` is removed
class WrappedConnect(wrapt.ObjectProxy):
"""WrappedConnect wraps mongoengines 'connect' function to ensure
that all returned connections are wrapped for tracing.
Expand All @@ -26,10 +29,6 @@ def __call__(self, *args, **kwargs):
client = self.__wrapped__(*args, **kwargs)
pin = ddtrace.Pin.get_from(self)
if pin:
# mongoengine uses pymongo internally, so we can just piggyback on the
# existing pymongo integration and make sure that the connections it
# uses internally are traced.
client = TracedMongoClient(client)
ddtrace.Pin(service=pin.service, tracer=pin.tracer).onto(client)

return client
Loading
Loading