|
15 | 15 | import functools |
16 | 16 |
|
17 | 17 | from newrelic.api.time_trace import current_trace, notice_error |
| 18 | +from newrelic.common.async_wrapper import async_wrapper as get_async_wrapper |
18 | 19 | from newrelic.common.object_wrapper import FunctionWrapper, wrap_object |
19 | 20 |
|
20 | 21 |
|
@@ -43,17 +44,31 @@ def __exit__(self, exc, value, tb): |
43 | 44 | ) |
44 | 45 |
|
45 | 46 |
|
46 | | -def ErrorTraceWrapper(wrapped, ignore=None, expected=None, status_code=None): |
47 | | - def wrapper(wrapped, instance, args, kwargs): |
48 | | - parent = current_trace() |
| 47 | +def ErrorTraceWrapper(wrapped, ignore=None, expected=None, status_code=None, async_wrapper=None): |
| 48 | + def literal_wrapper(wrapped, instance, args, kwargs): |
| 49 | + # Determine if the wrapped function is async or sync |
| 50 | + wrapper = async_wrapper if async_wrapper is not None else get_async_wrapper(wrapped) |
| 51 | + # Sync function path |
| 52 | + if not wrapper: |
| 53 | + parent = current_trace() |
| 54 | + if not parent: |
| 55 | + # No active tracing context so just call the wrapped function directly |
| 56 | + return wrapped(*args, **kwargs) |
| 57 | + # Async function path |
| 58 | + else: |
| 59 | + # For async functions, the async wrapper will handle trace context propagation |
| 60 | + parent = None |
49 | 61 |
|
50 | | - if parent is None: |
51 | | - return wrapped(*args, **kwargs) |
| 62 | + trace = ErrorTrace(ignore, expected, status_code, parent=parent) |
| 63 | + |
| 64 | + if wrapper: |
| 65 | + # The async wrapper handles the context management for us |
| 66 | + return wrapper(wrapped, trace)(*args, **kwargs) |
52 | 67 |
|
53 | | - with ErrorTrace(ignore, expected, status_code, parent=parent): |
| 68 | + with trace: |
54 | 69 | return wrapped(*args, **kwargs) |
55 | 70 |
|
56 | | - return FunctionWrapper(wrapped, wrapper) |
| 71 | + return FunctionWrapper(wrapped, literal_wrapper) |
57 | 72 |
|
58 | 73 |
|
59 | 74 | def error_trace(ignore=None, expected=None, status_code=None): |
|
0 commit comments