4
4
import logging
5
5
6
6
# 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
+
8
9
from scout_apm .core .tracked_request import TrackedRequest # noqa: F401
9
10
10
11
logger = logging .getLogger (__name__ )
@@ -77,17 +78,19 @@ def install(self):
77
78
for method_str in self .__class__ .PYMONGO_METHODS :
78
79
try :
79
80
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):
82
83
tracked_request = TrackedRequest.instance()
83
84
name = '/'.join(['MongoDB', self.name, '{camel_name}'])
84
85
span = tracked_request.start_span(operation=name, ignore_children=True)
85
86
span.tag('name', self.name)
86
87
87
88
try:
88
- return original (*args, **kwargs)
89
+ return wrapped (*args, **kwargs)
89
90
finally:
90
91
tracked_request.stop_span()
92
+
93
+ Collection.{method_str} = wrapped_{method_str}(Collection.{method_str})
91
94
""" .format (
92
95
method_str = method_str ,
93
96
camel_name = "" .join (c .title () for c in method_str .split ("_" )),
@@ -113,4 +116,4 @@ def uninstall(self):
113
116
from pymongo .collection import Collection
114
117
115
118
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