Skip to content

Commit fed7f06

Browse files
TimPansinoumaannamalailrafeei
committed
Fix logging erroneous error messages in asgiref (#441)
Co-authored-by: Uma Annamalai <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Uma Annamalai <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Uma Annamalai <[email protected]>
1 parent a36a031 commit fed7f06

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

newrelic/core/context.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,40 @@
2525

2626

2727
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):
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

newrelic/hooks/adapter_asgiref.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def _bind_thread_handler(loop, source_task, *args, **kwargs):
2323

2424
def thread_handler_wrapper(wrapped, instance, args, kwargs):
2525
task = _bind_thread_handler(*args, **kwargs)
26-
with ContextOf(trace_cache_id=id(task)):
26+
with ContextOf(trace_cache_id=id(task), strict=False):
2727
return wrapped(*args, **kwargs)
2828

2929

3030
def main_wrap_wrapper(wrapped, instance, args, kwargs):
3131
awaitable = wrapped(*args, **kwargs)
32-
return context_wrapper_async(awaitable, current_trace())
32+
return context_wrapper_async(awaitable, current_trace(), strict=False)
3333

3434

3535
def instrument_asgiref_sync(module):

0 commit comments

Comments
 (0)