25
25
26
26
27
27
class 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 ):
29
29
self .trace = None
30
30
self .trace_cache = trace_cache ()
31
31
self .thread_id = None
32
32
self .restore = None
33
33
self .should_restore = False
34
34
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
+
35
47
# Extract trace if possible, else leave as None for safety
36
48
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." )
40
51
elif trace is not None :
41
52
self .trace = trace
42
53
elif trace_cache_id is not None :
43
54
self .trace = self .trace_cache ._cache .get (trace_cache_id , None )
44
55
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 )
49
57
elif hasattr (request , "_nr_trace" ) and request ._nr_trace is not None :
50
58
# Unpack traces from objects patched with them
51
59
self .trace = request ._nr_trace
52
60
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." )
56
62
57
63
def __enter__ (self ):
58
64
if self .trace :
@@ -77,17 +83,17 @@ def __exit__(self, exc, value, tb):
77
83
self .trace_cache ._cache .pop (self .thread_id )
78
84
79
85
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 ):
81
87
@function_wrapper
82
88
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 ):
84
90
return wrapped (* args , ** kwargs )
85
91
86
92
return _context_wrapper (func )
87
93
88
94
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 ):
91
97
return await awaitable
92
98
93
99
0 commit comments