1414
1515from newrelic .api .asgi_application import wrap_asgi_application
1616from newrelic .api .background_task import BackgroundTaskWrapper
17- from newrelic .api .time_trace import current_trace
18- from newrelic .api .function_trace import FunctionTraceWrapper , wrap_function_trace
17+ from newrelic .api .function_trace import FunctionTraceWrapper
18+ from newrelic .api .time_trace import current_trace , notice_error
19+ from newrelic .api .transaction import current_transaction
20+ from newrelic .common .coroutine import is_coroutine_function
1921from newrelic .common .object_names import callable_name
20- from newrelic .common .object_wrapper import wrap_function_wrapper , function_wrapper , FunctionWrapper
21- from newrelic .core .trace_cache import trace_cache
22- from newrelic .api .time_trace import notice_error
22+ from newrelic .common .object_wrapper import (
23+ FunctionWrapper ,
24+ function_wrapper ,
25+ wrap_function_wrapper ,
26+ )
2327from newrelic .core .config import should_ignore_error
24- from newrelic .common .coroutine import is_coroutine_function
25- from newrelic .api .transaction import current_transaction
28+ from newrelic .core .trace_cache import trace_cache
2629
2730
2831def framework_details ():
@@ -93,10 +96,19 @@ def wrap_request(wrapped, instance, args, kwargs):
9396def wrap_background_method (wrapped , instance , args , kwargs ):
9497 func = getattr (instance , "func" , None )
9598 if func :
96- instance .func = BackgroundTaskWrapper (func )
99+ instance .func = wrap_background_task (func )
97100 return wrapped (* args , ** kwargs )
98101
99102
103+ @function_wrapper
104+ def wrap_background_task (wrapped , instance , args , kwargs ):
105+ transaction = current_transaction (active_only = False )
106+ if not transaction :
107+ return BackgroundTaskWrapper (wrapped )(* args , ** kwargs )
108+ else :
109+ return FunctionTraceWrapper (wrapped )(* args , ** kwargs )
110+
111+
100112async def middleware_wrapper (wrapped , instance , args , kwargs ):
101113 transaction = current_transaction ()
102114 if transaction :
@@ -158,7 +170,9 @@ async def wrap_exception_handler_async(coro, exc):
158170
159171def wrap_exception_handler (wrapped , instance , args , kwargs ):
160172 if is_coroutine_function (wrapped ):
161- return wrap_exception_handler_async (FunctionTraceWrapper (wrapped )(* args , ** kwargs ), bind_exc (* args , ** kwargs ))
173+ return wrap_exception_handler_async (
174+ FunctionTraceWrapper (wrapped )(* args , ** kwargs ), bind_exc (* args , ** kwargs )
175+ )
162176 else :
163177 with RequestContext (bind_request (* args , ** kwargs )):
164178 response = FunctionTraceWrapper (wrapped )(* args , ** kwargs )
@@ -168,14 +182,16 @@ def wrap_exception_handler(wrapped, instance, args, kwargs):
168182
169183def wrap_server_error_handler (wrapped , instance , args , kwargs ):
170184 result = wrapped (* args , ** kwargs )
171- handler = getattr (instance , ' handler' , None )
185+ handler = getattr (instance , " handler" , None )
172186 if handler :
173187 instance .handler = FunctionWrapper (handler , wrap_exception_handler )
174188 return result
175189
176190
177191def wrap_add_exception_handler (wrapped , instance , args , kwargs ):
178- exc_class_or_status_code , handler , args , kwargs = bind_add_exception_handler (* args , ** kwargs )
192+ exc_class_or_status_code , handler , args , kwargs = bind_add_exception_handler (
193+ * args , ** kwargs
194+ )
179195 handler = FunctionWrapper (handler , wrap_exception_handler )
180196 return wrapped (exc_class_or_status_code , handler , * args , ** kwargs )
181197
@@ -207,25 +223,36 @@ def instrument_starlette_requests(module):
207223
208224
209225def instrument_starlette_middleware_errors (module ):
210- wrap_function_wrapper (module , "ServerErrorMiddleware.__call__" , error_middleware_wrapper )
226+ wrap_function_wrapper (
227+ module , "ServerErrorMiddleware.__call__" , error_middleware_wrapper
228+ )
211229
212- wrap_function_wrapper (module , "ServerErrorMiddleware.__init__" , wrap_server_error_handler )
230+ wrap_function_wrapper (
231+ module , "ServerErrorMiddleware.__init__" , wrap_server_error_handler
232+ )
213233
214- wrap_function_wrapper (module , "ServerErrorMiddleware.error_response" , wrap_exception_handler )
234+ wrap_function_wrapper (
235+ module , "ServerErrorMiddleware.error_response" , wrap_exception_handler
236+ )
215237
216- wrap_function_wrapper (module , "ServerErrorMiddleware.debug_response" , wrap_exception_handler )
238+ wrap_function_wrapper (
239+ module , "ServerErrorMiddleware.debug_response" , wrap_exception_handler
240+ )
217241
218242
219243def instrument_starlette_exceptions (module ):
220- wrap_function_wrapper (module , "ExceptionMiddleware.__call__" , error_middleware_wrapper )
244+ wrap_function_wrapper (
245+ module , "ExceptionMiddleware.__call__" , error_middleware_wrapper
246+ )
221247
222- wrap_function_wrapper (module , "ExceptionMiddleware.http_exception" ,
223- wrap_exception_handler )
248+ wrap_function_wrapper (
249+ module , "ExceptionMiddleware.http_exception" , wrap_exception_handler
250+ )
224251
225- wrap_function_wrapper (module , "ExceptionMiddleware.add_exception_handler" ,
226- wrap_add_exception_handler )
252+ wrap_function_wrapper (
253+ module , "ExceptionMiddleware.add_exception_handler" , wrap_add_exception_handler
254+ )
227255
228256
229257def instrument_starlette_background_task (module ):
230258 wrap_function_wrapper (module , "BackgroundTask.__call__" , wrap_background_method )
231-
0 commit comments