2525
2626
2727class ContextOf (object ):
28- def __init__ (self , trace = None , request = None , trace_cache_id = None ):
28+ def __init__ (self , trace = None , request = None , trace_cache_id = None , strict = True ):
2929 self .trace = None
3030 self .trace_cache = trace_cache ()
3131 self .thread_id = None
3232 self .restore = None
3333 self .should_restore = False
3434
35+ def log_propagation_failure (s ):
36+ if strict :
37+ _logger .error (
38+ "Runtime instrumentation error. Request context propagation failed. %s Report this issue to New Relic support." ,
39+ s ,
40+ )
41+ else :
42+ _logger .debug (
43+ "Request context propagation failed. %s This may be an issue if there's an active transaction. Consult with New Relic support if further issues arise." ,
44+ s ,
45+ )
46+
3547 # Extract trace if possible, else leave as None for safety
3648 if trace is None and request is None and trace_cache_id is None :
37- _logger .error (
38- "Runtime instrumentation error. Request context propagation failed. No trace or request provided. Report this issue to New Relic support." ,
39- )
49+ if strict :
50+ log_propagation_failure ("No trace or request provided." )
4051 elif trace is not None :
4152 self .trace = trace
4253 elif trace_cache_id is not None :
4354 self .trace = self .trace_cache ._cache .get (trace_cache_id , None )
4455 if self .trace is None :
45- _logger .error (
46- "Runtime instrumentation error. Request context propagation failed. No trace with id %s. Report this issue to New Relic support." ,
47- trace_cache_id ,
48- )
56+ log_propagation_failure ("No trace with id %d." % trace_cache_id )
4957 elif hasattr (request , "_nr_trace" ) and request ._nr_trace is not None :
5058 # Unpack traces from objects patched with them
5159 self .trace = request ._nr_trace
5260 else :
53- _logger .error (
54- "Runtime instrumentation error. Request context propagation failed. No context attached to request. Report this issue to New Relic support." ,
55- )
61+ log_propagation_failure ("No context attached to request." )
5662
5763 def __enter__ (self ):
5864 if self .trace :
@@ -77,17 +83,17 @@ def __exit__(self, exc, value, tb):
7783 self .trace_cache ._cache .pop (self .thread_id )
7884
7985
80- def context_wrapper (func , trace = None , request = None , trace_cache_id = None ):
86+ def context_wrapper (func , trace = None , request = None , trace_cache_id = None , strict = True ):
8187 @function_wrapper
8288 def _context_wrapper (wrapped , instance , args , kwargs ):
83- with ContextOf (trace = trace , request = request , trace_cache_id = trace_cache_id ):
89+ with ContextOf (trace = trace , request = request , trace_cache_id = trace_cache_id , strict = strict ):
8490 return wrapped (* args , ** kwargs )
8591
8692 return _context_wrapper (func )
8793
8894
89- async def context_wrapper_async (awaitable , trace = None , request = None , trace_cache_id = None ):
90- with ContextOf (trace = trace , request = request , trace_cache_id = trace_cache_id ):
95+ async def context_wrapper_async (awaitable , trace = None , request = None , trace_cache_id = None , strict = True ):
96+ with ContextOf (trace = trace , request = request , trace_cache_id = trace_cache_id , strict = strict ):
9197 return await awaitable
9298
9399
0 commit comments