Skip to content

Commit fb43ab2

Browse files
committed
pymongo
1 parent 39f5620 commit fb43ab2

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/scout_apm/instruments/pymongo.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import logging
55

66
# Used in the exec() call below.
7-
from scout_apm.core.monkey import monkeypatch_method, unpatch_method # noqa: F401
7+
import wrapt # noqa: F401
8+
89
from scout_apm.core.tracked_request import TrackedRequest # noqa: F401
910

1011
logger = logging.getLogger(__name__)
@@ -77,17 +78,19 @@ def install(self):
7778
for method_str in self.__class__.PYMONGO_METHODS:
7879
try:
7980
code_str = """
80-
@monkeypatch_method(Collection)
81-
def {method_str}(original, self, *args, **kwargs):
81+
@wrapt.decorator
82+
def wrapped_{method_str}(wrapped, instance, args, kwargs):
8283
tracked_request = TrackedRequest.instance()
8384
name = '/'.join(['MongoDB', self.name, '{camel_name}'])
8485
span = tracked_request.start_span(operation=name, ignore_children=True)
8586
span.tag('name', self.name)
8687
8788
try:
88-
return original(*args, **kwargs)
89+
return wrapped(*args, **kwargs)
8990
finally:
9091
tracked_request.stop_span()
92+
93+
Collection.{method_str} = wrapped_{method_str}(Collection.{method_str})
9194
""".format(
9295
method_str=method_str,
9396
camel_name="".join(c.title() for c in method_str.split("_")),
@@ -113,4 +116,4 @@ def uninstall(self):
113116
from pymongo.collection import Collection
114117

115118
for method_str in self.__class__.PYMONGO_METHODS:
116-
unpatch_method(Collection, method_str)
119+
setattr(Collection, method_str, getattr(Collection, method_str).__wrapped__)

0 commit comments

Comments
 (0)