15
15
import functools
16
16
17
17
from newrelic .api .time_trace import TimeTrace , current_trace
18
- from newrelic .common .async_wrapper import async_wrapper
18
+ from newrelic .common .async_wrapper import async_wrapper as get_async_wrapper
19
19
from newrelic .common .object_wrapper import FunctionWrapper , wrap_object
20
20
from newrelic .core .datastore_node import DatastoreNode
21
21
@@ -125,7 +125,7 @@ def create_node(self):
125
125
)
126
126
127
127
128
- def DatastoreTraceWrapper (wrapped , product , target , operation ):
128
+ def DatastoreTraceWrapper (wrapped , product , target , operation , async_wrapper = None ):
129
129
"""Wraps a method to time datastore queries.
130
130
131
131
:param wrapped: The function to apply the trace to.
@@ -141,6 +141,8 @@ def DatastoreTraceWrapper(wrapped, product, target, operation):
141
141
library.
142
142
:type operation: str or callable
143
143
:rtype: :class:`newrelic.common.object_wrapper.FunctionWrapper`
144
+ :param async_wrapper: An async trace wrapper from newrelic.common.async_wrapper.
145
+ :type async_wrapper: callable or None
144
146
145
147
This is typically used to wrap datastore queries such as calls to Redis or
146
148
ElasticSearch.
@@ -155,7 +157,7 @@ def DatastoreTraceWrapper(wrapped, product, target, operation):
155
157
"""
156
158
157
159
def _nr_datastore_trace_wrapper_ (wrapped , instance , args , kwargs ):
158
- wrapper = async_wrapper (wrapped )
160
+ wrapper = async_wrapper if async_wrapper is not None else get_async_wrapper (wrapped )
159
161
if not wrapper :
160
162
parent = current_trace ()
161
163
if not parent :
@@ -198,7 +200,7 @@ def _nr_datastore_trace_wrapper_(wrapped, instance, args, kwargs):
198
200
return FunctionWrapper (wrapped , _nr_datastore_trace_wrapper_ )
199
201
200
202
201
- def datastore_trace (product , target , operation ):
203
+ def datastore_trace (product , target , operation , async_wrapper = None ):
202
204
"""Decorator allows datastore query to be timed.
203
205
204
206
:param product: The name of the vendor.
@@ -211,6 +213,8 @@ def datastore_trace(product, target, operation):
211
213
or the name of any API function/method in the client
212
214
library.
213
215
:type operation: str
216
+ :param async_wrapper: An async trace wrapper from newrelic.common.async_wrapper.
217
+ :type async_wrapper: callable or None
214
218
215
219
This is typically used to decorate datastore queries such as calls to Redis
216
220
or ElasticSearch.
@@ -224,10 +228,10 @@ def datastore_trace(product, target, operation):
224
228
... time.sleep(*args, **kwargs)
225
229
226
230
"""
227
- return functools .partial (DatastoreTraceWrapper , product = product , target = target , operation = operation )
231
+ return functools .partial (DatastoreTraceWrapper , product = product , target = target , operation = operation , async_wrapper = async_wrapper )
228
232
229
233
230
- def wrap_datastore_trace (module , object_path , product , target , operation ):
234
+ def wrap_datastore_trace (module , object_path , product , target , operation , async_wrapper = None ):
231
235
"""Method applies custom timing to datastore query.
232
236
233
237
:param module: Module containing the method to be instrumented.
@@ -244,6 +248,8 @@ def wrap_datastore_trace(module, object_path, product, target, operation):
244
248
or the name of any API function/method in the client
245
249
library.
246
250
:type operation: str
251
+ :param async_wrapper: An async trace wrapper from newrelic.common.async_wrapper.
252
+ :type async_wrapper: callable or None
247
253
248
254
This is typically used to time database query method calls such as Redis
249
255
GET.
@@ -256,4 +262,4 @@ def wrap_datastore_trace(module, object_path, product, target, operation):
256
262
... 'sleep')
257
263
258
264
"""
259
- wrap_object (module , object_path , DatastoreTraceWrapper , (product , target , operation ))
265
+ wrap_object (module , object_path , DatastoreTraceWrapper , (product , target , operation , async_wrapper ))
0 commit comments