2525 wrap_function_wrapper ,
2626)
2727from newrelic .core .config import should_ignore_error
28- from newrelic .core .context import context_wrapper , current_thread_id
29- from newrelic .core .trace_cache import trace_cache
28+ from newrelic .core .context import ContextOf , context_wrapper
3029
3130
3231def framework_details ():
@@ -43,30 +42,10 @@ def bind_exc(request, exc, *args, **kwargs):
4342 return exc
4443
4544
46- class RequestContext (object ):
47- def __init__ (self , request ):
48- self .request = request
49- self .force_propagate = False
50- self .thread_id = None
51-
52- def __enter__ (self ):
53- trace = getattr (self .request , "_nr_trace" , None )
54- self .force_propagate = trace and current_trace () is None
55-
56- # Propagate trace context onto the current task
57- if self .force_propagate :
58- self .thread_id = trace_cache ().thread_start (trace )
59-
60- def __exit__ (self , exc , value , tb ):
61- # Remove any context from the current thread as it was force propagated above
62- if self .force_propagate :
63- trace_cache ().thread_stop (self .thread_id )
64-
65-
6645@function_wrapper
6746def route_naming_wrapper (wrapped , instance , args , kwargs ):
6847
69- with RequestContext ( bind_request (* args , ** kwargs )):
48+ with ContextOf ( request = bind_request (* args , ** kwargs )):
7049 transaction = current_transaction ()
7150 if transaction :
7251 transaction .set_transaction_name (callable_name (wrapped ), priority = 2 )
@@ -136,16 +115,14 @@ def wrap_add_middleware(wrapped, instance, args, kwargs):
136115 return wrapped (wrap_middleware (middleware ), * args , ** kwargs )
137116
138117
139- def bind_middleware_starlette (
140- debug = False , routes = None , middleware = None , * args , ** kwargs
141- ):
118+ def bind_middleware_starlette (debug = False , routes = None , middleware = None , * args , ** kwargs ): # pylint: disable=W1113
142119 return middleware
143120
144121
145122def wrap_starlette (wrapped , instance , args , kwargs ):
146123 middlewares = bind_middleware_starlette (* args , ** kwargs )
147124 if middlewares :
148- for middleware in middlewares :
125+ for middleware in middlewares : # pylint: disable=E1133
149126 cls = getattr (middleware , "cls" , None )
150127 if cls and not hasattr (cls , "__wrapped__" ):
151128 middleware .cls = wrap_middleware (cls )
@@ -171,11 +148,9 @@ async def wrap_exception_handler_async(coro, exc):
171148
172149def wrap_exception_handler (wrapped , instance , args , kwargs ):
173150 if is_coroutine_function (wrapped ):
174- return wrap_exception_handler_async (
175- FunctionTraceWrapper (wrapped )(* args , ** kwargs ), bind_exc (* args , ** kwargs )
176- )
151+ return wrap_exception_handler_async (FunctionTraceWrapper (wrapped )(* args , ** kwargs ), bind_exc (* args , ** kwargs ))
177152 else :
178- with RequestContext ( bind_request (* args , ** kwargs )):
153+ with ContextOf ( request = bind_request (* args , ** kwargs )):
179154 response = FunctionTraceWrapper (wrapped )(* args , ** kwargs )
180155 record_response_error (response , bind_exc (* args , ** kwargs ))
181156 return response
@@ -190,9 +165,7 @@ def wrap_server_error_handler(wrapped, instance, args, kwargs):
190165
191166
192167def wrap_add_exception_handler (wrapped , instance , args , kwargs ):
193- exc_class_or_status_code , handler , args , kwargs = bind_add_exception_handler (
194- * args , ** kwargs
195- )
168+ exc_class_or_status_code , handler , args , kwargs = bind_add_exception_handler (* args , ** kwargs )
196169 handler = FunctionWrapper (handler , wrap_exception_handler )
197170 return wrapped (exc_class_or_status_code , handler , * args , ** kwargs )
198171
@@ -217,7 +190,7 @@ async def wrap_run_in_threadpool(wrapped, instance, args, kwargs):
217190 return await wrapped (* args , ** kwargs )
218191
219192 func , args , kwargs = bind_run_in_threadpool (* args , ** kwargs )
220- func = context_wrapper (func , current_thread_id () )
193+ func = context_wrapper (func , trace )
221194
222195 return await wrapped (func , * args , ** kwargs )
223196
@@ -241,35 +214,21 @@ def instrument_starlette_requests(module):
241214
242215
243216def instrument_starlette_middleware_errors (module ):
244- wrap_function_wrapper (
245- module , "ServerErrorMiddleware.__call__" , error_middleware_wrapper
246- )
217+ wrap_function_wrapper (module , "ServerErrorMiddleware.__call__" , error_middleware_wrapper )
247218
248- wrap_function_wrapper (
249- module , "ServerErrorMiddleware.__init__" , wrap_server_error_handler
250- )
219+ wrap_function_wrapper (module , "ServerErrorMiddleware.__init__" , wrap_server_error_handler )
251220
252- wrap_function_wrapper (
253- module , "ServerErrorMiddleware.error_response" , wrap_exception_handler
254- )
221+ wrap_function_wrapper (module , "ServerErrorMiddleware.error_response" , wrap_exception_handler )
255222
256- wrap_function_wrapper (
257- module , "ServerErrorMiddleware.debug_response" , wrap_exception_handler
258- )
223+ wrap_function_wrapper (module , "ServerErrorMiddleware.debug_response" , wrap_exception_handler )
259224
260225
261226def instrument_starlette_exceptions (module ):
262- wrap_function_wrapper (
263- module , "ExceptionMiddleware.__call__" , error_middleware_wrapper
264- )
227+ wrap_function_wrapper (module , "ExceptionMiddleware.__call__" , error_middleware_wrapper )
265228
266- wrap_function_wrapper (
267- module , "ExceptionMiddleware.http_exception" , wrap_exception_handler
268- )
229+ wrap_function_wrapper (module , "ExceptionMiddleware.http_exception" , wrap_exception_handler )
269230
270- wrap_function_wrapper (
271- module , "ExceptionMiddleware.add_exception_handler" , wrap_add_exception_handler
272- )
231+ wrap_function_wrapper (module , "ExceptionMiddleware.add_exception_handler" , wrap_add_exception_handler )
273232
274233
275234def instrument_starlette_background_task (module ):
0 commit comments