Skip to content

Commit

Permalink
Fix logging erroneous error messages in asgiref (#441)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
4 people committed Jan 20, 2022
1 parent a36a031 commit fed7f06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 21 additions & 15 deletions newrelic/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,40 @@


class ContextOf(object):
def __init__(self, trace=None, request=None, trace_cache_id=None):
def __init__(self, trace=None, request=None, trace_cache_id=None, strict=True):
self.trace = None
self.trace_cache = trace_cache()
self.thread_id = None
self.restore = None
self.should_restore = False

def log_propagation_failure(s):
if strict:
_logger.error(
"Runtime instrumentation error. Request context propagation failed. %s Report this issue to New Relic support.",
s,
)
else:
_logger.debug(
"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.",
s,
)

# Extract trace if possible, else leave as None for safety
if trace is None and request is None and trace_cache_id is None:
_logger.error(
"Runtime instrumentation error. Request context propagation failed. No trace or request provided. Report this issue to New Relic support.",
)
if strict:
log_propagation_failure("No trace or request provided.")
elif trace is not None:
self.trace = trace
elif trace_cache_id is not None:
self.trace = self.trace_cache._cache.get(trace_cache_id, None)
if self.trace is None:
_logger.error(
"Runtime instrumentation error. Request context propagation failed. No trace with id %s. Report this issue to New Relic support.",
trace_cache_id,
)
log_propagation_failure("No trace with id %d." % trace_cache_id)
elif hasattr(request, "_nr_trace") and request._nr_trace is not None:
# Unpack traces from objects patched with them
self.trace = request._nr_trace
else:
_logger.error(
"Runtime instrumentation error. Request context propagation failed. No context attached to request. Report this issue to New Relic support.",
)
log_propagation_failure("No context attached to request.")

def __enter__(self):
if self.trace:
Expand All @@ -77,17 +83,17 @@ def __exit__(self, exc, value, tb):
self.trace_cache._cache.pop(self.thread_id)


def context_wrapper(func, trace=None, request=None, trace_cache_id=None):
def context_wrapper(func, trace=None, request=None, trace_cache_id=None, strict=True):
@function_wrapper
def _context_wrapper(wrapped, instance, args, kwargs):
with ContextOf(trace=trace, request=request, trace_cache_id=trace_cache_id):
with ContextOf(trace=trace, request=request, trace_cache_id=trace_cache_id, strict=strict):
return wrapped(*args, **kwargs)

return _context_wrapper(func)


async def context_wrapper_async(awaitable, trace=None, request=None, trace_cache_id=None):
with ContextOf(trace=trace, request=request, trace_cache_id=trace_cache_id):
async def context_wrapper_async(awaitable, trace=None, request=None, trace_cache_id=None, strict=True):
with ContextOf(trace=trace, request=request, trace_cache_id=trace_cache_id, strict=strict):
return await awaitable


Expand Down
4 changes: 2 additions & 2 deletions newrelic/hooks/adapter_asgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def _bind_thread_handler(loop, source_task, *args, **kwargs):

def thread_handler_wrapper(wrapped, instance, args, kwargs):
task = _bind_thread_handler(*args, **kwargs)
with ContextOf(trace_cache_id=id(task)):
with ContextOf(trace_cache_id=id(task), strict=False):
return wrapped(*args, **kwargs)


def main_wrap_wrapper(wrapped, instance, args, kwargs):
awaitable = wrapped(*args, **kwargs)
return context_wrapper_async(awaitable, current_trace())
return context_wrapper_async(awaitable, current_trace(), strict=False)


def instrument_asgiref_sync(module):
Expand Down

0 comments on commit fed7f06

Please sign in to comment.