Skip to content

Commit faf3ccc

Browse files
committed
Add async_wrapper to datastore_trace api
1 parent dcc92a9 commit faf3ccc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

newrelic/api/datastore_trace.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import functools
1616

1717
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
1919
from newrelic.common.object_wrapper import FunctionWrapper, wrap_object
2020
from newrelic.core.datastore_node import DatastoreNode
2121

@@ -125,7 +125,7 @@ def create_node(self):
125125
)
126126

127127

128-
def DatastoreTraceWrapper(wrapped, product, target, operation):
128+
def DatastoreTraceWrapper(wrapped, product, target, operation, async_wrapper=None):
129129
"""Wraps a method to time datastore queries.
130130
131131
:param wrapped: The function to apply the trace to.
@@ -141,6 +141,8 @@ def DatastoreTraceWrapper(wrapped, product, target, operation):
141141
library.
142142
:type operation: str or callable
143143
: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
144146
145147
This is typically used to wrap datastore queries such as calls to Redis or
146148
ElasticSearch.
@@ -155,7 +157,7 @@ def DatastoreTraceWrapper(wrapped, product, target, operation):
155157
"""
156158

157159
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)
159161
if not wrapper:
160162
parent = current_trace()
161163
if not parent:
@@ -198,7 +200,7 @@ def _nr_datastore_trace_wrapper_(wrapped, instance, args, kwargs):
198200
return FunctionWrapper(wrapped, _nr_datastore_trace_wrapper_)
199201

200202

201-
def datastore_trace(product, target, operation):
203+
def datastore_trace(product, target, operation, async_wrapper=None):
202204
"""Decorator allows datastore query to be timed.
203205
204206
:param product: The name of the vendor.
@@ -211,6 +213,8 @@ def datastore_trace(product, target, operation):
211213
or the name of any API function/method in the client
212214
library.
213215
:type operation: str
216+
:param async_wrapper: An async trace wrapper from newrelic.common.async_wrapper.
217+
:type async_wrapper: callable or None
214218
215219
This is typically used to decorate datastore queries such as calls to Redis
216220
or ElasticSearch.
@@ -224,10 +228,10 @@ def datastore_trace(product, target, operation):
224228
... time.sleep(*args, **kwargs)
225229
226230
"""
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)
228232

229233

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):
231235
"""Method applies custom timing to datastore query.
232236
233237
:param module: Module containing the method to be instrumented.
@@ -244,6 +248,8 @@ def wrap_datastore_trace(module, object_path, product, target, operation):
244248
or the name of any API function/method in the client
245249
library.
246250
:type operation: str
251+
:param async_wrapper: An async trace wrapper from newrelic.common.async_wrapper.
252+
:type async_wrapper: callable or None
247253
248254
This is typically used to time database query method calls such as Redis
249255
GET.
@@ -256,4 +262,4 @@ def wrap_datastore_trace(module, object_path, product, target, operation):
256262
... 'sleep')
257263
258264
"""
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

Comments
 (0)